[英]Find longest substring in alphabetical order
我想編寫一個按字母順序打印最長子字符串的程序。
如果是平局,它會打印第一個子字符串。
這是我寫的
import sys
s1 = str(sys.argv[1])
alpha = "abcdefghijklmnopqrstuvwxyz"
def longest_substring(s1):
for i in range(len(alpha)):
for k in range(len(alpha)):
if alpha[i:k] in s1:
return alpha[i:k]
print("Longest substring in alphabetical order:", longest_substring(s1))
但是,它不起作用,我也不知道該如何做第二部分。
你能幫我嗎?
這是您的代碼應實現所需的外觀:
#!/usr/bin/env python3.6
import sys
s1 = str(sys.argv[1])
alpha = "abcdefghijklmnopqrstuvwxyz"
subs = []
def longest_substring(s1):
for i in range(len(alpha)):
for k in range(len(alpha)):
if alpha[i:k] in s1:
subs.append(alpha[i:k])
return max(subs, key=len)
print("Longest substring in alphabetical order:", longest_substring(s1))
您將在找到的第一個按字母順序排序的子字符串上從函數中返回。 在我的代碼中,我們將它們添加到列表中,然后打印出最長的列表。
您可以構建所有連續子字符串的列表,然后采用最大長度的子字符串,而不是構建所有可能的子字符串片段的列表,然后檢查字符串中存在哪個子字符串片段。
通過使用該字符的ord
與增加的計數器之間的差異對字符進行分組,可以輕松完成此操作; 連續的字符將具有恆定的差異。 itertools.groupby
用於執行分組:
from itertools import groupby, count
alpha = "abcdefghijklmnopqrstuvwxyz"
c = count()
lst_substrs = [''.join(g) for _, g in groupby(alpha, lambda x: ord(x)-next(c))]
substr = max(lst_substrs, key=len)
print(substr)
# abcdefghijklmnopqrstuvwxyz
正如@AdamSmith所評論的那樣,以上假設字符始終按字母順序排列。 如果情況並非如此,則可以通過檢查組中的項目是否按字母順序來強制執行以下命令:
from itertools import groupby, count, tee
lst = []
c = count()
for _, g in groupby(alpha, lambda x: ord(x)-next(c)):
a, b = tee(g)
try:
if ord(next(a)) - ord(next(a)) == -1:
lst.append(''.join(b))
except StopIteration:
pass
lst.extend(b) # add each chr from non-alphabetic iterator (could be empty)
substr = max(lst, key=len)
假定子字符串包含2個或更多字母順序的字符。 這樣,您不僅應該返回第一次出現的事件,而且應該收集所有消息並找到最長的消息。 我盡力保持您的想法不變,但這不是最有效的方法:
def longest_substring(s1):
res = []
for i in range(len(alpha) - 2):
for k in range(i + 2, len(alpha)):
if alpha[i:k] in s1:
res.append(alpha[i:k])
return max(res, key=len)
您重寫itertools.takewhile
版本以采用二進制比較功能,而不是一元比較功能。
def my_takewhile(predicate, starting_value, iterable):
last = starting_value
for cur in iterable:
if predicate(last, cur):
yield cur
last = cur
else:
break
然后,您可以將單詞小寫(因為"Za"
不是按字母順序排列,但是任何[AZ]
詞典上都比[az]
早[AZ]
進行比較)並獲得所有子字符串。
i = 0
substrings = []
while i < len(alpha):
it = iter(alpha[i:])
substring = str(my_takewhile(lambda x,y: x<y, chr(0), it))
i += len(substring)
substrings.append(substring)
然后只要找到子串中最長的子substrings
。
result = max(substrings, key=len)
備份並再次查看此問題。 1.您正在尋找最大值,並且應該基本上(偽代碼):
set a max to ""
loop through sequences
if new sequence is bigger the max, then replace max
這是一個版本:
def longest_substring(s1):
max_index, max_len = 0, 0 # keep track of the longest sequence here
last_c = s1[0] # previous char
start, seq_len = 0, 1 # tracking current seqence
for i, c in enumerate(s1[1:]):
if c >= last_c: # can we extend sequence in alpha order
seq_len += 1
if seq_len > max_len: # found longer
max_index, max_len = start, seq_len
else: # this char starts new sequence
seq_len = 0
start = i + 1
last_c = c
return s1[max_index:max_index+max_len]
s = 'azcbobobegghakl'
def max_alpha_subStr(s):
'''
INPUT: s, a string of lowercase letters
OUTPUT: longest substing of s in which the
letters occur in alphabetical order
'''
longest = s[0] # set variables 'longest' and 'current' as 1st letter in s
current = s[0]
for i in s[1:]: # begin iteration from 2nd letter to the end of s
if i >= current[-1]: # if the 'current' letter is bigger
# than the letter before it
current += i # add that letter to the 'current' letter(s) and
if len(current) > len(longest): # check if the 'current' length of
# letters are longer than the letters in'longest'
longest = current # if 'current' is the longest, make 'longest'
# now equal 'current'
else: # otherwise the current letter is lesser
# than the letter before it and
current = i # restart evaluating from the point of iteration
return print("Longest substring in alphabetical order is: ", longest)
max_alpha_subStr(s)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.