尝试从给定字符串中获取最长的递减子字符串,同时注意区分大小写
Trying to get the longest decreasing substring from a given string, keeping the case sensitivity in mind
我一直试图从该程序中获得低于结果的结果,但由于某种原因它没有提供所需的输出。
要求的结果:
Input1 : bbaasssrppoccbaaacbaba Output1 : ['bbaa','sssrppoccbaaa','cba','ba']
Input2: hjgAvjhjKLhbfvbZSF Output2 :['h', 'jgA', 'vjh', 'jK', 'L', 'hb', 'f','vbZSF']
我得到了什么
Output: ['bbaa', 'sssrppoccbaaa', 'cba']
& Output: ['h', 'jgA', 'vjh', 'jK', 'L', 'hb', 'f']
下面的代码没有得到最后一个子字符串 "ba" & "vbZSF"
.
s1 = 'bbaasssrppoccbaaacbaba'
# s1 = 'hjgAvjhjKLhbfvbZSF'
decSub = ''
listSub = []
i= 0
while i < len(s1):
current = s1[i]
previous = s1[i] if i == 0 else s1[i-1]
if ord(current) <= ord(previous):
decSub += current
else:
listSub.append(decSub)
decSub = ''
decSub += current
i +=1
print(listSub)
如果有人可以提前提出解决方案或更好的方法来实现此目的,那就太好了result.Thanks
您只需在列表中追加缺少的 decSub
。
更新代码:
s1 = 'bbaasssrppoccbaaacbaba'
# s1 = 'hjgAvjhjKLhbfvbZSF'
decSub = ''
listSub = []
i= 0
while i < len(s1):
current = s1[i]
previous = s1[i] if i == 0 else s1[i-1]
if ord(current) <= ord(previous):
decSub += current
else:
listSub.append(decSub)
decSub = ''
decSub += current
i += 1
listSub.append(decSub)
print(listSub)
输出:
# s1 = 'bbaasssrppoccbaaacbaba'
['bbaa', 'sssrppoccbaaa', 'cba', 'ba']
# s1 = 'hjgAvjhjKLhbfvbZSF'
['h', 'jgA', 'vjh', 'jK', 'L', 'hb', 'f', 'vbZSF']
while
循环是没有任意边界的迭代的更好选择 - 在您的情况下,for
循环可能是更好的选择。
请尝试以下操作:
from typing import List
def split_special(txt: str) -> List[str]:
if len(txt) == 0:
return []
res = [txt[0]]
prev = ord(txt[0])
for l in map(ord, txt[1:]):
if prev >= l:
res[-1] += chr(l)
else:
res.append(chr(l))
prev = l
return res
输出:
>>> print(Input1)
bbaasssrppoccbaaacbaba
>>> print(split_special(Input1))
['bbaa', 'sssrppoccbaaa', 'cba', 'ba']
>>> print(Input2)
hjgAvjhjKLhbfvbZSF
>>> print(split_special(Input2))
['h', 'jgA', 'vjh', 'jK', 'L', 'hb', 'f', 'vbZSF']
我一直试图从该程序中获得低于结果的结果,但由于某种原因它没有提供所需的输出。
要求的结果:
Input1 : bbaasssrppoccbaaacbaba Output1 : ['bbaa','sssrppoccbaaa','cba','ba']
Input2: hjgAvjhjKLhbfvbZSF Output2 :['h', 'jgA', 'vjh', 'jK', 'L', 'hb', 'f','vbZSF']
我得到了什么
Output: ['bbaa', 'sssrppoccbaaa', 'cba']
& Output: ['h', 'jgA', 'vjh', 'jK', 'L', 'hb', 'f']
下面的代码没有得到最后一个子字符串 "ba" & "vbZSF"
.
s1 = 'bbaasssrppoccbaaacbaba'
# s1 = 'hjgAvjhjKLhbfvbZSF'
decSub = ''
listSub = []
i= 0
while i < len(s1):
current = s1[i]
previous = s1[i] if i == 0 else s1[i-1]
if ord(current) <= ord(previous):
decSub += current
else:
listSub.append(decSub)
decSub = ''
decSub += current
i +=1
print(listSub)
如果有人可以提前提出解决方案或更好的方法来实现此目的,那就太好了result.Thanks
您只需在列表中追加缺少的 decSub
。
更新代码:
s1 = 'bbaasssrppoccbaaacbaba'
# s1 = 'hjgAvjhjKLhbfvbZSF'
decSub = ''
listSub = []
i= 0
while i < len(s1):
current = s1[i]
previous = s1[i] if i == 0 else s1[i-1]
if ord(current) <= ord(previous):
decSub += current
else:
listSub.append(decSub)
decSub = ''
decSub += current
i += 1
listSub.append(decSub)
print(listSub)
输出:
# s1 = 'bbaasssrppoccbaaacbaba'
['bbaa', 'sssrppoccbaaa', 'cba', 'ba']
# s1 = 'hjgAvjhjKLhbfvbZSF'
['h', 'jgA', 'vjh', 'jK', 'L', 'hb', 'f', 'vbZSF']
while
循环是没有任意边界的迭代的更好选择 - 在您的情况下,for
循环可能是更好的选择。
请尝试以下操作:
from typing import List
def split_special(txt: str) -> List[str]:
if len(txt) == 0:
return []
res = [txt[0]]
prev = ord(txt[0])
for l in map(ord, txt[1:]):
if prev >= l:
res[-1] += chr(l)
else:
res.append(chr(l))
prev = l
return res
输出:
>>> print(Input1)
bbaasssrppoccbaaacbaba
>>> print(split_special(Input1))
['bbaa', 'sssrppoccbaaa', 'cba', 'ba']
>>> print(Input2)
hjgAvjhjKLhbfvbZSF
>>> print(split_special(Input2))
['h', 'jgA', 'vjh', 'jK', 'L', 'hb', 'f', 'vbZSF']