Created
December 28, 2011 16:11
-
-
Save martijnvermaat/1528517 to your computer and use it in GitHub Desktop.
Trim common suffix from sequences
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def trim_common_suffix(sequences): | |
""" | |
Trim a list of sequences by removing the longest common suffix while | |
leaving all of them at least one character in length. | |
""" | |
if not sequences: | |
return [] | |
reverses = [seq[::-1] for seq in sequences] | |
rev_min = min(reverses) | |
rev_max = max(reverses) | |
if len(rev_min) < 2: | |
return sequences | |
for i, c in enumerate(rev_min[:-1]): | |
if c != rev_max[i]: | |
if i == 0: | |
return sequences | |
return [seq[:-i] for seq in sequences] | |
return [seq[:-(i + 1)] for seq in sequences] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment