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多线程扫描端口示例
Jan 16 Python
django自定义Field实现一个字段存储以逗号分隔的字符串
Apr 27 Python
Python实现图像几何变换
Jul 06 Python
浅谈Series和DataFrame中的sort_index方法
Jun 07 Python
在python中实现对list求和及求积
Nov 14 Python
python 提取tuple类型值中json格式的key值方法
Dec 31 Python
检测python爬虫时是否代理ip伪装成功的方法
Jul 12 Python
Python如何爬取qq音乐歌词到本地
Jun 01 Python
pytorch SENet实现案例
Jun 24 Python
python 抓取知乎指定回答下视频的方法
Jul 09 Python
Python使用UDP实现720p视频传输的操作
Apr 24 Python
基于Pygame实现简单的贪吃蛇游戏
Dec 06 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
迅雷下载《中学科技》怀旧期刊下载
2021/02/27 无线电
如何在PHP中使用正则表达式进行查找替换
2013/06/13 PHP
CodeIgniter与PHP5.6的兼容问题
2015/07/16 PHP
php 字符串中是否包含指定字符串的多种方法
2018/04/12 PHP
PHP实现的无限分类类库定义与用法示例【基于thinkPHP】
2018/08/06 PHP
PHP实现微信提现(企业付款到零钱)
2019/08/01 PHP
Prototype1.5 rc2版指南最后一篇之Position
2007/01/10 Javascript
细品javascript 寻址,闭包,对象模型和相关问题
2009/04/27 Javascript
JavaScript 未结束的字符串常量常见解决方法
2010/01/24 Javascript
JS 使用for循环遍历子节点查找元素
2014/09/06 Javascript
安装使用Mongoose配合Node.js操作MongoDB的基础教程
2016/03/01 Javascript
JavaScript原生对象常用方法总结(推荐)
2016/05/13 Javascript
Bootstrap Table使用方法解析
2016/10/19 Javascript
jQuery实现选中行变色效果(实例讲解)
2017/07/06 jQuery
温故知新——JavaScript中的字符串连接问题最全总结(推荐)
2017/08/21 Javascript
使用JS和canvas实现gif动图的停止和播放代码
2017/09/01 Javascript
vue按需加载组件webpack require.ensure的方法
2017/12/13 Javascript
详解layui弹窗父子窗口之间传参数的方法
2018/01/16 Javascript
ES6入门教程之let、const的使用方法
2019/04/13 Javascript
Vue 动态添加路由及生成菜单的方法示例
2019/06/20 Javascript
详解微信小程序图片地扯转base64解决方案
2019/08/18 Javascript
js实现贪吃蛇小游戏
2019/10/29 Javascript
vue 获取及修改store.js里的公共变量实例
2019/11/06 Javascript
[49:27]2018DOTA2亚洲邀请赛 4.4 淘汰赛 TNC vs VG 第一场
2018/04/05 DOTA
Python的ORM框架SQLObject入门实例
2014/04/28 Python
python使用in操作符时元组和数组的区别分析
2015/05/19 Python
python 读取.csv文件数据到数组(矩阵)的实例讲解
2018/06/14 Python
Python数学形态学实例分析
2019/09/06 Python
python线程安全及多进程多线程实现方法详解
2019/09/27 Python
Python读入mnist二进制图像文件并显示实例
2020/04/24 Python
美国最受欢迎的度假目的地优惠套餐:BookVIP
2018/09/27 全球购物
adidas菲律宾官网:adidas PH
2020/02/07 全球购物
中文专业学生自我评价范文
2014/02/06 职场文书
2016年度继续教育学习心得体会
2016/01/19 职场文书
关于springboot 配置date字段返回时间戳的问题
2021/07/25 Java/Android
Python干货实战之八音符酱小游戏全过程详解
2021/10/24 Python