我的输入中有我的字符串
'ATTAGACCTG', 'CCTGCCGGAA', 'AGACCTGCCG', 'GCCGGAATAC'
在输出中我想要常见的最短超弦。
ATTAGACCTGCCGGAATAC
我已经使用lambda表达式完成了它,但我希望它没有lambda表达式。
from itertools import *
print min((reduce(lambda s,w:(w+s[max(i*(s[:i]==w[-i:])for i in range(99)):],s)[w in s],p)
for p in permutations(input())),key=len)
我在没有使用lambda表达式的情况下尝试了它并输出错误。
from itertools import permutations
def solve(*strings):
"""
Given a list of strings, return the shortest string that contains them all.
"""
return min((simplify(p) for p in permutations(strings)), key=len)
def prefixes(s):
"""
Return a list of all the prefixes of the given string (including itself), in ascending order (from shortest to longest).
"""
return [s[:i+1] for i in range(len(s))]
return [(i,s[:i+1]) for i in range(len(s))][::-1]
def simplify(strings):
"""
Given a list of strings, concatenate them wile removing overlaps between
successive elements.
"""
ret = ''
for s in strings:
if s in ret:
break
for i, prefix in reversed(list(enumerate(prefixes(s)))):
if ret.endswith(prefix):
ret += s[i+1:]
break
else:
ret += s
return ret
print solve('ATTAGACCTG', 'CCTGCCGGAA', 'AGACCTGCCG', 'GCCGGAATAC')
我的错误输出:
ATTAGACCTGCCGGAA
答案 0 :(得分:1)
看起来好像是
if s in ret:
break
应该是
if s in ret:
continue
认为这会解决它。
第二个退货声明也是剩余的 - 它还不会模拟reversed(list(enumerate(prefixes(s))))
吗?
最后,我认为我更喜欢你的初始map-reduce解决方案!