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:后面不加空格
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 cha ...
- MIT Introduction to Computer Science and Programming (Lesson one )
MIT Introduction to Computer Science and Programming (Lesson one ) 这篇文是记载 MIT 计算机科学及编程导论 第一集 的笔记 Les ...
- Introduction to Computer Science and Programming in Python--MIT
学习总结--(Introduction to Computer Science and Programming in Python--MIT) 导论 主题 重新利用数据结构来表达知识 理解算法的复杂性 ...
- 6.00.1x Introduction to computation
6.00 Introduction to Computer Science and Programming • Goal: –Become skillful at ...
- Note 2 for <Pratical Programming : An Introduction to Computer Science Using Python 3>
Book Imformation : <Pratical Programming : An Introduction to Computer Science Using Python 3> ...
- Note 1 for <Pratical Programming : An Introduction to Computer Science Using Python 3>
Book Imformation : <Pratical Programming : An Introduction to Computer Science Using Python 3> ...
- Discovering the Computer Science Behind Postgres Indexes
This is the last in a series of Postgres posts that Pat Shaughnessy wrote based on his presentation ...
- Introduction to ASP.NET Web Programming Using the Razor Syntax (C#)
1, http://www.asp.net/web-pages/overview/getting-started/introducing-razor-syntax-c 2, Introduction ...
- Mathematics for Computer Science (Eric Lehman / F Thomson Leighton / Albert R Meyer 著)
I Proofs1 What is a Proof?2 The Well Ordering Principle3 Logical Formulas4 Mathematical Data Types5 ...
随机推荐
- 246. Strobogrammatic Number 上下对称的数字
[抄题]: A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at u ...
- InvocationtargetException 类型转换异常
日期类型转换不了json格式数据 json转换数据的时候可以设置某个字段不需要转换 jsonconfig=new JsonConfig() //{} 内传入不需要转换的字段 jsonconfig.se ...
- SparkR 读取数据& Spark运行的配置
1.本地LOCAL环境安装Spark并试运行配置(在Ubuntu系统下例子) # 打开文件配置环境变量: JAVA,SCALA,SPARK,HADOOP,SBT gedit /etc/profile ...
- Django框架 之 form组件
Django框架 之 form组件 浏览目录 Form介绍 普通的登录 使用form组件 Form详情 常用字段 校验 进阶 使用Django Form流程 一.Form介绍 我们之前在HTML页面中 ...
- Linux下面rpm命令和mount命令详解
在Linux下面我们经常会安装一些软件包,还有挂载命令.接下来,我们通过一些实例来演示这些命令的使用.. 第一步:我们先在linux下面挂载光盘,先进入到根目录,然后切换到根下面的/mnt目录,因为/ ...
- On the nightmare that is JSON Dates. Plus, JSON.NET and ASP.NET Web API
Ints are easy. Strings are mostly easy. Dates? A nightmare. They always will be. There's different c ...
- cgroup初步分析(1)
cgroup的功能和作用不废话,直说一下cgroup的几条设计准则,有了几条设计准则的约束,就比较容易理解其中的数据结构和函数,至于源代码cgroup.c,无非是两个内容,一是task_struct. ...
- POJ3295 Tautology(栈+枚举)
Description WFF 'N PROOF is a logic game played with dice. Each die has six faces representing some ...
- 图的遍历——DFS
原创 图的遍历有DFS和BFS两种,现选用DFS遍历图. 存储图用邻接矩阵,图有v个顶点,e条边,邻接矩阵就是一个VxV的矩阵: 若顶点1和顶点5之间有连线,则矩阵元素[1,5]置1,若是无向图[5, ...
- MongoDB整理笔记の管理Sharding
1.列出所有的Shard Server > db.runCommand({ listshards: 1 }) --列出所有的Shard Server { "shards" : ...