Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Mathematically Lucky Tickets by Leespy
# 对数字串排列,保持数字顺序不变,形成数字组合列表
def permutated(number:str):
newlist = []
if len(number) == 1:
newlist = [[int(number)]]
return newlist
else:
temp_num = int(number[-1])
number = number[:-1]
# tuple_num = []
for tuple_num in permutated(number):
newlist.append(tuple_num + [temp_num])
newlist.append(tuple_num[:-1]+[tuple_num[-1]*10 + temp_num])
return newlist
# from ast import Expression
# 对一组数字四则运算,若任一算式结果为100,返回TRUE(not lucky number),否则返回False(lucky number)
operations = ('ADD','SUB','MUL','DIV')
def calculated(numbers:list):
temp_num:float
Isnotlucky = False
if len(numbers) ==1 :
if numbers[0] == 100:
return True
else:
return False
else:
for i in range(len(numbers)-1):
if Isnotlucky:
return True
for op in operations:
if op == 'ADD':
temp_num = numbers[i] + numbers[i+1]
# formula = '(%d + %d)'%(numbers[i],numbers[i+1])
new_numbs = numbers[:i] + [temp_num] + numbers[i+2:]
Isnotlucky = calculated(new_numbs)
if Isnotlucky:
return True
elif op == 'SUB':
temp_num = numbers[i] - numbers[i+1]
# formula = '(%d - %d)'%(numbers[i],numbers[i+1])
new_numbs = numbers[:i] + [temp_num] + numbers[i+2:]
Isnotlucky = calculated(new_numbs)
if Isnotlucky:
return True
elif op == 'MUL':
temp_num = numbers[i] * numbers[i+1]
# formula = '(%d * %d)'%(numbers[i],numbers[i+1])
new_numbs = numbers[:i] + [temp_num] + numbers[i+2:]
Isnotlucky = calculated(new_numbs)
if Isnotlucky:
return True
elif op == 'DIV':
try:
temp_num = numbers[i] / numbers[i+1]
except ZeroDivisionError:
continue
else:
# formula = '(%d / %d)'%(numbers[i],numbers[i+1])
new_numbs = numbers[:i] + [temp_num] + numbers[i+2:]
Isnotlucky = calculated(new_numbs)
if Isnotlucky:
return True
# else:
# continue
# i += 1
if Isnotlucky:
return True
new_numbs = []
# formula = ''
def checkio(data):
#replace this for solution
for i in permutated(data):
if calculated(i):
return False
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 + ((9 / (3 / 34)) - 7)) = 100"
assert checkio('271353') == False, "(2 - (7 * (((1 / 3) - 5) * 3))) = 100"
March 16, 2022
Comments: