Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Generator_Usage solution in Clear category for Mathematically Lucky Tickets by Sillte
from itertools import product
_operations = [lambda a, b: a+b, lambda a, b: a-b, lambda a, b: a*b, lambda a, b: a/b if b != 0 else None]
def checkio(data):
def c_gen(s):
yield int(s)
yield from filter(lambda n: n != None, ([op(l, r) for i in range(1, len(s))
for l, r, op in product(c_gen(s[:i]), c_gen(s[i:]), _operations)]))
return not any([e == 100 for e in c_gen(data)])
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert checkio('000100') == False, "You can not transform it to 100"
assert checkio('707409') == True, "You can not transform it to 100"
assert checkio('595347') == False, "(5 + ((9 / (3 / 34)) - 7)) = 100"
assert checkio('271353') == False, "(2 - (7 * (((1 / 3) - 5) * 3))) = 100"
""" Original.
from itertools import product
_operations = [lambda a, b: a+b, lambda a, b: a-b, lambda a, b: a*b, lambda a, b: a/b if b != 0 else None]
def checkio(data):
def _inner(s):
if len(s) == 1:
return [int(s)]
cands = [int(s)]
for i in range(1, len(s)):
lefts = _inner(s[:i])
rights = _inner(s[i:])
cands += list(filter(lambda n: n != None, (op(l, r) for l, r, op in product(lefts, rights, _operations))))
return cands
result = _inner(data)
return not any([e == 100 for e in result])
"""
April 15, 2018