Sorry, I have not read about all these cool algorithms yet, but you asked about finding a common factor, so I thought about the following:
import itertools def commons(exprs): groups = [] for n in range(2, len(exprs)+1): for comb in itertools.combinations(exprs, n): common = set.intersection(*map(set, comb)) if common: groups.append( (len(common), n, comb, ''.join(common))) return sorted(groups, reverse=True) >>> exprs ['ABC', 'BCD', 'DE', 'ABCE'] >>> commons(exprs) [(3, 2, ('ABC', 'ABCE'), 'ACB'), (2, 3, ('ABC', 'BCD', 'ABCE'), 'CB'), (2, 2, ('BCD', 'ABCE'), 'CB'), (2, 2, ('ABC', 'BCD'), 'CB'), (1, 2, ('DE', 'ABCE'), 'E'), (1, 2, ('BCD', 'DE'), 'D')]
The function returns a list sorted by:
- Total Ratio Length
- Number of members having this total ratio