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
猜你喜欢
- (一)单一独立的参数如果命令行输入的参数都是各自单一独立的,直接用个循环把所有参数逐一读出来就行了。sys模块里面直接用args = sys
- 本文实例为大家分享了js实现全选取消效果的具体代码,供大家参考,具体内容如下<!DOCTYPE html><html la
- python除了关键字(keywords)和内置的类型和函数(builtins),更多的功能是通过libraries(即modules)来提
- 昨天美国雅虎正式宣布网站首页的新版,这也算互联网一件大事,尤其是对设计的朋友们。而且现在的美国雅虎的情况也不是很好的情况下有大刀阔斧的进行改
- 如果 replaceText 为函数,对于每一个匹配的子字符串,调用该函数时带有下面的 m+3 个参数,此处 m 是在 rgExp 中捕获的
- 除了IE浏览器,其他所有主流的浏览器均支持原生的 Base64 编码:btoa(text) – base64 encodes text. a
- 平时我们获取事件对象一般写法如下:function getEvent(event) { return event
- Harris 角点检测算法1. 角点角点是水平方向、垂直方向变化都很大的像素。角点检测算法的基本思想:
- Thinkphp5微信小程序获取用户信息接口的实例详解首先在官网下载示例代码, 选php的,这里有个坑 官方的php文件,编码是UTF-8+
- 内容摘要:Cookies的值比ASP其他集合(例如Form和ServerVariables)的值要复杂得多。Cookie是一小块由浏览器存贮
- 1.基于rbac的权限管理RBAC(Role-Based Access Control,基于角色的访问控制),就是用户通过角色与权限进行关联
- 1.开始----程序-----oracle------配置和移植工具-----Net Manager----本地----服务命名---ora
- asp中我们可以利用ERR对象来判断sql语句执行是否成功:SQL="Insert INTO TABLE(F1,F2) value
- 对于python的tkinter库来说,如果需要弹出文件选择框,我们需要引入一下tkinter.filedialog包,让用户直观地先择一个
- MySQL扩展库操作MySQL数据库的步骤如下:1:获取连接.2:选取书库。3:设置操作编码。4:发送SQL指令(MySQL数据库可以分为四
- 在数据库应用的设计中,我们往往会需要获取某些表的记录总数,用于判断表的记录总数是否过大,是否需要备份数据等。我们通常的做法是:select
- 首先,来说一下对话框: 对话框在Windows应用程序中使用非常普遍,许多应用程序的设定,与用户交互需要通过对话框来进行,因此对话框是Win
- IE 一如既往地带来问题。真的,我觉得 IE 坚持自己的一套无所谓,不支持或者不完全支持标准也无所谓。但是,IE6 IE7 IE8 这三个版
- 本文实例讲述了smarty简单分页的实现方法,分享给大家供大家参考。具体实现方法如下:以下是模板中的smarty代码,用smarty简单的代
- 在安排Web页面的布局时,最常用的方法之一是用HTML表格界定页面的结构。例如,假设Web页面由顶端的