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, does not include raw_input
statements or define the variable on any s
. Our automated testing would provide a value of for you-so the code you submit in the s
following box should assume 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:") fromItertoolsImportCountdeffind_substring (input_string): Maxsubstr=input_string[0:0] forStartinchRange (len (input_string)): forEndinchCount (Start+len (MAXSUBSTR) +1): substr=Input_string[start:end]ifLen (substr)! = (End-start): Break ifSorted (substr) = =list (substr): Maxsubstr=substrreturnMaxsubstrlog_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 (Sub[-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 + = 1#elif s[i]>s[i+1]:#If Len (substr) <len (temp):#substr = Temp#temp = s[i+1]#i = i + 1#If Len (temp) >len (substr):#substr = Temp#print ' longest substring in alphabetical order is: ', substr#s = ' Cyqfjhcclkbxpbojgkar '#def Long_alphabet (input_string):#maxsubstr = input_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 alphabetical order is:%s"%bla
mitx:6.00.1x alphabetical substrings (python)