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检测远程服务器tcp端口的方法
Mar 14 Python
在Python3中初学者应会的一些基本的提升效率的小技巧
Mar 31 Python
python脚本实现xls(xlsx)转成csv
Apr 10 Python
python实现随机森林random forest的原理及方法
Dec 21 Python
单链表反转python实现代码示例
Feb 08 Python
在PyCharm下使用 ipython 交互式编程的方法
Jan 17 Python
不到20行代码用Python做一个智能聊天机器人
Apr 19 Python
python获取地震信息 微信实时推送
Jun 18 Python
python对象销毁实例(垃圾回收)
Jan 16 Python
基于plt.title无法显示中文的快速解决
May 16 Python
golang/python实现归并排序实例代码
Aug 30 Python
从np.random.normal()到正态分布的拟合操作
Jun 02 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带公钥加密类分享(每次加密结果都不一样哦)
2014/08/20 PHP
PHP获取一年有几周以及每周开始日期和结束日期
2015/08/06 PHP
PHP中strtr与str_replace函数运行性能简单测试示例
2019/06/22 PHP
通过实例解析PHP数据类型转换方法
2020/07/11 PHP
js类中获取外部函数名的方法
2007/08/19 Javascript
3种Jquery限制文本框只能输入数字字母的方法
2014/12/03 Javascript
js文本框走动跑马灯效果代码分享
2015/08/25 Javascript
js实现贪吃蛇小游戏(容易理解)
2017/01/22 Javascript
JS实现根据密码长度显示安全条功能
2017/03/08 Javascript
JS实现移动端判断上拉和下滑功能
2017/08/07 Javascript
thinkjs 文件上传功能实例代码
2017/11/08 Javascript
基于nodejs res.end和res.send的区别
2018/05/14 NodeJs
Javascript中弹窗confirm与prompt的区别
2018/10/26 Javascript
JavaScript模板引擎应用场景及实现原理详解
2018/12/14 Javascript
微信JS-SDK updateAppMessageShareData安卓不能自定义分享详解
2019/03/29 Javascript
layui实现显示数据表格、搜索和修改功能示例
2020/06/03 Javascript
手写Vue2.0 数据劫持的示例
2021/03/04 Vue.js
[01:06:25]Secret vs Liquid 2018国际邀请赛淘汰赛BO3 第一场 8.25
2018/08/29 DOTA
python中的实例方法、静态方法、类方法、类变量和实例变量浅析
2014/04/26 Python
Python中对列表排序实例
2015/01/04 Python
Python itertools模块详解
2015/05/09 Python
python 函数传参之传值还是传引用的分析
2017/09/07 Python
django实现登录时候输入密码错误5次锁定用户十分钟
2017/11/05 Python
python调用函数、类和文件操作简单实例总结
2019/11/29 Python
Python 通过正则表达式快速获取电影的下载地址
2020/08/17 Python
python IP地址转整数
2020/11/20 Python
GNC健安喜官方海外旗舰店:美国著名保健品牌
2017/01/04 全球购物
数据库的约束含义
2012/09/09 面试题
应征英语教师求职信
2013/11/27 职场文书
考试没考好检讨书
2014/01/31 职场文书
暑期培训随笔感言
2014/03/10 职场文书
乡镇食品安全责任书
2014/07/28 职场文书
四风个人对照检查材料思想汇报(办公室通用版)
2014/10/07 职场文书
2014年客服工作总结范文
2014/11/13 职场文书
房屋买卖定金协议书
2016/03/21 职场文书
JVM上高性能数据格式库包Apache Arrow入门和架构详解(Gkatziouras)
2021/05/26 Servers