From 6e8a4c73641db18085191ac635b39636263ad2d7 Mon Sep 17 00:00:00 2001 From: AKU Date: Wed, 1 Dec 2021 07:43:08 +0000 Subject: [PATCH] Cleanup Python code Signed-off-by: AKU --- challenges/2021/01-sonarSweep/py/__init__.py | 23 ++++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/challenges/2021/01-sonarSweep/py/__init__.py b/challenges/2021/01-sonarSweep/py/__init__.py index 36ca721..378bd06 100644 --- a/challenges/2021/01-sonarSweep/py/__init__.py +++ b/challenges/2021/01-sonarSweep/py/__init__.py @@ -6,23 +6,22 @@ def parse(instr: str) -> List[int]: return [int(x) for x in instr.splitlines() if x != ""] -class Challenge(BaseChallenge): +def count_increases(data: List[int]) -> int: + c = 0 + for i in range(1, len(data)): + if data[i] > data[i - 1]: + c += 1 + return c + +class Challenge(BaseChallenge): @staticmethod def one(instr: str) -> int: data = parse(instr) - c = 0 - for i in range(1, len(data)): - if data[i] > data[i-1]: - c += 1 - return c + return count_increases(data) @staticmethod def two(instr: str) -> int: data = parse(instr) - c = 0 - sums = [sum(data[i:i+3]) for i in range(len(data)-2)] - for i in range(1, len(sums)): - if sums[i] > sums[i-1]: - c += 1 - return c + sums = [sum(data[i : i + 3]) for i in range(len(data) - 2)] + return count_increases(sums)