Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
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
def hypothesize(data):
yield Fraction(data)
if len(data) == 1:
return
for pair in ((data[:i], data[i:]) for i in range(6)):
if pair[0] and pair[1]:
# I could filter those that are obviously zeroes...
for left in hypothesize(pair[0]):
for right in hypothesize(pair[1]):
for op in ops:
try:
yield op(left, right)
except ZeroDivisionError:
pass
def checkio(data):
# for obvious reasons
if int(data) < 100:
return True
# 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
Comments: