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:后面不加空格

最新文章

  1. ma60笔记
  2. bzoj2219: 数论之神
  3. 详解;(function ($,window,document,undefined){...})(jQuery,window,document);
  4. ABAP DEMO
  5. Window下开发React-Native Android步骤
  6. margin设置为负数
  7. Java内存模型四
  8. python中math模块常用的方法整理
  9. Gradle 1.12用户指南翻译——第二十七章. Ear 插件
  10. 安装Java语言的jdk,配置java环境变量
  11. url参数 加密
  12. PAT 1067 试密码(20)(代码)
  13. 【C#】datetimepicker里面如何设置日期为当天日期,而时间设为0:00或23:59?
  14. Python学习笔记011——内置函数eval()
  15. Django Pagination
  16. Stalstack 连接管理配置
  17. POJ1020(小正方形铺大正方形)
  18. 开发常用宏 - iOS
  19. display:table-cell的min-height
  20. xml 操作

热门文章

  1. XP下,移动窗口产生重影的问题
  2. Process management of windows
  3. (函数分治法)实现pow函数(x的y次方幂)
  4. LibreOJ 6283 数列分块入门 7(区间加区间乘区间求和)
  5. 全排列——DFS实现
  6. [转]-webkit-overflow-scrolling:touch的应用
  7. 【转】开源视频录制库LandscapeVideoCamera
  8. 使用CodeMaid自动程序排版[转]
  9. 【SQL】- 基础知识梳理(一) - 数据库
  10. c# as与is的区别