Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Сombinatorics solution in Uncategorized category for Mathematically Lucky Tickets by bryukh
from itertools import combinations, product, permutations
from operator import add, sub, mul, truediv
from math import isnan
UNLUCKY = 100
OPERATORS = [add, sub, mul, truediv]
def check_separated(variant):
numbers = [int(v) for v in variant]
oper_length = len(variant) - 1
for oper_var in product(OPERATORS, repeat=oper_length):
for order in permutations(range(len(oper_var))):
temp = numbers[:]
num = 0
for i in order:
op = oper_var[i]
left = i
while isnan(temp[left]):
left -= 1
right = i + 1
while isnan(temp[right]):
right += 1
try:
num = op(temp[left], temp[right])
except ZeroDivisionError:
continue
temp[left] = num
temp[right] = float("NaN")
if round(num, 6) == UNLUCKY:
return False
return True
def split_ticket(s_str, dividers):
"""str, list[int] -> list[str]
Split string by dividers index
"""
res = []
start = 0
dividers = list(dividers) + [len(s_str)]
for d in dividers:
fragment = s_str[start:d]
if fragment:
res.append(fragment)
start = d
return res
def checkio(ticket):
#degenerated case
if int(ticket) == UNLUCKY:
return False
l_ticket = len(ticket)
for i in range(1, l_ticket):
for var in combinations(range(1, l_ticket), i):
separated = split_ticket(ticket, var)
if not check_separated(separated):
return False
#replace this for solution
return True
May 30, 2013
Comments: