Skip to content

Commit

Permalink
Dramatic speed bump using itertools
Browse files Browse the repository at this point in the history
  • Loading branch information
micknudsen committed Sep 10, 2023
1 parent fb6fbec commit 2b57c15
Showing 1 changed file with 3 additions and 15 deletions.
18 changes: 3 additions & 15 deletions 2015/10/code.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
import unittest

from itertools import groupby

def say(number: str) -> str:
result = ""

digit = number[0]
count = 1

for next_digit in number[1:]:
if next_digit == digit:
count += 1
else:
result += f"{count}{digit}"
digit = next_digit
count = 1

result = result + f"{count}{digit}"
return result
def say(number: str) -> str:
return "".join(f"{len(list(group))}{digit}" for digit, group in groupby(number))


class TestCode(unittest.TestCase):
Expand Down

0 comments on commit 2b57c15

Please sign in to comment.