Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
split into groups and recursion check solution in Clear category for Mathematically Lucky Tickets by kdim
def check(t):
if len(t) < 2: return t!=[100] # end of recursion, "False" if 100, "True" if not
for i in range(len(t)-1): # go through all position between numbers (analogue of brackets)
x,y = t[i:i+2] # get two number around of position
for j in [x+y, x-y, x*y, x/y if y else 0]: # go through all action with number in position
if not check(t[:i]+[j]+t[i+2:]): return False # recursion with decrement number of array
return True
def checkio(data):
l = len(data)-1 # split into possible groups of numbers
split_group = []
for i in range(2**l): # there are total 2**5 (l==5) combinations
s = f'{i:b}'.rjust(l,'0')+'0' # get a binary string like "000100", 1-" "(whitespace), 0-"" (empty string)
a = "".join(map(lambda x,y: x+' '*int(y), data, s)).split() # a==['2713', '53']
split_group.append(list(map(int,a))) # [[271353], [27135, 3], [2713, 53] ... [2, 7, 1, 35, 3], [2, 7, 1, 3, 53], [2, 7, 1, 3, 5, 3]]
return all([check(i) for i in split_group]) # if no combination gives "100", then "True"
Jan. 6, 2021
Comments: