Skip to content

Commit bc8ccb3

Browse files
committed
use str.format
1 parent 8f7f6fd commit bc8ccb3

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

Python/chapter01/1.6 - String Compression/miguel_1.6_sol.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,15 @@ def str_compression(s: str) -> str:
2929
if c == prev_char:
3030
count += 1
3131
continue
32-
compressed.append(prev_char)
33-
compressed.append(str(count)[0])
32+
compressed.append('{}{}'.format(prev_char, str(count)[0]))
3433
prev_char = c
3534
count = 1
3635
# clean up: last character count
37-
compressed.append(prev_char)
38-
compressed.append(str(count)[0])
39-
36+
compressed.append('{}{}'.format(prev_char, str(count)[0]))
37+
compressed = ''.join(compressed)
4038
if len(compressed) >= len(s):
4139
return s
42-
return ''.join(compressed)
40+
return compressed
4341

4442

4543
class TestStringCompressionFunction(unittest.TestCase):

0 commit comments

Comments
 (0)