Skip to content

Commit aba2741

Browse files
author
Víctor Terrón
committed
Replace string accumulation with str.join()
1 parent 6e5b924 commit aba2741

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

python/Chapter 1/Question1_5/compress.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@ def compress(str_):
33
if len(str_) < 2:
44
return str_
55

6+
groups = []
67
previous_character = str_[0]
78
counter = 1
8-
result = ''
9+
910
for c in str_[1:]:
1011
if c == previous_character:
1112
counter += 1
1213
else:
13-
result += previous_character + str(counter)
14+
groups.append(previous_character + str(counter))
1415
previous_character = c
1516
counter = 1
16-
result = result + c + str(counter)
17+
groups.append(c + str(counter))
18+
result = ''.join(groups)
1719
if len(result) < len(str_):
1820
return result
1921
else:

0 commit comments

Comments
 (0)