-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcompress-string.py
More file actions
46 lines (33 loc) · 907 Bytes
/
compress-string.py
File metadata and controls
46 lines (33 loc) · 907 Bytes
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
- AAABCDDDE
- A3BCD3E
"""
# TODO: use a multiset or a hashtable to store the frequency of each char
def main():
input_string = "AAABCDDDE"
input_string = "AAABCC"
compressed = ''
count = 0
print input_string
print len(input_string), range(len(input_string))
for i in range(len(input_string)):
if i+2 > len(input_string):
if count == 0:
compressed += input_string[i]
break
if input_string[i] != input_string[i+1]:
"""
if input_string[i+1] != input_string[i+2]:
compressed += input_string[i]
else:
"""
count += 1
compressed += input_string[i] + str(count)
count = 0
else:
count += 1
print compressed
if __name__ == "__main__":
main()