Assume is s
a string of lower case characters.
Write A program This prints the longest substring of in s
which the letters occur in alphabetical order. For example, if s = ‘azcbobobegghakl‘
, then your program should print
Longest substring in alphabetical order Is:beggh
In the case of ties, print the first substring. For example, if s = ‘abcbcd‘
, then your program should print
Longest substring in alphabetical order IS:ABC
# Paste Your code into this box
Count = 1
result = S[0]
While S:
Newcount = 1
Newresult = ' '
i = 0
While I+1<len (s):
If Ord (S[i]) <= Ord (s[i+1]):
Newcount+=1
NEWRESULT+=S[I+1]
Else
Break
I+=1
If Newcount>count:
Count = Newcount
result = S[0]+newresult
s = s[i+1:]
Print (Result)
Note: the sort () function or some other advanced function is not used because it is a pre-course.
Here are a few points to note:
1) Do not remove Newcount, Newresult variable, because to find the oldest string in S, so if you can find the back to replace the front slightly shorter substring
2) Ord (S[i]) <= Ord (s[i+1]):% note is less than equals sign
3) s = s[i+1:] % Note i+1: no space behind
EdX mitx:6.00.1x Introduction to Computer science and programming Using Python course Week 1:python Basics problem Set 1 Pro Blem 3