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牛刀小试密码爆破
Feb 03 Python
在Python中移动目录结构的方法
Jan 31 Python
Python使用poplib模块和smtplib模块收发电子邮件的教程
Jul 02 Python
Python IDLE 错误:IDLE''s subprocess didn''t make connection 的解决方案
Feb 13 Python
学生信息管理系统python版
Oct 17 Python
对Pandas DataFrame缺失值的查找与填充示例讲解
Nov 06 Python
详解Python 定时框架 Apscheduler原理及安装过程
Jun 14 Python
PyQt5笔记之弹出窗口大全
Jun 20 Python
Pytorch 实现sobel算子的卷积操作详解
Jan 10 Python
python nohup 实现远程运行不宕机操作
Apr 16 Python
python是怎么被发明的
Jun 15 Python
Python基于Twilio及腾讯云实现国际国内短信接口
Jun 18 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
UTF8编码内的繁简转换的PHP类
2009/07/09 PHP
PHP中include()与require()的区别说明
2010/03/10 PHP
php文件下载处理方法分析
2015/04/22 PHP
PHP多维数组排序array详解
2017/11/21 PHP
PHP实现的猴王算法(猴子选大王)示例
2018/04/30 PHP
用jquery实现下拉菜单效果的代码
2010/07/25 Javascript
js制作简易年历完整实例
2015/01/28 Javascript
JS控制表单提交的方法
2015/07/09 Javascript
jQuery原生的动画效果
2015/07/10 Javascript
jQuery Mobile开发中日期插件Mobiscroll使用说明
2016/03/02 Javascript
JavaScript的函数式编程基础指南
2016/03/19 Javascript
老生常谈javascript变量的命名规范和注释
2016/09/29 Javascript
Bootstrap 填充Json数据的实例代码
2017/01/11 Javascript
ES6新特性八:async函数用法实例详解
2017/04/21 Javascript
如何解决React官方脚手架不支持Less的问题(小结)
2018/09/12 Javascript
详解javascript replace高级用法
2019/02/17 Javascript
微信小程序连续签到7天积分获得功能的示例代码
2020/08/20 Javascript
python实现一次创建多级目录的方法
2015/05/15 Python
Django中传递参数到URLconf的视图函数中的方法
2015/07/18 Python
Python网络爬虫实例讲解
2016/04/28 Python
python中for循环输出列表索引与对应的值方法
2018/11/07 Python
Python基于opencv调用摄像头获取个人图片的实现方法
2019/02/21 Python
python装饰器原理与用法深入详解
2019/12/19 Python
什么是python的列表推导式
2020/05/26 Python
利用PyQt5+Matplotlib 绘制静态/动态图的实现代码
2020/07/13 Python
罗德与泰勒百货官网:Lord & Taylor
2016/08/12 全球购物
德国足球商店:OUTFITTER
2019/05/06 全球购物
高中毕业生的个人自我评价
2014/02/21 职场文书
开业主持词
2014/03/21 职场文书
大学生英语演讲稿
2014/04/24 职场文书
幼师求职自荐信
2014/05/31 职场文书
机械设计制造及其自动化专业求职信
2014/06/17 职场文书
2015年检察院个人工作总结
2015/05/20 职场文书
谢师宴学生致辞
2015/07/27 职场文书
心理学培训心得体会
2016/01/22 职场文书
2016年大学生暑期社会实践活动总结
2016/04/06 职场文书