The code runs fine and the output seems ok. But my print
is inside the for loop, this makes me doubt that the coding is correct:
s = 'azcbobobegghakl'
i = 0
increase = 0
longest = 1
for i in range(len(s) - 1):
if s[i+1] >= s[i]:
increase +=1
else:
if increase > longest:
longest = increase
print ("
Longest substring in alphabetical order is:"+""+s[i-longest:i+1])
increase =0
#With the help of MIT pythontutor(www.pythontutor.com) and
their exercise code test machine,I reworte my code and achieve its function in any string.
i = 0
increase = 0
longest = 0
tem_longest= 0
max_i = 0
start = 0
end = 0
for i in range(len(s)-1 ):
if s[i+1] >= s[i]:
increase +=1
tem_longest = increase
max_i=i+1
if increase==(len(s)-1):
longest = increase
max_i=i+1
start = max_i-longest
end = max_i+1
break
else:
max_i=i
tem_longest = increase
increase =0
if tem_longest > longest:
longest = tem_longest
start = max_i-longest
end = max_i+1
if i+1 == (len(s)-1) and tem_longest == 0 and longest == 0:
start = 0
end = 1
print ("Longest substring in alphabetical order is:"+""+s[start:end])