python找到连续不重复最长英文串(Python实现简单查找最长子串功能示例)
python找到连续不重复最长英文串
Python实现简单查找最长子串功能示例本文实例讲述了Python实现简单查找最长子串功能。分享给大家供大家参考,具体如下:
题目选自edX公开课 MITx: 6.00.1x Introduction to Computer Science and Programming 课程 Week2 的Problem Set 1的第三题。下面是原题内容。
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, ifs = '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 printLongest substring in alphabetical order is: abc
For problems such as these, do not include raw_input statements or define the variable s in any way. Our automated testing will provide a value of s for you - so the code you submit in the following box should assume s is already defined. If you are confused by this instruction, please review L4 Problems 10 and 11 before you begin this problem set.
代码如下:
|
# -*- coding:utf-8 -*- #! python2 #判断一个字符串内的字母是否是按字母表顺序 # 如IsStrIncre('abbcdg') 返回 True # IsStrIncre('abbadg') 返回 False # 如果只有一个字符,也返回False def IsStrIncre(s): for cnt in range ( len (s) - 1 ): if len (s) = = 1 : return False elif s[cnt] > s[cnt + 1 ]: return False return True s = 'abajsiesnwdw' # example code substr = '' for length in range ( 1 , len (s) + 1 ): firstflag = True # a flag to remember the first string that satisfied the requirements # and ignore the strings satisfied the requirements but appeared after for cnt in range ( len (s) - length + 1 ): if IsStrIncre(s[cnt: cnt + length]): if firstflag: substr = s[cnt: cnt + length] firstflag = False print 'Longest substring in alphabetical order is: ' + substr |
运行结果:
Longest substring in alphabetical order is: ajs
希望本文所述对大家Python程序设计有所帮助。
原文链接:https://blog.csdn.net/dazuo01/article/details/20283619
- python教程列表排序(Python一行代码实现快速排序的方法)
- python中的eval函数的用法(Python eval的常见错误封装及利用原理详解)
- python爬微信好友(python抓取需要扫微信登陆页面)
- python循环创建字典(Python字典的基本用法实例分析创建、增加、获取、修改、删除)
- python的几种数据结构(python中的数据结构比较)
- pythonselenium判断网页加载完成(python 实现selenium断言和验证的方法)
- anaconda如何配置python3.7(WIn10+Anaconda环境下安装PyTorch避坑指南)
- python字典的key(Python使用dict.fromkeys快速生成一个字典示例)
- pythonrequest包设置编码(解决python3中的requests解析中文页面出现乱码问题)
- python的4种数字变量(Python将字符串常量转化为变量方法总结)
- python创建进程的方法(Python多进程fork函数详解)
- python的模块与包与库(Python 中包/模块的 `import` 操作代码)
- python正则表达式入门(Python正则表达式实现简易计算器功能示例)
- python下划线怎么用(Python3中_下划线和__双下划线的用途和区别)
- python中查看数据类型的内置函数(python学习——内置函数、数据结构、标准库的技巧推荐)
- python怎么用pandas读取两列数据(python 利用pandas将arff文件转csv文件的方法)
- 怎么快速学好英语(怎么快速学好英语初中)
- 中国留学生都是富二代吗()
- 我们现在吃的苹果是哪里来的 原来现代苹果引入中国仅有一百多年(我们现在吃的苹果是哪里来的)
- 买绿宝不能只挑黄绿色 菜农教你3招挑,个个皮薄肉脆,香甜爆汁(买绿宝不能只挑黄绿色)
- 大果肉搭配薄瓜皮, 绿宝 脆甜爽口,不愧是甜瓜中的 佼佼者(大果肉搭配薄瓜皮)
- 河南尉氏县因地制宜发展果蔬种植 水坡镇绿宝甜瓜变 金瓜(河南尉氏县因地制宜发展果蔬种植)
热门推荐
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9