Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 7th, 2012  |  syntax: Python  |  size: 0.63 KB  |  hits: 29  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. from itertools import chain, combinations
  2.  
  3. def party_combinations(blah):
  4.   return chain(*map(lambda x: combinations(blah, x), range(0, len(blah)+1)))
  5.  
  6. def print_subset(lcombo, value):
  7.   lstr = "%d : " % value
  8.   for i in lcombo:
  9.     lstr += parties[i][0] + " "
  10.   print lstr
  11.  
  12. def more_than_enough(lcombo):
  13.   local_sum = 0
  14.   for i in lcombo:
  15.     local_sum += parties[i][1]
  16.   if local_sum > 150:
  17.     print_subset(lcombo, local_sum)
  18.  
  19. parties = { 1:('nd', 108), 2: ('syriza',52), 3: ('pasok',41), 4: ('ae',33), 5: ('kke',26), 6: ('xa',21), 7:('dhmar',19)}
  20.  
  21. for subset in party_combinations(parties):
  22.   more_than_enough(subset)