From 619b59ee9f66aaa2d141e4895bb830c969cefac5 Mon Sep 17 00:00:00 2001 From: mindm Date: Fri, 13 Jan 2017 12:26:47 +0200 Subject: [PATCH] [Python] challenge 12 (Unreviewed) --- challenge_12/python/mindm/README.md | 11 ++++++ challenge_12/python/mindm/src/compression.py | 35 ++++++++++++++++++++ challenge_12/python/mindm/src/test.py | 29 ++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 challenge_12/python/mindm/README.md create mode 100644 challenge_12/python/mindm/src/compression.py create mode 100644 challenge_12/python/mindm/src/test.py diff --git a/challenge_12/python/mindm/README.md b/challenge_12/python/mindm/README.md new file mode 100644 index 000000000..3e1406140 --- /dev/null +++ b/challenge_12/python/mindm/README.md @@ -0,0 +1,11 @@ +# Challenge_12 +## Compression and Decompression I +These methods handle string compression and decompression. +The input can contain any characters but only alphabethicals are compressed. + +These methods use re-library, which is a builtin. + +You can run tests with: +``` +$ python3 test.py +``` \ No newline at end of file diff --git a/challenge_12/python/mindm/src/compression.py b/challenge_12/python/mindm/src/compression.py new file mode 100644 index 000000000..0237795ab --- /dev/null +++ b/challenge_12/python/mindm/src/compression.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +import re + + +def sub_com(matchobj): + """ Calculates the compressed string from the first letter of the match + and the length of the match + """ + letter = matchobj.group(0)[0] + length = len(matchobj.group(0)) + return "{}#{}".format(letter, length) + + +def sub_def(matchobj): + """ Decompresses the match by multiplying the first character by the + digit after the hash character + """ + letter = matchobj.group(0)[0] + length = int(matchobj.group(0)[2:]) + return "{}".format(letter * length) + + +def compress(input): + """ Compresses string using 're'-library + """ + out = re.sub(r'(.)\1\1\1+', sub_com, input) + return out + + +def decompress(input): + """ Decompresses string using 're'-library + """ + out = re.sub(r'([A-Za-z]#\d+)', sub_def, input) + return out diff --git a/challenge_12/python/mindm/src/test.py b/challenge_12/python/mindm/src/test.py new file mode 100644 index 000000000..d5948a40f --- /dev/null +++ b/challenge_12/python/mindm/src/test.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +import unittest +import random +from compression import compress, decompress + + +class TestComp(unittest.TestCase): + + def test_1(self): + self.assertEqual(decompress("a#5"), "aaaaa") + + def test_2(self): + self.assertEqual(compress("abbcccddddeeeee"), "abbcccd#4e#5") + + def test_3(self): + original = "aliiiiicia foooooooohx" + compressed = compress(original) + decompressed = decompress(compressed) + self.assertEqual(original, decompressed) + + def test_long(self): + self.assertEqual(compress("fooooooooooooooox"), "fo#15x") + + def test_long2(self): + self.assertEqual(decompress("fo#15x"), "fooooooooooooooox") + +if __name__ == '__main__': + unittest.main() \ No newline at end of file