I don't understand how these two implementations generating the look and say sequence have different times of execution because they perform really similar operations but more I increase n
more the time difference between the two codes is bigger.
The two algorithms are both O(n^2)
for the n-th number of the sequence since the while loop in the first one iterates n
times as the for loop in the second one and the nested for loop in the first one scans all elements as the two nested while loops in the second one.
this is the first one :
def look_and_say_sequence(first, n):
while n != 1 :
succ, start, k = '', first[0], 0
for x in first :
if x != start :
succ, start, k = succ + str(k) + start, x, 1
else :
k = k + 1
first = succ + str(k) + start
n -= 1
return first
look_and_say_sequence('1', 48) # 10 SECONDS
and this is the second one :
def look_and_say_sequence(first, n):
for _ in range(n-1):
seq, res, ind = first, '', 0
while ind < len(seq):
pending, c = seq[ind], 0
while ind < len(seq) and pending == seq[ind]:
c += 1; ind += 1
res += "{}{}".format(c, pending)
return res
look_and_say_sequence('1', 48) # 2 SECONDS
So, how can it be that there is a factor of 5 between the two implementations?
Thank you for the help!