Python实现简单查找最长子串功能示例
作者:dazuo_01 发布时间:2023-01-14 01:20:33
本文实例讲述了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 printLongest 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程序设计有所帮助。
来源:https://blog.csdn.net/dazuo01/article/details/20283619


猜你喜欢
- 根据菜鸟教程Django教程学习,运行"python manage.py migrate" 报错,出现django.db
- 前言:由程序去执行的操作不允许有任何误差,有些时候在测试的时候未出现问题,但是放到服务器上就会报错,而且打印的错误信息并不十分明确。这时,我
- 苦逼的前夜昨晚很辛苦,搞到晚上快两点,最后还是没有把python3下的scrapy框架安装起来,后面还把yum这玩意给弄坏了,一直找不到命令
- moviepy官网:https://pypi.org/project/moviepy/是一个用于视频编辑的Python库:切割、连接、标题插
- 一个Javascript 的类库,用于table内容排序。使用很方便,不用每次都去调用数据库了。特别适合多表查询的排序。加上<tbod
- keras根据层名称来初始化网络def get_model(input_shape1=[75, 75, 3], input_shape2=[
- 一、读写excel数据利用pandas可以很方便的读写excel数据1.1 读:data_in = pd.read_excel('M
- 1、类变量、实例变量概念类变量:类变量就是定义在类中,但是在函数体之外的变量。通常不使用self.变量名赋值的变量。类变量通常不作为类的实例
- 最近 W3C 一口气推出 7 个 HTML 工作草案,涵盖了 HTML5,HTML RDF,HTML Microdata,HTM
- MaxDB是MySQL AB公司通过SAP认证的数据库。MaxDB数据库服务器补充了MySQL AB产品系列。某些MaxDB特性在MySQL
- Python PyTorch深度学习框架PyTorch是一个基于Python的深度学习框架,它支持使用CPU和GPU进行高效的神经网络训练。
- 作为主题的制作者, 除了实现功能, 展示界面, 还有责任使主题灵活多变, 以满足更多人不同的需求.可能一些朋友曾为选用双栏主题 (单侧边栏)
- 业务场景使用elasticsearch作为全文搜索引擎,对标题、内容等,实现智能搜索、输入提示、拼音搜索等elasticsearch索引与数
- 1 环境准备192.168.1.34 服务器A(主机)192.168.1.35 服务器B(从机)2 安装mysqlMySQL版本:这里采用S
- 网页过渡是指当浏览者进入或离开网页时,页面呈现的不同的刷新效果,比如卷动、百叶窗等。注:通过模板所建网页无法添加网页过渡效果!制作步骤:1、
- 在广大网友心目中,他们就是中国互联网搜索领域的三驾马车。无论这三家搜索巨头承不承认,在网友眼中总会来将他们进行对比比较。当然,更多时候的比较
- 含义切片是一个种特殊的数组。是对数组的一个连续片段的引用,所以切片是一个引用类型。切片可以是数组中的一部分,也可以是由起始和终止索引标识的一
- 一、前言 英语单词之间是通过空格分隔的,但是中文却不存在空格的概念,因此需要
- 优化查询使用Explain语句分析查询语句Explain 用来分析 SELECT 查询语句,开发人员可以通过分析 Explain 结果来优化
- 2017年底,Tensorflow 推出Lite版本,可实现移动端的快速运行,其中,一个很关键的问题,如何把现有分类模型(.pb) 转换为(