Last active
September 13, 2017 03:27
-
-
Save jonemo/6acc478fa73c82b6de2dac32f70480b6 to your computer and use it in GitHub Desktop.
Compress AAABBCCCC into A3B2C4 with regular expressions
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
#!/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