Python實(shí)現(xiàn)簡(jiǎn)單查找最長(zhǎng)子串功能示例
本文實(shí)例講述了Python實(shí)現(xiàn)簡(jiǎn)單查找最長(zhǎng)子串功能。分享給大家供大家參考,具體如下:
題目選自edX公開(kāi)課 MITx: 6.00.1x Introduction to Computer Science and Programming 課程 Week2 的Problem Set 1的第三題。下面是原題內(nèi)容。
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 #判斷一個(gè)字符串內(nèi)的字母是否是按字母表順序 # 如IsStrIncre('abbcdg') 返回 True # IsStrIncre('abbadg') 返回 False # 如果只有一個(gè)字符,也返回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
運(yùn)行結(jié)果:
Longest substring in alphabetical order is: ajs
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python列表(list)操作技巧總結(jié)》、《Python編碼操作技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門(mén)與進(jìn)階經(jīng)典教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
簡(jiǎn)單談?wù)凱ython中的json與pickle
下面小編就為大家?guī)?lái)一篇簡(jiǎn)單談?wù)凱ython中的json與pickle。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07python 辦公自動(dòng)化——基于pyqt5和openpyxl統(tǒng)計(jì)符合要求的名單
前幾天接到的一個(gè)需求,因?yàn)閷W(xué)校給的名單是青年大學(xué)習(xí)已學(xué)習(xí)的名單,然而要知道未學(xué)習(xí)的名單只能從所有團(tuán)員中再排查一次,過(guò)程相當(dāng)麻煩。剛好我也學(xué)過(guò)一些操作辦公軟件的基礎(chǔ),再加上最近在學(xué)pyqt5,所以我決定用python寫(xiě)個(gè)自動(dòng)操作文件的腳本給她用用。2021-05-05Python自定義命令行參數(shù)選項(xiàng)和解析器
這篇文章主要介紹了Python自定義命令行參數(shù)選項(xiàng)和解析器,本文主要使用的方法為argparse.ArgumentParser(),此模塊可以讓人輕松編寫(xiě)用戶(hù)友好的命令行接口,程序定義它需要的參數(shù),需要的朋友可以參考下2023-07-07Python爬蟲(chóng)之Selenium實(shí)現(xiàn)窗口截圖
這篇文章主要介紹了Python爬蟲(chóng)之Selenium實(shí)現(xiàn)窗口截圖,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12利用Python Django實(shí)現(xiàn)簡(jiǎn)單博客系統(tǒng)
這篇文章主要介紹了利用Python Django實(shí)現(xiàn)簡(jiǎn)單博客系統(tǒng),文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴們有很好地幫助,需要的朋友可以參考下2021-05-05