0
votes

I make a list of the permutations between the number of fruits available and the number of farmers. In the permutations it enters the (1,2,..,max_number of fruits) and number of farmers(3). This is the code I use:

action_list = list(permutations(no_fruits(list), no_farmers(just a number)))

Imagine there are 3 fruits are 3 farmers. Fruit number 1 is an apple, number 2 is another apple and number 3 a mango. The information of which fruit it is is inside the object 1.fruit= apple for example.I have already a lis that says which numbers have the same fruits. We have these 6 combinations: (1,2,3) (1,3,2) (2,1,3) (2,3,1) (3,1,2) (3,2,1)

But now I don't want to differenciate apple 1 from apple 2. So the final output should be: (1,2,3) (1,3,2) (3,1,2)

How can I remove the other combinations? In other words, how can I remove the combinations where only fruit number 1 and number 2 swap places with each other?

Other relevant information: The number of farmers can be superior to the number of available fruits. In that case there would be some farmers that wouldn't have a fruit and would be represented as '-'. here is an example:

(1,2,-)

This means farmer 3 doesn't have a fruit, while farmer 1 and 2 have fruits 1 and 2, respectively.

1

1 Answers

1
votes

I haven't yet grasped to the fullest. However, here is an updated code to handle mismatch between fruits and farmers but requires two iterables:

def customZip(*obj):
    while any(obj):
        yield ([i.pop(0) if i else '-' for i in obj])

def permutation(fruits, farmers):
    populate = []
    for i in customZip(fruits, farmers):
        populate.append(i)

    def _permutation(obj, _len):
        if len(obj) == 1:
            yield obj
        else:
            l = []
            for i in range(len(obj)):
                for j in _permutation(obj[:i] + obj[i+1:], _len):
                    alt = obj[i:i+1] + j
                    if len(alt) != _len:
                        yield alt
                    else:
                        res = [i[0] for i in alt]
                        if res not in l:
                            l.append(res)
                            yield ([i[1] for i in alt], res)

    for j in _permutation(populate, len(populate)):
        yield j

for i in permutation(fruits, farmers):
    print(i)
#1
farmers = [1, 2, 3]
fruits = ['apple', 'apple', 'melon']

([1, 2, 3], ['apple', 'apple', 'melon'])
([1, 3, 2], ['apple', 'melon', 'apple'])
([3, 1, 2], ['melon', 'apple', 'apple'])
[Finished in 0.2s]
#2
farmers = [1, 2, 3]
fruits = ['apple', 'melon']

([1, 2, 3], ['apple', 'melon', '-'])
([1, 3, 2], ['apple', '-', 'melon'])
([2, 1, 3], ['melon', 'apple', '-'])
([2, 3, 1], ['melon', '-', 'apple'])
([3, 1, 2], ['-', 'apple', 'melon'])
([3, 2, 1], ['-', 'melon', 'apple'])
[Finished in 0.2s]