一求若干个字符串列表中连续的最长字符串
''
给你一个字符串组成列表strarr,给你一个整数k。[del]{啊,strarr-k}[/del]
你的任务是找出由k个字符组成的最长的连续的字符串组合。
例子:
longest_consec(["zone", "abigail", "theta", "form", "libe", "zas", "theta", "abigail"], 2) --> "abigailtheta"
n是列表的长度,如果if n = 0 or k > n or k <= 0 return ""
定义length:记录长度
定义temp:记录list[i:i+k]的长度
如果temp的长度大于length,则将list[i:i+k]的值放入变量result中
'''
def longest_consec(list,k):
if len(list) == 0 or k > len(list) or k<= 0:
return ""
else:
length = 0
result = [] #存放最长的连续字符串组合的结果
#len(list)-k+1 确保可以遍历到最后K个连续的字符串组合
for i in range(len(list)-k+1):
temp = sum([len(each) for each in list[i:i+k]])
if temp > length:
length = temp
result = list[i:i+k]
return "".join(result)
def assert_equals(func, target, *args):
if func == target:
print("SUCC")
else:
print("Fail ! {0} not equals {1}".format(func,target))
assert_equals(longest_consec(["zone", "abigail", "theta", "form", "libe", "zas"], 2), "abigailtheta")
assert_equals(longest_consec(["ejjjjmmtthh", "zxxuueeg", "aanlljrrrxx", "dqqqaaabbb", "oocccffuucccjjjkkkjyyyeehh"], 1), "oocccffuucccjjjkkkjyyyeehh")
assert_equals(longest_consec([], 3), "")
assert_equals(longest_consec(["itvayloxrp","wkppqsztdkmvcuwvereiupccauycnjutlv","vweqilsfytihvrzlaodfixoyxvyuyvgpck"], 2), "wkppqsztdkmvcuwvereiupccauycnjutlvvweqilsfytihvrzlaodfixoyxvyuyvgpck")
assert_equals(longest_consec(["wlwsasphmxx","owiaxujylentrklctozmymu","wpgozvxxiu"], 2), "wlwsasphmxxowiaxujylentrklctozmymu")
assert_equals(longest_consec(["zone", "abigail", "theta", "form", "libe", "zas"], -2), "")
assert_equals(longest_consec(["it","wkppv","ixoyx", "3452", "zzzzzzzzzzzz"], 3), "ixoyx3452zzzzzzzzzzzz")
assert_equals(longest_consec(["it","wkppv","ixoyx", "3452", "zzzzzzzzzzzz"], 15), "")
assert_equals(longest_consec(["it","wkppv","ixoyx", "3452", "zzzzzzzzzzzz"], 0), "")
结果:
SUCC
SUCC
SUCC
SUCC
SUCC
SUCC
SUCC
SUCC
SUCC
题目来自:
https://fishc.com.cn/forum.php?mod=viewthread&tid=86431&ctid=588
求数字列表的切片最大值
'''
相信前两天看那么多字的题目已经烦了,今天看个简单的。
求出数列中和最大的切片的值。
例如[-2, 1, -3, 4, -1, 2, 1, -5, 4]中,[ 4, -1, 2, 1]的和最大为6,则返回 6
如果空列表,或者和为负,则返回0
解题思路
1、先求局部最大的切片
2、最大的切片值来自于局部的最大切片
变量:
local:代表局部最大的切片值.local 去当前的local 与 local + 下一个列表元素 的最大值
global:代表全局的最大的切片值。global取当前global 与 当前 local 的最大值
'''
def max_list(list):
local_max = -999999999
global_max = -999999999
if len(list) == 0:
return 0
else:
for i in range(len(list)):
local_max = max(list[i],local_max+list[i])
global_max = max(local_max,global_max)
if global_max <=0:
return 0
else:
return global_max
def assert_equals(func,target,*args):
if func == target:
print("SUCC")
else:
print("Fail !{0} not equal {1}".format(func,target))
assert_equals(max_list([]), 0)
assert_equals(max_list([-2, 1, -3, 4, -1, 2, 1, -5, 4]), 6)
assert_equals(max_list([-1, -1]), 0)
assert_equals(max_list(
[-6, 21, -4, 19, -27, 22, -19, -27, 2, 20, -16, 26, 5, 13, -14, 22, -17, 23, -7, -8, 3, 26, -11, -28, 15, -21, -6,
-22, 24, -2, -29, 28, 22, -6, 17, 4, -29, 3, 8, 2, -18, -1, -9, -23, 9, -18, 17, 15, 23, 29]), 84)
题目:
https://fishc.com.cn/forum.php?mod=viewthread&tid=86460&ctid=588