-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_compression.py
More file actions
64 lines (56 loc) · 2.79 KB
/
string_compression.py
File metadata and controls
64 lines (56 loc) · 2.79 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# Implement a method to perform basic string compression on duplicate strings:
# aaaabbbbcccdddd => a4b4c3d4. However, if the compressed string would not be
# smaller than the original string, the function should return the original string. It
# can be assumed that the string in this case will only be composed of uppercase and
# lowercase letters.
# First solution: Go over the string once, and count duplicate letters, placing the counts in the new string
def compress_string(string_to_compress, return_compressed_value = False):
#compressed_string = ""
counter = 1
character_on = ""
prev_character = ""
final_list = []
for char in string_to_compress:
if char != prev_character and prev_character != "":
# compressed_string += str.format("{}{}", character_on, counter)
final_list.append(str(character_on))
final_list.append(str(counter))
character_on = char
counter = 1
elif prev_character == "":
character_on = char
counter = 1
elif char == prev_character:
counter += 1
prev_character = char
compressed_string = "".join(final_list)
if len(compressed_string) < len(string_to_compress) or return_compressed_value:
return compressed_string
return string_to_compress
if __name__ == "__main__":
import time
from random import choice
import string
from timeit import default_timer as timer
string_val = "".join(choice(string.ascii_lowercase) for i in range(1000))
start_time = timer()
val_1 = str.format("Compressed: {} || Chosen: {} || Input Value: {}",
compress_string("Hello", True), compress_string("Hello"), "Hello")
val_2 = str.format("Compressed: {} || Chosen: {} || Input Value: {}",
compress_string(" That isn't possible ", True), compress_string(" That isn't possible "), " That isn't possible ")
val_3 = str.format("Compressed: {} || Chosen: {} || Input Value: {}",
compress_string("aaabbbccc", True), compress_string("aaabbbccc"), "aaabbbccc")
val_4 = str.format("Compressed: {} || Chosen: {} || Input Value: {}",
compress_string("abc", True), compress_string("abc"), "abc")
val_5 = str.format("Compressed: {} || Chosen: {} || Input Value: {}",
compress_string("abcdefff", True), compress_string("abcdefff"), "abcdefff")
val_6 = str.format("Compressed: {} || Chosen: {} || Input Value: {}",
compress_string("There once was a man who wore a hat.", True),
compress_string("There once was a man who wore a hat."), "There once was a man who wore a hat.")
end_time = timer()
print(val_1)
print(val_2)
print(val_3)
print(val_4)
print(val_5)
print(val_6)