Recursive Generators solution in Uncategorized category for Mathematically Lucky Tickets by AndriusMk
# migrated from python 2.7
from fractions import Fraction
# operand look up table OLUT :)
ops = [lambda a, b: Fraction(a * b),
lambda a, b: Fraction(a + b),
lambda a, b: Fraction(a - b),
lambda a, b: Fraction(a, b) ]
# love recursive generators
if len(data) == 1:
for pair in ((data[:i], data[i:]) for i in range(6)):
if pair and pair:
# I could filter those that are obviously zeroes...
for left in hypothesize(pair):
for right in hypothesize(pair):
for op in ops:
yield op(left, right)
# for obvious reasons
if int(data) < 100:
# i'm not sure if we have to check all possibilities
return not any(i==100 for i in hypothesize(data))
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert checkio('000000') == True, "All zeros"
assert checkio('707409') == True, "You can not transform it to 100"
assert checkio('595347') == False, "(5 * (6 + (1 + (40 / 3)))) = 100"
assert checkio('271353') == False, "(2 - (7 * (((1 / 3) - 5) * 3))) = 100"
May 30, 2013