Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import fractions
- import itertools
- def calculate_result(numbers, respect_order_of_operations, allow_non_integer_division):
- def divides(x,y):
- return x % y == 0
- a,b,c,d,e,f,g,h,i = numbers
- if respect_order_of_operations:
- #a + (13*b/c) + d + (12*e) - f - 11 + (g*h/i) - 10 = 66
- if not allow_non_integer_division:
- if not divides(13*b, c): return None
- if not divides(g * h, i): return None
- return a + (13*b//c) + d + (12*e) - f - 11 + (g*h//i) - 10
- else:
- #(((((((((((a+13)*b)/c)+d)+12)*e)-f)-11)+g)*h)/i)-10 = 66
- if not allow_non_integer_division:
- if not divides((a+13)*b, c): return None
- if not divides((((((((((a+13)*b)/c)+d)+12)*e)-f)-11)+g)*h, i): return None
- return (((((((((((a+13)*b)/c)+d)+12)*e)-f)-11)+g)*h)/i)-10
- def get_all_solutions(respect_order_of_operations, allow_non_integer_division, allow_duplicate_numbers):
- digits = map(fractions.Fraction, range(1,10))
- possibilities = itertools.product(digits, repeat=len(digits)) if allow_duplicate_numbers else itertools.permutations(digits)
- for numbers in possibilities:
- if calculate_result(numbers, respect_order_of_operations, allow_non_integer_division) == 66:
- yield map(int, numbers)
- for respect_order_of_operations in [True, False]:
- for allow_non_integer_division in [False]:
- for allow_duplicate_numbers in [False]:
- print "Respecting order of operations:", respect_order_of_operations
- print "Allowing non-integer division:", allow_non_integer_division
- print "Allowing duplicate numbers:", allow_duplicate_numbers
- for solution in get_all_solutions(respect_order_of_operations, allow_non_integer_division, allow_duplicate_numbers):
- print solution
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement