Sunday, April 14, 2024
HomeSoftware EngineeringThe best way to Take away Duplicate Phrases from a String in...

The best way to Take away Duplicate Phrases from a String in Python


The problem

Take away all duplicate phrases from a string, leaving solely single (first) phrases entries.

Instance:

Enter:

‘alpha beta beta gamma gamma gamma delta alpha beta beta gamma gamma gamma delta’

Output:

‘alpha beta gamma delta’

The answer in Python code

Choice 1:

def remove_duplicate_words(s):
    def unique_list(l):
        ulist = []
        [ulist.append(x) for x in l if x not in ulist]
        return ulist

    return ' '.be a part of(unique_list(s.break up()))

Choice 2:

def remove_duplicate_words(s):
    return ' '.be a part of(dict.fromkeys(s.break up()))

Choice 3:

def remove_duplicate_words(s):
    return ' '.be a part of(sorted(set(s.break up()), key = s.index))

Take a look at instances to validate our resolution

import check
from resolution import remove_duplicate_words

@check.describe("Fastened Checks")
def fixed_tests():
    @check.it('Fundamental Take a look at Circumstances')
    def basic_test_cases():
        check.assert_equals(remove_duplicate_words("alpha beta beta gamma gamma gamma delta alpha beta beta gamma gamma gamma delta"), "alpha beta gamma delta")
        check.assert_equals(remove_duplicate_words("my cat is my cat fats"), "my cat is fats")

Further check instances

import  check
from resolution import remove_duplicate_words

@check.describe("Fastened Checks")
def fixed_tests():
    @check.it('Fundamental Take a look at Circumstances')
    def basic_test_cases():
        check.assert_equals(remove_duplicate_words("alpha beta beta gamma gamma gamma delta alpha beta beta gamma gamma gamma delta"), "alpha beta gamma delta")
        check.assert_equals(remove_duplicate_words("my cat is my cat fats"), "my cat is fats")

@check.describe("Random Checks")
def randou8():
    
    import random
    from string import ascii_letters as lets
    
    def mnu8(s):
        a = set(); b = a.add
        return ' '.be a part of([x for x in s.split(" ") if not (x in a or b(x))])

    @check.it("Testing for remove_duplicate_words") 
    def _():
        for i in vary(0,100):
            randWords = []
            for j in vary(0,10):
                c, cnt, randWord = random.randrange(8,12),0,''
                whereas cnt < c:       
                    randWord+=lets[random.randrange(0,len(lets))] 
                    cnt+=1              
                randWords.append(randWord)      
            repeat = random.randrange(1,len(randWords))       
            for okay in vary(0,repeat):
                idx = random.randrange(2,len(randWords))
                randWords.insert(idx,randWords[random.randrange(0,len(randWords))])       
            res = ' '.be a part of(randWords)
            exp = mnu8(res)
            check.assert_equals(remove_duplicate_words(res), exp)
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments