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微信红包代码(php生成微信红包数组的方法)
- python四舍五入怎么用(python3 小数位的四舍五入用两种方法解决round 遇5不进)
- 怎么用python分析足球(使用Python进行体育竞技分析预测球队成绩)
- python3字符串怎么操作(Python3字符串encode与decode的讲解)
- python爬取数据总结(python3爬虫学习之数据存储txt的案例详解)
- 用python画圣诞树送给女朋友(情人节快乐! python绘制漂亮玫瑰花)
- python在txt指定行添加文本(Python修改文件往指定行插入内容的实例)
- ocr公式识别python(初探利用Python进行图文识别OCR)
- python3安装编程环境(Python3 修改默认环境的方法)
- python itchat库介绍(Python利用itchat库向好友或者公众号发消息的实例)
- 推荐算法第三方python(GitHub 热门:Python 算法大全,Star 超过 2 万)
- 学python可以应用到哪些方面(学习python可以干什么)
- python如何获取微信好友(利用Python查看微信共同好友功能的实现代码)
- python同步钉钉用户(python 调用钉钉机器人的方法)
- python数据存取(python异步存储数据详解)
- 日本菜有什么好吃(日本菜有什么好吃的做法)
- 韩国泡菜做法(韩国泡菜的做法步骤)
- 泰国旅游攻略(泰国旅游攻略必去景点)
- 越难春卷(越难春卷皮怎么用)
- 休闲VS新古典 办公家居简约设计(办公家居简约设计)
- 15个新成 园 位置公布 深圳龙岗2022年共建花园建设又有大动作(15个新成园位置公布)
热门推荐
- sql重复记录怎么删除(SQL去除重复记录七种)
- SQL中Exists的用法
- pythonexcel报表实例(对python生成业务报表的实例详解)
- js中什么是宏任务(浅谈JavaScript宏任务和微任务执行顺序)
- sql server作业的调度信息中各列的含义
- chrome适合开发的插件(Chrome插件扩展开发全攻略完整demo)
- linux内核设备树(解析Linux内核与设备树的编译和烧写)
- python撤回的微信消息怎么看(Python实现微信消息防撤回功能的实例代码)
- python常见知识点整理(Python基础知识点 初识Python.md)
- 从宝塔面板找网站会员数据(BT宝塔面板高并发大流量网站优化方案分享)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9