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 相关文章推荐
python常用web框架简单性能测试结果分享(包含django、flask、bottle、tornado)
Aug 25 Python
Python实现国外赌场热门游戏Craps(双骰子)
Mar 31 Python
基于tensorflow加载部分层的方法
Jul 26 Python
在IPython中执行Python程序文件的示例
Nov 01 Python
pymongo中聚合查询的使用方法
Mar 22 Python
Python 数据可视化pyecharts的使用详解
Jun 26 Python
Python爬虫爬取煎蛋网图片代码实例
Dec 16 Python
Ubuntu16.04安装python3.6.5步骤详解
Jan 10 Python
用python解压分析jar包实例
Jan 16 Python
Python requests.post方法中data与json参数区别详解
Apr 30 Python
Kears 使用:通过回调函数保存最佳准确率下的模型操作
Jun 17 Python
python利用while求100内的整数和方式
Nov 07 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&&mysql)六
2006/10/09 PHP
用PHPdig打造属于你自己的Google[图文教程]
2007/02/14 PHP
php对数组排序的简单实例
2013/12/25 PHP
php模仿qq空间或朋友圈发布动态、评论动态、回复评论、删除动态或评论的功能(中)
2017/06/11 PHP
解决thinkPHP 5 nginx 部署时,只跳转首页的问题
2019/10/16 PHP
php5.3/5.4/5.5/5.6/7常见新增特性汇总整理
2020/02/27 PHP
Jquery 弹出层插件实现代码
2009/10/24 Javascript
JavaScript DOM 学习第二章 编辑文本
2010/02/19 Javascript
js 弹出框只弹一次(二次修改之后的)
2013/11/26 Javascript
JS判断表单输入是否为空(示例代码)
2013/12/23 Javascript
简述Jquery与DOM对象
2015/07/10 Javascript
js实现仿qq消息的弹出窗效果
2016/01/06 Javascript
jQuery简单操作cookie的插件实例
2016/01/13 Javascript
JavaScript实现Base64编码转换
2016/04/23 Javascript
JS实现购物车特效
2017/02/02 Javascript
canvas压缩图片转换成base64格式输出文件流
2017/03/09 Javascript
使用ajax的post同步执行(实现方法)
2017/12/21 Javascript
从源码里了解vue中的nextTick的使用
2018/11/22 Javascript
微信小程序上传多图到服务器并获取返回的路径
2019/05/05 Javascript
[03:17]史诗级大片应援2018DOTA2国际邀请赛 致敬每一位坚守遗迹的勇士
2018/07/20 DOTA
python3+PyQt5 自定义窗口部件--使用窗口部件样式表的方法
2019/06/26 Python
Python实现ATM系统
2020/02/17 Python
django 模型中的计算字段实例
2020/05/19 Python
python 制作磁力搜索工具
2021/03/04 Python
详解CSS3中强大的filter(滤镜)属性
2017/06/29 HTML / CSS
英国最大的在线蜡烛商店:Candles Direct
2019/03/26 全球购物
泰国时尚电商:POMELO Fashion
2020/03/11 全球购物
英语系本科生个人求职信
2013/09/21 职场文书
拓展训练激励口号
2014/06/17 职场文书
慰问信模板
2015/02/14 职场文书
小学教师教学随笔
2015/08/14 职场文书
2016会计专业自荐信范文
2016/01/28 职场文书
2019学生会干事辞职信
2019/06/27 职场文书
MySQL安装后默认自带数据库的作用详解
2021/04/27 MySQL
Opencv中cv2.floodFill算法的使用
2021/06/18 Python
CSS变量实现主题切换的方法
2021/06/23 HTML / CSS