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运行的17个时新手常见错误小结
Aug 07 Python
python字典键值对的添加和遍历方法
Sep 11 Python
Python 编码Basic Auth使用方法简单实例
May 25 Python
Python实现的桶排序算法示例
Nov 29 Python
Python实现随机生成手机号及正则验证手机号的方法
Apr 25 Python
深入浅析Python传值与传址
Jul 10 Python
在Python中定义一个常量的方法
Nov 10 Python
Python一句代码实现找出所有水仙花数的方法
Nov 13 Python
基于wxPython的GUI实现输入对话框(1)
Feb 27 Python
python 实现list或string按指定分段
Dec 25 Python
使用python matploblib库绘制准确率,损失率折线图
Jun 16 Python
Python实现自动玩连连看的脚本分享
Apr 04 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/06/08 PHP
php分页思路以及在ZF中的使用
2012/05/30 PHP
如何让搜索引擎抓取AJAX内容解决方案
2014/08/25 PHP
在Windows XP下安装Apache+MySQL+PHP环境
2015/02/22 PHP
Prototype的Class.create函数解析
2011/09/22 Javascript
浅析js设置控件的readonly与enabled属性问题
2013/12/25 Javascript
js利用事件的阻止冒泡实现点击空白模态框的隐藏
2014/01/24 Javascript
$(document).ready(function() {})不执行初始化脚本
2014/06/19 Javascript
Jquery时间轴特效(三种不同类型)
2015/11/02 Javascript
jQuery Mobile操作HTML5的常用函数总结
2016/05/17 Javascript
AngularJS基础 ng-if 指令用法
2016/08/01 Javascript
Javascript中字符串replace方法的第二个参数探究
2016/12/05 Javascript
javascript数据结构之串的概念与用法分析
2017/04/12 Javascript
vue 实现全选全不选的示例代码
2018/03/29 Javascript
微信小程序实现自上而下字幕滚动
2018/07/14 Javascript
最简单的JS实现json转csv的方法
2019/01/10 Javascript
JQuery中的常用事件、对象属性与使用方法分析
2019/12/23 jQuery
Vue中component标签解决项目组件化操作
2020/09/04 Javascript
python正则表达式之作业计算器
2016/03/18 Python
python内置函数:lambda、map、filter简单介绍
2017/11/16 Python
Python编写Windows Service服务程序
2018/01/04 Python
Python datetime和unix时间戳之间相互转换的讲解
2019/04/01 Python
Django自定义用户表+自定义admin后台中的字段实例
2019/11/18 Python
解决Python Matplotlib绘图数据点位置错乱问题
2020/05/16 Python
如何查看浏览器对html5的支持情况
2020/12/15 HTML / CSS
轻金属冶金专业毕业生自荐信
2013/11/02 职场文书
客服部工作职责范本
2014/02/14 职场文书
禁毒宣传标语
2014/06/19 职场文书
法定代表人身份证明书
2014/09/10 职场文书
镇党政领导班子民主生活会思想汇报
2014/10/11 职场文书
环卫工作汇报材料
2014/10/28 职场文书
房产分割协议书范文
2014/11/21 职场文书
工作证明书
2015/06/15 职场文书
茶花女读书笔记
2015/06/29 职场文书
2016年七夕爱情寄语
2015/12/04 职场文书
使用javascript解析二维码的三种方式
2021/11/11 Javascript