-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompression_decompression.py
More file actions
103 lines (82 loc) · 3.37 KB
/
Copy pathcompression_decompression.py
File metadata and controls
103 lines (82 loc) · 3.37 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
'''Compress or decompress text using simple run-length encoding.'''
class RunLengthEncoder:
'''Handle compression and decompression for run-length encoded strings.'''
def __init__(self, input_string):
'''Store the text that the user entered.'''
self.input_string = input_string
def initialize(self) -> str:
'''Choose whether the input should be compressed or decompressed.'''
if not self.input_string:
return "Empty input"
# If the input starts with a number, treat it as compressed text.
if self.input_string[0].isdigit():
return self.decompression(self.input_string)
# If the input starts with a letter, treat it as normal text to compress.
elif self.input_string[0].isalpha():
for i in self.input_string:
if i.isdigit():
return "Invalid input"
return self.compression(self.input_string)
else:
return "Invalid input"
def compression(self, compress:str) -> str:
'''Compress repeated letters into count-letter pairs.'''
result: list[str] = []
seq: str = ''
# Start the sequence with the first character.
seq += compress[0]
# Add a space whenever the current letter is different from the previous one.
# Example: "aaabb" becomes "aaa bb", then split() turns it into ["aaa", "bb"].
for i in compress[1:]:
if i.isalpha():
if i != seq[-1]:
seq += ' '
else:
return ("Invalid input")
seq += i
seq = seq.split()
# Convert each group into count + letter.
# Example: "aaa" becomes "3a".
for i in seq:
result.append(str(len(i)) + i[0])
return ''.join(result)
def decompression(self, decompress:str) -> str:
'''Turn count-letter pairs back into the original text.'''
result: list[str] = []
seq: str = ''
counter_letters: int = 0
counter_numbers: int = 0
# Separate numbers and letters with spaces so split() can create pairs.
# Example: "3a2b" becomes ["3", "a", "2", "b"].
for i in decompress:
if i.isdigit():
seq += i
counter_numbers += 1
counter_letters = 0
elif i.isalpha():
counter_letters += 1
counter_numbers = 0
if counter_letters > 1:
return "Invalid input"
else:
seq += ' ' + i + ' '
else:
return "Invalid input"
seq = seq.split()
if len(seq) % 2 != 0:
return "Invalid input"
# Read two items at a time: the count and the letter.
# Example: ["3", "a"] becomes "aaa".
while seq:
seq1 = seq[:2]
result.append(int(seq1[0]) * seq1[1])
seq = seq[2:]
return ''.join(result)
if __name__ == "__main__":
# Ask the user what they want to compress or decompress.
user_input = input("Enter seq of letters example:'aabbcc'"
" or number followed by letter example:'4a2b5n'\n:"
).strip()
# Create the object and run the correct action.
rle = RunLengthEncoder(input_string=user_input)
print(rle.initialize())