Skip to content

Instantly share code, notes, and snippets.

@martijnvermaat
Created December 28, 2011 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martijnvermaat/1528517 to your computer and use it in GitHub Desktop.
Save martijnvermaat/1528517 to your computer and use it in GitHub Desktop.
Trim common suffix from sequences
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