Search code examples
setlist-comprehensionnested-loops

Comprehensions with nested loop to create set


I've to create a set which is the all sum of all combinations of three coins that are dividable by 65. what I have so far is the following:

  • coins = [200, 100, 50, 20, 10, 5]

my code so far:

set([x + y + i for x in coins for y in coins for i in coins if (x+y+i)%65])

the outcome has to be:

set(result) == {65, 130, 260}

Solution

  • You need to see if the modulus equals zero (there is no remainder left).

    m = set([x + y + i for x in coins for y in coins for i in coins if (x+y+i)%65==0])
    
    print(m)
    
    >>> {65, 130, 260}