Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from itertools import permutations
- def apply(uno, dos, num):
- if num == 1:
- return uno + dos
- if num == 2:
- return uno - dos
- if num == 3:
- return uno * dos
- if num == 4:
- return uno / dos
- def churn(listing, lineup):
- modifier = 0
- for operator in lineup:
- if operator[1] == 2:
- listing[2 - modifier] = apply(listing[2 - modifier], listing[2 - modifier + 1], operator[0])
- listing[2 - modifier + 1] = ""
- listing.pop(listing.index(""))
- if operator[1] == 1:
- listing[1 - modifier] = apply(listing[1 - modifier], listing[1 - modifier + 1], operator[0])
- listing[1 - modifier + 1] = ""
- listing.pop(listing.index(""))
- modifier += 1
- if operator[1] == 0:
- listing[0] = apply(listing[0], listing[1], operator[0])
- listing[1] = ""
- listing.pop(listing.index(""))
- modifier += 1
- return listing[0]
- operators = []
- for first in range(1, 5):
- for second in range(1, 5):
- for third in range(1, 5):
- operators.append([first, second, third])
- digits = []
- for one in range(1, 10):
- for two in range(one + 1, 10):
- for three in range(two + 1, 10):
- for four in range(three + 1, 10):
- if len([one, two, three, four]) == len({one, two, three, four}):
- digits.append([one, two, three, four])
- best, answer = 0, []
- for x in digits:
- chain = []
- for y in permutations(x):
- y = list(y)
- for a in operators:
- for b in permutations([0, 1, 2]):
- try:
- entry = churn([y[0], y[1], y[2], y[3]], zip(a, b))
- if entry > 0 and abs(round(entry) - entry) < 0.0000000005 and round(entry) not in chain:
- chain.append(round(entry))
- except ZeroDivisionError:
- pass
- starting, length = 0, 0
- for z in sorted(set(chain)):
- if z != starting + 1:
- if length > best:
- best = length
- answer = x
- break
- starting += 1
- length += 1
- print(best, answer)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement