Skip to content

Instantly share code, notes, and snippets.

@jonemo
Last active September 13, 2017 03:27
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 jonemo/6acc478fa73c82b6de2dac32f70480b6 to your computer and use it in GitHub Desktop.
Save jonemo/6acc478fa73c82b6de2dac32f70480b6 to your computer and use it in GitHub Desktop.
Compress AAABBCCCC into A3B2C4 with regular expressions
#!/usr/bin/env python3
import re
def compress_iter(instr):
"""Generate one compressed chunk at a time, e.g. "A3"""
for match in re.finditer(r'(.)\1{0,}', instr):
yield match.group()[0] + str(len(match.group()))
def compress(instr: str) -> str:
"""Compress AAABBCCCC into A3B2C4"""
return ''.join(compress_iter(instr))
def test_compressor():
assert compress('AAAAB') == 'A4B1'
assert compress('aaaabbcddddee') == 'a4b2c1d4e2'
assert compress('') == ''
assert compress(' ') == ' 1'
assert compress('🤖🤖🤖') == '🤖3'
if __name__ == '__main__':
test_compressor()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment