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实现分析apache和nginx日志文件并输出访客ip列表的方法
Apr 04 Python
Python实现大文件排序的方法
Jul 10 Python
Python中MySQLdb和torndb模块对MySQL的断连问题处理
Nov 09 Python
Python全局变量用法实例分析
Jul 19 Python
关于python的list相关知识(推荐)
Aug 30 Python
python3爬取各类天气信息
Feb 24 Python
python实现的MySQL增删改查操作实例小结
Dec 19 Python
PyTorch中Tensor的拼接与拆分的实现
Aug 18 Python
Python matplotlib生成图片背景透明的示例代码
Aug 30 Python
python实现音乐播放器 python实现花框音乐盒子
Feb 25 Python
使用python修改文件并立即写回到原始位置操作(inplace读写)
Jun 28 Python
Python 删除List元素的三种方法remove、pop、del
Nov 16 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 Global变量定义当前页面的全局变量实现探讨
2013/06/05 PHP
如何用php生成扭曲及旋转的验证码图片
2013/06/07 PHP
php字符串替换函数substr_replace()用法实例
2015/03/17 PHP
php实现上传图片文件代码
2015/07/19 PHP
利用PHP计算有多少小于当前数字的数字方法示例
2020/08/26 PHP
JavaScript 在线压缩和格式化收藏
2009/01/16 Javascript
jQuery 添加/移除CSS类实现代码
2010/02/11 Javascript
jquery中获取select选中值的代码
2011/06/27 Javascript
使用jQuery UI的tooltip函数修饰title属性的气泡悬浮框
2013/06/24 Javascript
深入理解JavaScript系列(27):设计模式之建造者模式详解
2015/03/03 Javascript
如何使用HTML5地理位置定位功能
2015/04/27 Javascript
JavaScript中解析JSON数据的三种方法
2015/07/03 Javascript
AngularJS入门教程之REST和定制服务详解
2016/08/19 Javascript
JavaScript简单生成 N~M 之间随机数的方法
2017/01/13 Javascript
使用重写url机制实现验证码换一张功能
2017/08/01 Javascript
详解微信小程序的 request 封装示例
2018/08/21 Javascript
在 Angular-cli 中使用 simple-mock 实现前端开发 API Mock 接口数据模拟功能的方法
2018/11/28 Javascript
如何用RxJS实现Redux Form
2018/12/29 Javascript
Vue组件系列开发之模态框
2019/04/18 Javascript
浅入深出Vue之组件使用
2019/07/11 Javascript
Vue父组件向子组件传值以及data和props的区别详解
2020/03/02 Javascript
Vue 简单实现前端权限控制的示例
2020/12/25 Vue.js
[01:12]DOTA2次级职业联赛 - Newbee.Y 战队宣传片
2014/12/01 DOTA
跟老齐学Python之dict()的操作方法
2014/09/24 Python
利用Python绘制数据的瀑布图的教程
2015/04/07 Python
详解Django缓存处理中Vary头部的使用
2015/07/24 Python
Python动刷新抢12306火车票的代码(附源码)
2018/01/24 Python
Python Selenium 之关闭窗口close与quit的方法
2019/02/13 Python
Python高并发和多线程有什么关系
2020/11/14 Python
英国儿童图书网站:Scholastic
2017/03/26 全球购物
会计工作心得体会
2014/01/13 职场文书
志愿者活动总结报告
2014/06/27 职场文书
2014年学生会个人工作总结
2014/11/07 职场文书
2014全年工作总结
2014/11/27 职场文书
男人帮观后感
2015/06/18 职场文书
Redis如何实现分布式锁
2021/08/23 Redis