fork download
  1. import itertools
  2. import operator
  3. import fractions
  4.  
  5. operators = [operator.add, operator.sub, operator.div, operator.mul]
  6. numbers = [1, 5, 6, 7]
  7.  
  8. numbers = [fractions.Fraction(n) for n in numbers]
  9.  
  10. for ops in itertools.product(operators, repeat=3):
  11. for nums in itertools.permutations(numbers):
  12.  
  13. value = ops[0](nums[0], nums[1])
  14. value = ops[1](value, nums[2])
  15. value = ops[2](value, nums[3])
  16.  
  17. if value == 21:
  18. print 21, str(ops), [str(n) for n in nums]
  19. elif value == 16:
  20. print 16, str(ops), [str(n) for n in nums]
  21. elif value == 11:
  22. print 11, str(ops), [str(n) for n in nums]
  23.  
Success #stdin #stdout 0.08s 8528KB
stdin
Standard input is empty
stdout
11 (<built-in function sub>, <built-in function mul>, <built-in function sub>) ['7', '5', '6', '1']