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中属性和描述符的正确使用
Aug 23 Python
Python简单定义与使用字典dict的方法示例
Jul 25 Python
Python冲顶大会 快来答题!
Jan 17 Python
对Python+opencv将图片生成视频的实例详解
Jan 08 Python
python实现随机漫步方法和原理
Jun 10 Python
使用Python检测文章抄袭及去重算法原理解析
Jun 14 Python
Pandas0.25来了千万别错过这10大好用的新功能
Aug 07 Python
Python分析最近大火的网剧《隐秘的角落》
Jul 02 Python
python定义类的简单用法
Jul 24 Python
python合并多个excel文件的示例
Sep 23 Python
Pycharm创建文件时自动生成文件头注释(自定义设置作者日期)
Nov 24 Python
python+opencv实现车道线检测
Feb 19 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 print类函数使用总结
2010/06/25 PHP
php实现水仙花数示例分享
2014/04/03 PHP
Linux下PHP连接Oracle数据库
2014/08/20 PHP
PHP实现PDO操作mysql存储过程示例
2019/02/13 PHP
php操作redis数据库常见方法实例总结
2020/02/20 PHP
浅析PHP echo 和 print 语句
2020/06/30 PHP
收集的10个免费的jQuery相册
2011/02/26 Javascript
js使浏览器窗口最大化实现代码(适用于IE)
2013/08/07 Javascript
jquery 隐藏与显示tr标签示例代码
2014/06/06 Javascript
同一个网页中实现多个JavaScript特效的方法
2015/02/02 Javascript
Javascript核心读书有感之表达式和运算符
2015/02/11 Javascript
jQuery过滤选择器用法示例
2016/09/12 Javascript
原生JS实现九宫格抽奖效果
2017/04/01 Javascript
详解Node.js access_token的获取、存储及更新
2017/06/20 Javascript
React进阶学习之组件的解耦之道
2017/08/07 Javascript
浅谈Vuex的状态管理(全家桶)
2017/11/04 Javascript
Vue中的v-for指令不起效果的解决方法
2018/09/27 Javascript
vue实现的双向数据绑定操作示例
2018/12/04 Javascript
JavaScript寄生组合式继承原理与用法分析
2019/01/11 Javascript
零基础之Node.js搭建API服务器的详解
2019/03/08 Javascript
Vue实现随机验证码功能
2020/12/29 Vue.js
给Python初学者的一些编程技巧
2015/04/03 Python
Python使用type关键字创建类步骤详解
2019/07/23 Python
Python如何使用字符打印照片
2020/01/03 Python
以SQLite和PySqlite为例来学习Python DB API
2020/02/05 Python
css3选择器基本介绍
2014/12/15 HTML / CSS
Puritan’s Pride(普丽普莱)官方网站:美国最大最全的保健品公司之一
2016/10/23 全球购物
一家专门经营包包的英国网站:MyBag
2019/09/08 全球购物
Python是如何进行类型转换的
2013/06/09 面试题
暑期实习鉴定
2013/12/16 职场文书
项目建议书格式
2014/03/12 职场文书
岗位说明书标准范本
2014/07/30 职场文书
小学生差生评语
2014/12/29 职场文书
煤矿安全生产管理协议书
2016/03/22 职场文书
确保减税降费落地生根,用实实在在措施
2019/07/19 职场文书
Java死锁的排查
2022/05/11 Java/Android