Python实现简单查找最长子串功能示例

所属分类: 脚本专栏 / python 阅读数: 1344
收藏 0 赞 0 分享

本文实例讲述了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列表(list)操作技巧总结》、《Python编码操作技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程

希望本文所述对大家Python程序设计有所帮助。

更多精彩内容其他人还在看

python进行TCP端口扫描的实现

这篇文章主要介绍了python进行TCP端口扫描的实现,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Python简单获取二维数组行列数的方法示例

这篇文章主要介绍了Python简单获取二维数组行列数的方法,结合实例形式分析了Python基于numpy模块的二维数组相关运算技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Python实现的字典排序操作示例【按键名key与键值value排序】

这篇文章主要介绍了Python实现的字典排序操作,结合实例形式分析了Python针对字典分别按照键名key与键值value进行排序的相关操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Python类装饰器实现方法详解

这篇文章主要介绍了Python类装饰器实现方法,结合实例形式较为详细的分析了Python类装饰器的相关概念、原理、实现方法与使用技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

使用python对文件中的单词进行提取的方法示例

这篇文章主要介绍了使用python对文件中的单词进行提取的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Python函数装饰器实现方法详解

这篇文章主要介绍了Python函数装饰器实现方法,结合实例形式较为详细的分析了Python函数装饰器的概念、功能、用法及相关操作注意事项,需要的朋友可以参考下
收藏 0 赞 0 分享

python 删除字符串中连续多个空格并保留一个的方法

今天小编就为大家分享一篇python 删除字符串中连续多个空格并保留一个的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

python 文本单词提取和词频统计的实例

今天小编就为大家分享一篇python 文本单词提取和词频统计的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Python装饰器基础概念与用法详解

这篇文章主要介绍了Python装饰器基础概念与用法,结合实例形式详细分析了Python装饰器的概念、功能、用法及相关操作注意事项,需要的朋友可以参考下
收藏 0 赞 0 分享

对python 读取线的shp文件实例详解

今天小编就为大家分享一篇对python 读取线的shp文件实例详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多