Python实现简单查找最长子串功能示例


Posted in Python onFebruary 26, 2019

本文实例讲述了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 print

Longest 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程序设计有所帮助。

Python 相关文章推荐
Python3.x和Python2.x的区别介绍
Feb 12 Python
解析Mac OS下部署Pyhton的Django框架项目的过程
May 03 Python
node.js获取参数的常用方法(总结)
May 29 Python
Python正则表达式分组概念与用法详解
Jun 24 Python
python监控键盘输入实例代码
Feb 09 Python
Python2.7 实现引入自己写的类方法
Apr 29 Python
Python之两种模式的生产者消费者模型详解
Oct 26 Python
django框架实现一次性上传多个文件功能示例【批量上传】
Jun 19 Python
18个Python脚本可加速你的编码速度(提示和技巧)
Oct 17 Python
浅谈keras2 predict和fit_generator的坑
Jun 17 Python
python爬虫scrapy基于CrawlSpider类的全站数据爬取示例解析
Feb 20 Python
Python安装使用Scrapy框架
Apr 12 Python
基于Python实现用户管理系统
Feb 26 #Python
python selenium firefox使用详解
Feb 26 #Python
Django实现学员管理系统
Feb 26 #Python
Python实现读取txt文件中的数据并绘制出图形操作示例
Feb 26 #Python
Django实现学生管理系统
Feb 26 #Python
python爬取微信公众号文章的方法
Feb 26 #Python
python下载微信公众号相关文章
Feb 26 #Python
You might like
PHP无敌近乎加密方式!
2010/07/17 PHP
php中$美元符号与Zen Coding冲突问题解决方法分享
2014/05/28 PHP
php去掉URL网址中带有PHPSESSID的配置方法
2014/07/08 PHP
PHP实现自动识别Restful API的返回内容类型
2015/02/07 PHP
详解WordPress中添加和执行动作的函数使用方法
2015/12/29 PHP
Symfony2中被遗弃的getRequest()方法分析
2016/03/17 PHP
PHP实现自动识别原编码并对字符串进行编码转换的方法
2016/07/13 PHP
mac os快速切换多个PHP版本的方法
2017/03/07 PHP
JavaScript中数组对象的那些自带方法介绍
2013/03/12 Javascript
通过javascript把图片转化为字符画
2013/10/24 Javascript
关于js数组去重的问题小结
2014/01/24 Javascript
JQuery动画与特效实例分析
2015/02/02 Javascript
js中遍历Map对象的方法
2016/07/27 Javascript
AngularJS 执行流程详细介绍
2016/08/18 Javascript
AngularJs 最新验证手机号码的实例,成功测试通过
2017/11/26 Javascript
[01:08]DOTA2“血战之命”预告片
2017/08/12 DOTA
Python程序设计入门(1)基本语法简介
2014/06/13 Python
《Python之禅》中对于Python编程过程中的一些建议
2015/04/03 Python
Python中join和split用法实例
2015/04/14 Python
python字符串对其居中显示的方法
2015/07/11 Python
Python中异常重试的解决方案详解
2017/05/05 Python
关于反爬虫的一些简单总结
2017/12/13 Python
python tornado微信开发入门代码
2018/08/24 Python
python使用递归的方式建立二叉树
2019/07/03 Python
Python依赖包整体迁移方法详解
2019/08/15 Python
Pycharm+django2.2+python3.6+MySQL实现简单的考试报名系统
2019/09/05 Python
python编程进阶之类和对象用法实例分析
2020/02/21 Python
python中数据库like模糊查询方式
2020/03/02 Python
如何理解python面向对象编程
2020/06/01 Python
如何利用python检测图片是否包含二维码
2020/10/15 Python
传播学专业毕业生自荐信
2013/11/04 职场文书
信息管理员岗位职责
2013/12/01 职场文书
出纳员的岗位职责
2014/02/22 职场文书
党课心得体会范文
2014/09/09 职场文书
个人委托书如何写
2014/09/25 职场文书
幼儿园六一主持词开场白
2015/05/28 职场文书