Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Professional solution in Clear category for Mathematically Lucky Tickets by veky
def possible_values(substring: str) -> set:
"""All values obtainable from substring."""
from operator import add, sub, mul, truediv as div
entire_number = int(substring)
results = {entire_number}
for split_position in range(1, len(substring)):
# split substring in all possible ways
left_part = substring[:split_position]
right_part = substring[split_position:]
for left_operand in possible_values(left_part):
for operation in add, sub, mul, div:
for right_operand in possible_values(right_part):
try:
current_result = operation(left_operand, right_operand)
except ZeroDivisionError:
pass
else:
results.add(current_result)
return results
def checkio(ticket_number: str) -> bool:
"""Whether the ticket_number is lucky."""
return 100 not in possible_values(ticket_number)
assert all((
checkio('000000') is True,
checkio('707409') is True,
checkio('595347') is False,
checkio('593347') is False,
checkio('271353') is False,
))
Aug. 31, 2013
Comments: