Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Second solution in Uncategorized category for Mathematically Lucky Tickets by Bibiche
from itertools import product
spaces = ['', ' ']
symbols = [' ', '+', '-', '*', '/']
def checkio(s):
# contains the string-numbers on which oprations will be made
all_numbers = []
for t in product(spaces, repeat = 5):
all_numbers.append((''.join([s[i]+t[i] for i in range(5)])+ s[5]).split())
# returns {set of results}, [list of list of string-numbers]
# enter : list of string-numbers
def operates(L):
set_results = set()
list_numbers = []
N = len(L)
for t in product(symbols, repeat = N-1):
try:
res = list(map(eval,(''.join([L[i]+t[i] for i in range(N-1)]) + L[N-1]).split()))
if len(res) == 1 :
set_results.add(res[0])
else:
res = list(map(str, res))
if res != L and not res in list_numbers:
list_numbers.append(res)
except:
pass
return set_results, list_numbers
# step by step, recursively construct results of operations
to_treat = all_numbers
while to_treat != []:
to_treat2 = []
for p in to_treat:
s_res, l_num = operates(p)
if 100 in s_res:
return False
to_treat2 += l_num
to_treat = [p for p in to_treat2 if p not in to_treat]
return True
#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 * (38 + ((7 + 5) * 6))) = 100"
May 23, 2013
Comments: