简体   繁体   中英

find the common superstring from given list of strings in python

I have my strings in my input

'ATTAGACCTG', 'CCTGCCGGAA', 'AGACCTGCCG', 'GCCGGAATAC'

In output I want the common shortest superstring.

ATTAGACCTGCCGGAATAC

I have completed it using the lambda expression but I want it without lambda expression.

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)  

I tried it without using lambda expression and got wrong output.

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')  

My wrong output:

ATTAGACCTGCCGGAA

Looks as though

if s in ret:
    break

should be

if s in ret:
    continue

Think that will fix it.

Also the second return statement is surplus - doesn't it simulate reversed(list(enumerate(prefixes(s)))) anyway?

Finally, I think I prefer your initial map-reduce solution!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM