EdX mitx:6.00.1x Introduction to Computer science and programming Using Python course Week 1:python Basics problem Set 1 Pro Blem 3

Source: Internet
Author: User
Tags ord

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

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.