edX MITx: 6.00.1x Introduction to Computer Science and Programming Using Python 课程 Week 1: Python Basics Problem Set 1 Problem 3

Assume s is a string of lower case characters.

Write a program that prints the longest substring of s in 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)

注:因为只是课程前期,故未使用sort()函数或一些其他高级函数。

有几点需要注意的地方:

1)不可去掉 newcount, newresult 变量,因为要找s中的最长子串,故如果后面能找到要替换掉前面稍短的子串

2)ord(s[i]) <= ord(s[i+1]):     %注意是小于等于号

3)s = s[i+1:]        %注意i+1:后面不加空格

时间: 2024-10-11 04:46:00

edX MITx: 6.00.1x Introduction to Computer Science and Programming Using Python 课程 Week 1: Python Basics Problem Set 1 Problem 3的相关文章

MITx: 6.00.1x Introduction to Computer Science and Programming Using Python Week 2: Simple Programs 4. Functions

ESTIMATED TIME TO COMPLETE: 18 minutes We can use the idea of bisection search to determine if a character is in a string, so long as the string is sorted in alphabetical order. First, test the middle character of a string against the character you'r

MIT Introduction to Computer Science and Programming (Lesson one )

MIT Introduction to Computer Science and Programming (Lesson one ) 这篇文是记载 MIT 计算机科学及编程导论 第一集 的笔记 Lesson one : Goals of the course;what is computation;introduction to data types,operators,and variables 一 讲解课程的任务.课程目标 目标 像一个计算机科学家一样思考 都能够读写程序 tacking t

Note 2 for &lt;Pratical Programming : An Introduction to Computer Science Using Python 3&gt;

Book Imformation : <Pratical Programming : An Introduction to Computer Science Using Python 3> 2nd Edtion Author : Paul Gries,Jennifer Campbell,Jason Montojo Page : Chapter 2.3 to Chapter 2.5 1.A type consists of two things: (1).a set of values (2).

MITx: 6.00.1x 计算机科学和Python编程导论 WEEK-01

***** WEEK-01 ***** Lecture1 introduction to computation : Algorithm are recipes. An algorithm is a conceptual(概念的) idea, a program is a concrete instantiation(实例化) of an algorithm. 现代计算机的雏形:Stored program computer Basic Primitives:   #操作系统用语范畴.是由若干条

MITx: 6.00.1x Alphabetical Substrings (python)

Assume s is a string of lower case characters. Write a program that prints the longest substring of s in which the letters occur in alphabetical order. For example, if s = 'azcbobobegghakl', then your program should print Longest substring in alphabe

chapter 3 introduction to computer science

主机文件: <chapter3.docx>

MITx 6.00 Problem Set 3 hangman游戏

全部代码参见Github:https://github.com/pxjw/MITx-6.00.1-2-Problem-Set/tree/master/6.00.1x/proSet3 HANGMAN PART 1: IS THE WORD GUESSED? Please read the Hangman Introduction before starting this problem. The helper functions you will be creating in the next t

MIT 6.00.1x学习心得

现在是大三上半学期,看了萧井陌的编程入门指南之后,用了大概一个月的时间终于把MIT 6.00.1x 课程学完了,有编程经验,但是因为本科是信息工程准备跨考计算机科学,而且不愿意当一个只会敲代码的码农,所以对于基础看的特别特别重要,所以才会很仔细的从计算机导论学起. 对于这门课,我觉得应该算是计算机导论里数一数二的课程. 谈一下收获: 1.培养出了计算机思想(Abstractions, algorithms, automated execution) 2.对于运行中的错误和异常的处理(try...

MTH5001: Introduction to Computer Programming

projectMarch 14, 20191 MTH5001: Introduction to Computer Programming 2018/191.1 Final Report Project: "Networks"1.1.1 Instructions:First, please type your name and student number into the Markdown cell below:Name:Student number:You must write yo