请帮我解决这个问题。我是初学者,这个练习让我疯狂,因为我尝试切片和现在的ASCII,这对我来说似乎更容易? 我只设法提取char的ord值,如果它大于字符串中的前一个char。如何从字母数组或至少它们的ord值中获取最大排序的子字符串? (代码应拉:' fruw'因为它最大化排序)
s = 'gasgrerupxkgfruwgohfzl'
s2 = []
print s
for char in range(0, len(s)):
if ord(s[char]) >= ord(s[char-1]):
s2.append(str(ord(s[char])))
print s2
我是以正确的方式思考还是应该做出哪些修改?感谢
答案 0 :(得分:1)
我最低限度地尝试并更改了您的代码
s = 'gasgrerupxkgfruwgohfzl'
s2 = "" # a temporary string to hold value
temp = [] # added an extra array
print s
if s:
for char in range(0, len(s)-1):
if ord(s[char+1]) >= ord(s[char]): # same as your code
s2+=s[char]
else: # if some letter is smaller, then add the previously longest str to temp array
s2+=s[char]
temp.append(s2)
s2 = ""
s2+=s[char+1] # As pointed out, the last letter would not have been detected as we loop up to len -1, thus we need to add this
temp.append(s2)
print max(temp,key = len) # print the longest string
else:
print s
评论尝试并解释变化
答案 1 :(得分:1)
让我们先按照您的方式使用索引:
def solve(s):
max_g = [] # this will store the final answer
curr_g = [s[0]] # this list will store the contents of current group, start with first item.
for i in xrange(1, len(s)):
c = s[i]
# Now if the current character is greater than or equal to
# last item in curr_g then simply append the current item to
# curr_g
# else compare the length of current_group with max_g
# if it's greater than length of max_g then we have found a
# new bigger group, so time to update value of max_g
# and lastly update curr_g to [c]
if c >= curr_g[-1]:
curr_g.append(c)
else:
if len(curr_g) > len(max_g):
max_g = curr_g
curr_g = [c]
#One last check of the group
if curr_g and len(curr_g) > len(max_g):
return ''.join(curr_g)
else:
return ''.join(max_g)
s = 'gasgrerupxkgfruwgohfzl'
print solve(s)
# fruw
使用生成器函数和max
的更好方法。这将一次仅在内存中存储一个组:
def groups(s):
it = iter(s)
group = [next(it)]
for c in it:
if c >= group[-1]:
group.append(c)
else:
yield ''.join(group)
group = [c]
if group:
yield ''.join(group)
<强>用法:强>
print max(groups(s), key=len)
# fruw
<强>更新强>
当然如果你想处理空字符串,那么在函数的顶部添加一个简单的if
条件。并且您没有详细说明如何处理重复的字符(实际上您在答案中也使用了>=
),但是在comment的另一个答案中判断你想要的是一个简单的>
。
答案 2 :(得分:1)
这是一个执行相同操作的函数,阅读和理解它对任何必须使用代码的人(包括将来的自己)的作用要简单得多。
def find_lss(s):
if not s:
return s # empty s or None
solutions = []
current = None
for char in s:
if current is None or current[-1] > char:
if current is not None: solutions.append(current)
current = char
else:
current += char
solutions.append(current)
return max(solutions, key=len)
请注意,无需使用ord()
,因为直接比较字符可以很好地解决您的问题。
然后你可以将它与你的例子一起使用:
>>> find_lss('gasgrerupxkgfruwgohfzl')
'fruw'
更重要的是,这个实现也适用于经典的角落,空字符串:
>>> find_lss('')
''
它甚至适用于None
:
>>> find_lss(None)
(没有输出,也没有错误;即,函数返回None
。)
答案 3 :(得分:0)
因此,我们需要花费大量精力来跟踪您所处的索引。直接迭代为for x in y
更加清晰。此外,无需为s2执行所有列表语法。只需让你的s2成为一个字符串。
s = 'gasgrerupxkgfruwgohfzl'
biglist = []
substring = s[0] #this is going to get the first character
for char in s[1:]: #loop over remaining characters
if char > substring[-1]: #if in order, add to end of string
substring += char
else: #if out of order, save what we have, start over
biglist.append(substring)
substring = char
biglist.append(substring) #save the last thing
print biglist
print max(biglist, key = len)
输出
['g','as','gr','eru','px','k','g','fruw','go','h','fz','l “]
fruw
答案 4 :(得分:0)
您可以尝试izip_longest()
创建相邻字符对并在循环中比较它们:
from itertools import izip_longest
def longest_sorted_substring(s):
longest = tmp = []
for c1, c2 in izip_longest(s, s[1:]):
tmp.append(c1)
if c1 > c2:
longest = max(tmp, longest, key=len)
tmp = []
return ''.join(longest)
>>> for s in 'gasgrerupxkgfruwgohfzl', '', 'a', 'ab', 'ba', 'acb', 'abdefgaooooooooozq', '123456780123456789abc0d':
... print "Longest sorted substring of '{}' is '{}'".format(s, longest_sorted_substring(s))
...
Longest sorted substring of 'gasgrerupxkgfruwgohfzl' is 'fruw'
Longest sorted substring of '' is ''
Longest sorted substring of 'a' is 'a'
Longest sorted substring of 'ab' is 'ab'
Longest sorted substring of 'ba' is 'a'
Longest sorted substring of 'acb' is 'ac'
Longest sorted substring of 'abdefgaooooooooozq' is 'aoooooooooz'
Longest sorted substring of '123456780123456789abc0d' is '0123456789abc'
请注意,使用izip_longest()
是因为它处理最终字符而不添加特殊情况来处理字符串结尾。