File tree Expand file tree Collapse file tree 2 files changed +42
-0
lines changed Expand file tree Collapse file tree 2 files changed +42
-0
lines changed Original file line number Diff line number Diff line change 2
2
import optparse
3
3
import os
4
4
5
+
5
6
def main ():
6
7
"""Reads through README.md for question/answer pairs and adds them to a
7
8
list to randomly select from and quiz yourself.
Original file line number Diff line number Diff line change
1
+ ## Compress String Solution
2
+
3
+ 1 . Write a function that gets a string and compresses it
4
+ - 'aaaabbccc' -> 'a4b2c3'
5
+ - 'abbbc' -> 'a1b3c1'
6
+
7
+ ```
8
+ def compress_str(mystr: str) -> str:
9
+
10
+ result = ''
11
+
12
+ if mystr:
13
+ prevchar = mystr[0]
14
+ else:
15
+ return result
16
+
17
+ count = 1
18
+ for nextchar in mystr[1:]:
19
+ if nextchar == prevchar:
20
+ count += 1
21
+ else:
22
+ result += prevchar + str(count)
23
+ count = 1
24
+ prevchar = nextchar
25
+
26
+ result += prevchar + str(count)
27
+ return result
28
+ ```
29
+
30
+
31
+ 2 . Write a function that decompresses a given string
32
+ - 'a4b2c3' -> 'aaaabbccc'
33
+ - 'a1b3c1' -> 'abbbc'
34
+
35
+ ```
36
+ def decompress_str(mystr: str) -> str:
37
+ result = ''
38
+ for index in range(0, len(mystr), 2):
39
+ result += mystr[index] * int(mystr[index + 1])
40
+ return result
41
+ ```
You can’t perform that action at this time.
0 commit comments