mitx:6.00.1x alphabetical substrings (python)

Source: Internet
Author: User


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]&LT;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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.