From b4af41133dfa4697d7147fb747e352a0b2e2f28a Mon Sep 17 00:00:00 2001 From: jerr-it Date: Thu, 29 Sep 2022 16:40:45 +0200 Subject: [PATCH 1/3] Removed unused commit --- compression/run_length_encoding.py | 50 ++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 compression/run_length_encoding.py diff --git a/compression/run_length_encoding.py b/compression/run_length_encoding.py new file mode 100644 index 000000000000..7b8be87c59f9 --- /dev/null +++ b/compression/run_length_encoding.py @@ -0,0 +1,50 @@ +def run_length_encode(input: str) -> list: + """ + Performs Run Length Encoding + >>> run_length_encode("AAAABBBCCDAA") + [('A', 4), ('B', 3), ('C', 2), ('D', 1), ('A', 2)] + >>> run_length_encode("A") + [('A', 1)] + >>> run_length_encode("AA") + [('A', 2)] + >>> run_length_encode("AAADDDDDDFFFCCCAAVVVV") + [('A', 3), ('D', 6), ('F', 3), ('C', 3), ('A', 2), ('V', 4)] + """ + encoded = [] + count = 1 + + for i in range(len(input)): + if i + 1 < len(input) and input[i] == input[i + 1]: + count += 1 + else: + encoded.append((input[i], count)) + count = 1 + + return encoded + + +def run_length_decode(input: list) -> str: + """ + Performs Run Length Decoding + >>> run_length_decode([('A', 4), ('B', 3), ('C', 2), ('D', 1), ('A', 2)]) + 'AAAABBBCCDAA' + >>> run_length_decode([('A', 1)]) + 'A' + >>> run_length_decode([('A', 2)]) + 'AA' + >>> run_length_decode([('A', 3), ('D', 6), ('F', 3), ('C', 3), ('A', 2), ('V', 4)]) + 'AAADDDDDDFFFCCCAAVVVV' + """ + decoded = "" + + for i in input: + decoded += i[0] * i[1] + + return decoded + + +if __name__ == "__main__": + from doctest import testmod + + testmod(name="run_length_encode", verbose=True) + testmod(name="run_length_decode", verbose=True) From 711a6d697624dba2d52bb20bda8e47d50a3bdad8 Mon Sep 17 00:00:00 2001 From: jerr-it Date: Sat, 1 Oct 2022 21:38:50 +0200 Subject: [PATCH 2/3] Added wikipedia url --- compression/run_length_encoding.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/compression/run_length_encoding.py b/compression/run_length_encoding.py index 7b8be87c59f9..82475d4a4c96 100644 --- a/compression/run_length_encoding.py +++ b/compression/run_length_encoding.py @@ -1,3 +1,6 @@ +# https://en.wikipedia.org/wiki/Run-length_encoding + + def run_length_encode(input: str) -> list: """ Performs Run Length Encoding From 1d71bc486f9eff1475e6582bcdf66be9d0a6fed4 Mon Sep 17 00:00:00 2001 From: jerr-it Date: Sun, 2 Oct 2022 16:37:42 +0200 Subject: [PATCH 3/3] Renamed parameter, changed decoding to use list comprehension --- compression/run_length_encoding.py | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/compression/run_length_encoding.py b/compression/run_length_encoding.py index 82475d4a4c96..691e19095dc6 100644 --- a/compression/run_length_encoding.py +++ b/compression/run_length_encoding.py @@ -1,7 +1,7 @@ # https://en.wikipedia.org/wiki/Run-length_encoding -def run_length_encode(input: str) -> list: +def run_length_encode(text: str) -> list: """ Performs Run Length Encoding >>> run_length_encode("AAAABBBCCDAA") @@ -16,17 +16,17 @@ def run_length_encode(input: str) -> list: encoded = [] count = 1 - for i in range(len(input)): - if i + 1 < len(input) and input[i] == input[i + 1]: + for i in range(len(text)): + if i + 1 < len(text) and text[i] == text[i + 1]: count += 1 else: - encoded.append((input[i], count)) + encoded.append((text[i], count)) count = 1 return encoded -def run_length_decode(input: list) -> str: +def run_length_decode(encoded: list) -> str: """ Performs Run Length Decoding >>> run_length_decode([('A', 4), ('B', 3), ('C', 2), ('D', 1), ('A', 2)]) @@ -38,12 +38,7 @@ def run_length_decode(input: list) -> str: >>> run_length_decode([('A', 3), ('D', 6), ('F', 3), ('C', 3), ('A', 2), ('V', 4)]) 'AAADDDDDDFFFCCCAAVVVV' """ - decoded = "" - - for i in input: - decoded += i[0] * i[1] - - return decoded + return "".join(char * length for char, length in encoded) if __name__ == "__main__":