Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # commonprefix.py
- from os.path import commonprefix
- v = commonprefix('outstanding outside outshines outspoken'.split())
- print v
- # longestcommonprefix.py
- zzz = '''
- hello
- world
- forecastable
- specialxyz
- xyz
- foresight
- shortabcdefghijklmnopqrstuvwxyz
- longestprefix
- shortest
- shortage
- longestxyz
- long
- foretold
- forever
- alpha
- foreign
- speciality
- beta
- longestabc
- short
- zebra
- forego
- abc
- shortxyz
- '''.splitlines()
- def lcp(L):
- longest = [0, []]
- vL = 0
- if len(L)>1:
- sL = sorted(L)
- v2 = ''
- for i in range(len(L)-1):
- v = commonprefix(sL[i:i+2])
- if v:
- vL = len(v)
- v2 = v
- else:
- if longest[0] == vL:
- if v2 not in longest[1]:
- longest[1].append(v2)
- elif longest[0] < vL:
- longest = [vL, [v2]]
- return longest[-1]
- 0
- print lcp(zzz)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement