These paired functions will find the longest common string in any arbitrary array of strings:
def long_substr(data): substr = '' if len(data) > 1 and len(data[0]) > 0: for i in range(len(data[0])): for j in range(len(data[0])-i+1): if j > len(substr) and is_substr(data[0][i:i+j], data): substr = data[0][i:i+j] return substr def is_substr(find, data): if len(data) < 1 and len(find) < 1: return False for i in range(len(data)): if find not in data[i]: return False return True print long_substr(['Oh, hello, my friend.', 'I prefer Jelly Belly beans.', 'When hell freezes over!'])
Without a doubt, the algorithm could be improved, and I did not have many features for Python, so it might be more efficient and syntactic, but it should do the job.
EDIT: the nested second function is_substr, as demonstrated by J. F. Sebastian. Use remains the same. Note: without changing the algorithm.
def long_substr(data): substr = '' if len(data) > 1 and len(data[0]) > 0: for i in range(len(data[0])): for j in range(len(data[0])-i+1): if j > len(substr) and all(data[0][i:i+j] in x for x in data): substr = data[0][i:i+j] return substr
Hope this helps,
Jason
jtjacques May 24 '10 at 12:12 a.m. 2010-05-24 00:12
source share