Re Van B
Re Van B

Reputation: 41

Error: returned list doesn't a valid sequence of combinations

Assignment: I am writing a function that takes two sequences (lists or tuples) of words (strings). The function also has a third optional parameter amount that may take an integer argument. The function must return a list of strings, whose elements are formatted as a word1 word2 with word1 a randomly chosen word from the first sequence and word2 a randomly chosen word from the second sequence. The function must make different combinations until all words from one of the given sequences have been used, taking into account that each word from a sequence can be used at most once. If a third argument is given, no more than the given amount of combinations should be included in the returned list.

Problem:

Error: returned list doesn't a valid sequence of combinations

For example:

colors = ['purple', 'yellow', 'green']
fruits = ('grape', 'banana', 'apple')
combine(colors, fruits) 

Correct answer:

['a purple grape', 'a green banana', 'a yellow apple']

Answer with my code:

['a purple apple', 'a yellow grape', 'a green grape']

Error: returned list doesn't a valid sequence of combinations

This is my code:

def combine(colors, fruits, amount=None):

    '''
    >>> colors = ['purple', 'yellow', 'green']
    >>> fruits = ('grape', 'banana', 'apple')
    >>> combine(colors, fruits)
    ['a purple grape', 'a green banana', 'a yellow apple']
    >>> combine(colors, fruits)
    ['a purple grape', 'a green apple', 'a yellow banana']
    >>> combine(colors, fruits)
    ['a purple apple', 'a yellow grape', 'a green banana']
    >>> combine(colors, fruits, amount=1)
    ['a purple grape']
    >>> combine(colors, fruits, amount=2)
    ['a yellow apple', 'a green banana']    
    >>> combine(colors, fruits, amount=4)
    ['a yellow banana', 'a green grape', 'a purple apple']
    '''


    import random
    secure_random = random.SystemRandom()

    l = list()
    count = 0

    if amount is None:

        b = len(list(colors))

        if len(list(fruits)) >= len(list(colors)):
            b = len(list(colors))

        if len(list(fruits)) <= len(list(colors)):
            b = len(list(fruits))

        while count < b:


            word1 = "a" 
            listcolors = list(colors)
            word2 = random.choice(listcolors)   # random.choice()  random.shuffle()
            listcolors.remove(word2)
            listfruits = list(fruits)
            word3 = random.choice(listfruits)
            listfruits.remove(word3)
            k = str(word1 + ' ' + word2 + ' ' + word3)    
            l.append(k)
            count += 1

    else:

        if amount < len(list(colors)) and amount < len(list(fruits)) :
            amount2 = amount
        if amount > len(list(colors)) and amount > len(list(fruits)):
            if len(list(colors)) >= len(list(fruits)):
                amount2 = len(list(fruits))
            if len(list(colors)) <= len(list(fruits)):
                amount2 = len(list(colors))


        while count < amount2:
            word1 = "a" 
            listcolors = list(colors)
            word2 = random.choice(listcolors)   # random.choice()  random.shuffle()
            listcolors.remove(word2)
            listfruits = list(fruits)
            word3 = random.choice(listfruits)
            listfruits.remove(word3)
            k = str(word1 + ' ' + word2 + ' ' + word3)    
            l.append(k)
            count += 1

    return l

Upvotes: 0

Views: 47

Answers (2)

Reblochon Masque
Reblochon Masque

Reputation: 36702

You are overthinking this, what you need is each time you pick one word, remove it from the list; or in the following example, shuffle both lists and pair the words two by two, without repeating.

import random
def combine(colors, fruits, amount=None):

    assert len(fruits) == len(colors)

    random.shuffle(colors)
    random.shuffle(fruits)

    result = []
    for combi in zip(colors, fruits):
        result.append(list(combi))
    if amount is not None and amount < len(fruits):
        result = result[: amount]
    return result

Upvotes: 1

Re Van B
Re Van B

Reputation: 41

The correct answer:

def combine(colors, fruits, amount=None):

    '''
    >>> colors = ['purple', 'yellow', 'green']
    >>> fruits = ('grape', 'banana', 'apple')
    >>> combine(colors, fruits)
    ['a purple grape', 'a green banana', 'a yellow apple']
    >>> combine(colors, fruits)
    ['a purple grape', 'a green apple', 'a yellow banana']
    >>> combine(colors, fruits)
    ['a purple apple', 'a yellow grape', 'a green banana']
    >>> combine(colors, fruits, amount=1)
    ['a purple grape']
    >>> combine(colors, fruits, amount=2)
    ['a yellow apple', 'a green banana']    
    >>> combine(colors, fruits, amount=4)
    ['a yellow banana', 'a green grape', 'a purple apple']
    '''


    import random
    secure_random = random.SystemRandom()

    listcolors = list(colors)
    listfruits = list(fruits)

    l = list()
    count = 0

    if amount is None:

        b = len(list(colors))

        if len(list(fruits)) >= len(list(colors)):
            b = len(list(colors))

        if len(list(fruits)) <= len(list(colors)):
            b = len(list(fruits))

        while count < b:


            word1 = "a" 
            word2 = random.choice(listcolors)   # random.choice()  random.shuffle()
            listcolors.remove(word2)
            word3 = random.choice(listfruits)
            listfruits.remove(word3)
            k = str(word1 + ' ' + word2 + ' ' + word3)    
            l.append(k)
            count += 1

    else:

        if amount < len(list(colors)) and amount < len(list(fruits)) :
            amount2 = amount
        if amount > len(list(colors)) and amount > len(list(fruits)):
            if len(list(colors)) >= len(list(fruits)):
                amount2 = len(list(fruits))
            if len(list(colors)) <= len(list(fruits)):
                amount2 = len(list(colors))

            listcolors = list(colors)
            listfruits = list(fruits)

        while count < amount2:
            word1 = "a" 
            word2 = random.choice(listcolors)   # random.choice()  random.shuffle()
            listcolors.remove(word2)
            word3 = random.choice(listfruits)
            listfruits.remove(word3)
            k = str(word1 + ' ' + word2 + ' ' + word3)    
            l.append(k)
            count += 1

    return l

Upvotes: 1

Related Questions