Skip to content

Instantly share code, notes, and snippets.

@fluency03
Created October 24, 2016 07:21
Show Gist options
  • Select an option

  • Save fluency03/b8c8eb9c000b97d03f13e1bf9646ea9c to your computer and use it in GitHub Desktop.

Select an option

Save fluency03/b8c8eb9c000b97d03f13e1bf9646ea9c to your computer and use it in GitHub Desktop.
def compress_seq(seq, limit=5):
'''
Compress a sequence to a short on by compress a long-continuously-apprearing
sub-sequence into limited length.
Args:
seq: the sequence to be compressed.
limit: the sub-sequence length limitation.
Returns:
A compressed sequence.
'''
label_mark = ''
count_mark = 0
new_seq = []
for idx, lb in enumerate(seq):
if lb == label_mark:
count_mark += 1
else:
new_count = limit if count_mark > limit else count_mark
new_seq += [label_mark] * new_count
label_mark, count_mark = lb, 1
new_count = limit if count_mark > limit else count_mark
new_seq += [label_mark] * new_count
return new_seq
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment