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 命令行参数sys.argv
Sep 06 Python
python操作MongoDB基础知识
Nov 01 Python
Python实现去除代码前行号的方法
Mar 10 Python
使用PDB简单调试Python程序简明指南
Apr 25 Python
python字符串连接方法分析
Apr 12 Python
python 接口返回的json字符串实例
Mar 27 Python
对python 操作solr索引数据的实例详解
Dec 07 Python
python将txt等文件中的数据读为numpy数组的方法
Dec 22 Python
python简单鼠标自动点击某区域的实例
Jun 25 Python
django有哪些好处和优点
Sep 01 Python
Python pathlib模块使用方法及实例解析
Oct 05 Python
python数字图像处理数据类型及颜色空间转换
Jun 28 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 array_push()数组函数:将一个或多个单元压入数组的末尾(入栈)
2011/07/12 PHP
php5.3中连接sqlserver2000的两种方法(com与ODBC)
2012/12/29 PHP
php 判断服务器操作系统的类型
2014/02/17 PHP
dojo随手记 gird组件引用
2011/02/24 Javascript
jQuery插件Validate实现自定义表单验证
2016/01/18 Javascript
jQuery判断是否存在滚动条的简单方法
2016/09/17 Javascript
Nodejs实现短信验证码功能
2017/02/09 NodeJs
详解nodejs操作mongodb数据库封装DB类
2017/04/10 NodeJs
基于AngularJS的拖拽文件上传的实例代码
2017/07/15 Javascript
Vue + Vue-router 同名路由切换数据不更新的方法
2017/11/20 Javascript
对node.js中render和send的用法详解
2018/05/14 Javascript
Bootstrap实现省市区三级联动(亲测可用)
2019/07/26 Javascript
React中Ref 的使用方法详解
2020/04/28 Javascript
Python实现删除文件但保留指定文件
2015/06/21 Python
Python批量创建迅雷任务及创建多个文件
2016/02/13 Python
python操作excel文件并输出txt文件的实例
2018/07/10 Python
Python实现手写一个类似django的web框架示例
2018/07/20 Python
python 文本单词提取和词频统计的实例
2018/12/22 Python
Python中出现IndentationError:unindent does not match any outer indentation level错误的解决方法
2020/04/18 Python
使用Python在Windows下获取USB PID&VID的方法
2019/07/02 Python
详解将Python程序(.py)转换为Windows可执行文件(.exe)
2019/07/19 Python
基于python及pytorch中乘法的使用详解
2019/12/27 Python
PyQt5 文本输入框自动补全QLineEdit的实现示例
2020/05/13 Python
Python while true实现爬虫定时任务
2020/06/08 Python
详细分析Python可变对象和不可变对象
2020/07/09 Python
Python实现迪杰斯特拉算法并生成最短路径的示例代码
2020/12/01 Python
HTML5本地存储之IndexedDB
2017/06/16 HTML / CSS
英国时尚泳装品牌:Maru Swimwear
2019/10/06 全球购物
Java 中访问数据库的步骤?Statement 和PreparedStatement 之间的区别?
2012/06/05 面试题
通信工程专业女生个人求职信
2013/09/21 职场文书
自我评价的写作规则
2014/01/06 职场文书
酒店值班经理的工作职责范本
2014/02/18 职场文书
学生请假条
2014/04/11 职场文书
人大代表选举标语
2014/10/07 职场文书
房贷收入证明范本
2015/06/12 职场文书
JavaGUI模仿QQ聊天功能完整版
2021/07/04 Java/Android