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
for problems such as these, do not include raw_input
statements or define the Variable s
in any". Our automated testing would provide a value of s
for you-so the CO De you submit in the following box should Assume s
is already Defined. If You is confused by this instruction, please review L4 problems and one before you begin this problem set.
Note:this problem is fairly challenging. We encourage you to work smart. If you've spent more than a few hours on this problem, we suggest that's move on to a different part of the course. If you have time, come-problem-had a break and cleared your head.
# from string Import lowercases = raw_input ("Please input a string:") from Itertools import countdef find_substring (input_s Tring): Maxsubstr = input_string[0:0]for start in range (Len (input_string)): As End in count (Start+len (MAXSUBSTR) +1): substr = Input_string[start:end]if len (substr)! = (End-start): Breakif sorted (substr) = = List (substr): Maxsubstr = Substrreturn maxsubstrlog_substr = find_substring (s) print ("Longest substring in alphabetical order is:" +log_substr) # Lowe = ' ABCDEFGHIJKLMNOPQRSTUVWXYZ ' # cont = []# sub = []# for i in s:# if Len (sub) >= 1 and Lowe.index (Sub[-1]) + 1! = Lowe.index (i): # Cont.append (". Join (sub)) # sub = []# sub.append (i) # cont = SORTED (cont, Key = Len, reverse=true) # print cont[0]# cont = []# sub = []# for i in s:# if Len (sub) >= 1 and Lowercase.index (su B[-1]) + 1! = Lowercase.index (i): # Cont.append (". Join (sub)) # sub = []# sub.append (i) # cont = Sorted (cont, key = Len, reverse=true) # Print cont[0]# counter = 0# i = 0# length = Len (s) # temp = s[0]# substr = s[0]# while (i<=length-1): # if S[I]<S[I+1]: # temp + = s[i+1]# counter + = # elif s[i]>s[i+1]:# If Len (substr) <len (temp): # substr = temp# Temp = s[i+1]# i = i + # if Len (temp) >len (substr): # substr = temp# print ' Longest sub String in alphabetical order is: ', substr#s = ' Cyqfjhcclkbxpbojgkar ' # def Long_alphabet (input_string): # maxsubstr = INP UT_STRING[0:0] # Empty slice (to accept subclasses of str) # for start in range (Len (input_string)): # O (n) # for End in count (start + len (maxsubstr) + 1): # O (m) # substr = input_string[start:end] # o (m) # If Len (substr)! = (End-start): # found duplicates or eos# break# if sorted (substr) = = List (substr) : # maxsubstr = substr# return maxsubstr# bla = (Long_alphabet (s)) # print "Longest substring in Alphabe Tical order is:%s "%bla
mitx:6.00.1x alphabetical substrings (python)