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的更多相关文章

  1. 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 ...

  2. MIT Introduction to Computer Science and Programming (Lesson one )

    MIT Introduction to Computer Science and Programming (Lesson one ) 这篇文是记载 MIT 计算机科学及编程导论 第一集 的笔记 Les ...

  3. Introduction to Computer Science and Programming in Python--MIT

    学习总结--(Introduction to Computer Science and Programming in Python--MIT) 导论 主题 重新利用数据结构来表达知识 理解算法的复杂性 ...

  4. 6.00.1x Introduction to computation

    6.00 Introduction to Computer Science                  and  Programming • Goal: –Become skillful at ...

  5. 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> ...

  6. 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> ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. php扩展开发1--添加函数

    目标:便携php扩展 要求实现 输出hello word 首先用的是php7.0.3   centos7.1或者centos6.+ 1.1 RPM安装PHP rpm -Uvh https://mirr ...

  2. XP下,移动窗口产生重影的问题

    最近做了一个东西,其中有一个小窗口需要跟着主窗口一起移动,结果发现在Xp系统上总是产生重影,需要刷新桌面才能消失. 移动窗口我使用的是MoveWindow,最后一个参数bRepaint传递的是FALS ...

  3. java容器 Map Set List

    容器:在java中,如果有一个类专门用来存放其他类的对象,这个类就叫做容器,或者叫集合,集合就是将若干性质相同或者相近的类的对象组合在一起而形成一个整体. boolean add(Object obj ...

  4. jQuery--左侧菜单收缩隐藏

    实现步骤: 步骤一. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 ...

  5. Luogu 4159 [SCOI2009]迷路

    BZOJ 1297 应当是简单题. 发现边权的数量很小,所以我们暴力把一个点拆成$9$个点,然后把$(x, i)$到$(x, i + 1)$连边,代表转移一次之后可以走回来:对于每一条存在的边$(i, ...

  6. JavaScript跨域调用基于JSON的RESTful API

    1. 基本术语 AJAX(Asynchronous JavaScript And XML,异步JavaScript和XML):AJAX是一种用于创建快速动态网页的技术,通过在后台与服务器进行少量数据交 ...

  7. initWithFrame 和 initWithCoder 区别?

    当我们所写的程序里用代码创建控制视图内容,需要调用initWithFrame去初始化 - (id)initWithFrame:(CGRect)frame { if (self =[superinitW ...

  8. attachEvent 与 addEventListener 的监听

    说到 attachEvent 与 addEventListener 的事件必然会提到  浏览器的判断,因为attachEvent只适用于于IE 先来看看常用的浏览器的判断 //判断浏览器类型 if(n ...

  9. linux 查看 PHP 的默认版本。

    命令 env env:显示当前用户的环境变量: which php

  10. [web] [vscode] 自定义语言缩进

    vscode 默认的html 语言的缩进有点过,貌似一个tab6个space, html看起来太空了,所幸的是可以自己调整单个语言的缩进模式. 方法如下 Preferences: Open User ...