fork download
  1. import itertools
  2.  
  3. def twentyfour(cards):
  4. for nums in itertools.permutations(cards):
  5. for ops in itertools.product('+-*/', repeat=3):
  6. bds1 = '({0}{4}{1}){5}({2}{6}{3})'.format(*nums, *ops)
  7. bds2 = '(({0}{4}{1}){5}{2}){6}{3}'.format(*nums, *ops)
  8. bds3 = '{0}{4}({1}{5}({2}{6}{3}))'.format(*nums, *ops)
  9.  
  10. for bds in [bds1, bds2, bds3]:
  11. try:
  12. if abs(eval(bds) - 24.0) < 1e-10:
  13. bds += '=24'
  14. return bds
  15. except ZeroDivisionError:
  16. continue
  17.  
  18. return 'Not found!'
  19.  
  20. print(twentyfour([int(input()) for i in range(4)]))
  21.  
Success #stdin #stdout 0.02s 27712KB
stdin
2
3
4
5
stdout
2*(3+(4+5))=24