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

 更新时间:2019年02月26日 11:01:26   作者:dazuo_01  
这篇文章主要介绍了Python实现简单查找最长子串功能,涉及字符串遍历、统计等相关操作技巧,需要的朋友可以参考下

本文实例讲述了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程序设计有所帮助。

相关文章

  • django和flask哪个值得研究学习

    django和flask哪个值得研究学习

    在本篇文章里小编给大家整理的是一篇关于django和flask哪个值得研究学习内容,需要的朋友们可以参考下。
    2020-07-07
  • Python实现的多线程端口扫描工具分享

    Python实现的多线程端口扫描工具分享

    这篇文章主要介绍了Python实现的多线程端口扫描工具分享,工具实现了扫单IP和扫IP段功能,本文给出运行效果和实现源码,需要的朋友可以参考下
    2015-01-01
  • Python基础之with语句和上下文管理器详解

    Python基础之with语句和上下文管理器详解

    这篇文章主要为大家详细介绍了Python中with语句和上下文管理器的具体使用方法,文中的示例代码讲解详细,感兴趣的小伙伴可以了解一下
    2023-04-04
  • 初学python数组的处理代码

    初学python数组的处理代码

    初学python数组的处理代码,学习python的朋友可以参考下。
    2011-01-01
  • windowns使用PySpark环境配置和基本操作

    windowns使用PySpark环境配置和基本操作

    pyspark是Spark对Python的api接口,可以在Python环境中通过调用pyspark模块来操作spark,这篇文章主要介绍了windowns使用PySpark环境配置和基本操作,感兴趣的可以了解一下
    2021-05-05
  • Tensorflow加载Vgg预训练模型操作

    Tensorflow加载Vgg预训练模型操作

    这篇文章主要介绍了Tensorflow加载Vgg预训练模型操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-05-05
  • 基于python3的socket聊天编程

    基于python3的socket聊天编程

    这篇文章主要为大家详细介绍了基于python3的socket聊天编程,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-02-02
  • Django获取应用下的所有models的例子

    Django获取应用下的所有models的例子

    今天小编就为大家分享一篇Django获取应用下的所有models的例子,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-08-08
  • python加载自定义词典实例

    python加载自定义词典实例

    今天小编就为大家分享一篇python加载自定义词典实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-12-12
  • Flask  response 对象详情

    Flask  response 对象详情

    在 Flask 中,响应使用 Response 对象表示,响应报文中的大部分内容由服务器处理,一般情况下,我们只负责返回主体内容即可。在之前的文章中,我们了解到 Flask 会先匹配请求 url 的路由,调用对应的视图函数,视图函数的返回值构成了响应报文的主体内容。
    2021-11-11

最新评论