-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_compress.py
More file actions
42 lines (32 loc) · 1.15 KB
/
string_compress.py
File metadata and controls
42 lines (32 loc) · 1.15 KB
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
'''
1.6 String Compression:
Implement a method to perform basic string compression using the counts of repeated characters.
For example, the string aabcccccaaa would become a2b1c5a3.
If the 'compressed' string would not become smaller than the original string,
your method should return the original string.
You can assume the string has only uppercase and lowercase letters (a - z).
'''
import unittest
def compress(string):
compressed = ''
count = 1
for i, char in enumerate(string):
if i == 0:
continue
prev_char = string[i - 1]
if char == prev_char:
count += 1
else:
compressed += prev_char + str(count)
count = 1
compressed += char + str(count)
return compressed if len(compressed) < len(string) else string
class TestStringCompress(unittest.TestCase):
def test_provided_example(self):
self.assertEqual(compress('aabcccccaaa'), 'a2b1c5a3')
def test_return_of_original_string(self):
self.assertEqual(compress('aabbcc'), 'aabbcc')
def test_return_compressed_if_just_shorter_than_original(self):
self.assertEqual(compress('aabbccc'), 'a2b2c3')
if __name__ == '__main__':
unittest.main()