repl.it
@shahinism/

Compress String Python

Python

No description

fork
loading
Files
  • main.py
  • Packager files
  • poetry.lock
  • pyproject.toml
  • requirements.txt
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from itertools import groupby

from nose.tools import assert_equal


class CompressString(object):
    def compress(self, string):
        if not string:
            return string

        result = ""
        for char, group in groupby(string):
            count = len(list(group))
            if count > 1:
                result += f"{char}{count}"
            else:
                result += char

        if len(result) == len(string):
            return string

        return result




class TestCompress(object):
    def test_compress(self, func):
        assert_equal(func(None), None)
        assert_equal(func(""), "")
        assert_equal(func("AABBCC"), "AABBCC")
        assert_equal(func("AAABCCDDDDE"), "A3BC2D4E")
        assert_equal(func("BAAACCDDDD"), "BA3C2D4")
        assert_equal(func("AAABAACCDDDD"), "A3BA2C2D4")
        print("Success: test_compress")


def main():
    test = TestCompress()
    compress_string = CompressString()
    test.test_compress(compress_string.compress)

main()
Fetching token
?