python compress and decompress string solution (#309)
* compress string solution * decompress string solution * style fix
This commit is contained in:
parent
066a273cd2
commit
3d161adce3
@ -2,6 +2,7 @@ import random
|
|||||||
import optparse
|
import optparse
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
"""Reads through README.md for question/answer pairs and adds them to a
|
"""Reads through README.md for question/answer pairs and adds them to a
|
||||||
list to randomly select from and quiz yourself.
|
list to randomly select from and quiz yourself.
|
||||||
|
41
topics/python/solutions/compress_string_solution.md
Normal file
41
topics/python/solutions/compress_string_solution.md
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
## Compress String Solution
|
||||||
|
|
||||||
|
1. Write a function that gets a string and compresses it
|
||||||
|
- 'aaaabbccc' -> 'a4b2c3'
|
||||||
|
- 'abbbc' -> 'a1b3c1'
|
||||||
|
|
||||||
|
```
|
||||||
|
def compress_str(mystr: str) -> str:
|
||||||
|
|
||||||
|
result = ''
|
||||||
|
|
||||||
|
if mystr:
|
||||||
|
prevchar = mystr[0]
|
||||||
|
else:
|
||||||
|
return result
|
||||||
|
|
||||||
|
count = 1
|
||||||
|
for nextchar in mystr[1:]:
|
||||||
|
if nextchar == prevchar:
|
||||||
|
count += 1
|
||||||
|
else:
|
||||||
|
result += prevchar + str(count)
|
||||||
|
count = 1
|
||||||
|
prevchar = nextchar
|
||||||
|
|
||||||
|
result += prevchar + str(count)
|
||||||
|
return result
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
2. Write a function that decompresses a given string
|
||||||
|
- 'a4b2c3' -> 'aaaabbccc'
|
||||||
|
- 'a1b3c1' -> 'abbbc'
|
||||||
|
|
||||||
|
```
|
||||||
|
def decompress_str(mystr: str) -> str:
|
||||||
|
result = ''
|
||||||
|
for index in range(0, len(mystr), 2):
|
||||||
|
result += mystr[index] * int(mystr[index + 1])
|
||||||
|
return result
|
||||||
|
```
|
Loading…
Reference in New Issue
Block a user