I've completed a codingame assessment but i was not able to pass all tests on one challenge about lucky numbers. I need some help.
Definition: a lucky number is a number that contains either 6s or 8s in its digits but not both.
For example 16, 38, 666 are lucky numbers. 234, 687 are not.
Task: Print the number of lucky numbers between L and H inclusive.
Constraints:
L < HMemory:
512MBTime:
6 seconds
Here's what I did (I chose Python as programming language)
def is_lucky(nbr):
nbr = [*str(nbr)]
if '6' in nbr and '8' in nbr:
return False
if '6' in nbr or '8' in nbr:
return True
return False
n_lucky_number = 0
for number in range(L, H + 1):
n_lucky_number += is_lucky(number)
print(n_lucky_number)
I failed the tests where L and H are big (or the gap between) due to timeout.
L, H = 1, 1000000000000000000
L, H = 92871036442, 3363728910382456
Could someone help me to optimize my code?