fork download
  1. from itertools import product, permutations, izip
  2.  
  3. def find_countdown(instr):
  4. innums = [x for x in instr.split()]
  5. for nums in permutations(innums[:-1]):
  6. for symbs in product('*/+-', repeat=5):
  7. teststr = nums[0]
  8. for num, symb in izip(nums[1:], symbs):
  9. teststr = '(' + teststr + symb.center(3) + num + ')'
  10. if eval(teststr) == int(innums[-1]):
  11. print teststr.translate(None, '()') + ' = ' + innums[-1]
  12. return
  13.  
  14. find_countdown('1 3 7 6 8 3 250')
  15. find_countdown('25 100 9 7 3 7 881')
  16. find_countdown('6 75 3 25 50 100 952')
Success #stdin #stdout 3.82s 23352KB
stdin
Standard input is empty
stdout
3 + 8 * 7 + 6 * 3 + 1 = 250
25 - 9 * 7 * 7 + 100 - 3 = 881
6 + 100 * 75 * 3 - 50 / 25 = 952