From be6e4e88dd1f63705556b0f464c4f968c596e0be Mon Sep 17 00:00:00 2001 From: linopino <lasse.pikkemaat@web.de> Date: Thu, 27 Mar 2025 17:51:14 +0100 Subject: [PATCH] Umsetzung der Testszenarien von Feature4 --- src/stringcalculator.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/stringcalculator.py b/src/stringcalculator.py index 9c4ba23..a1463b3 100644 --- a/src/stringcalculator.py +++ b/src/stringcalculator.py @@ -7,17 +7,23 @@ class StringCalculator(IStringCalculator): if not numbers: return 0 - # Prüfe auf das ungültige Format "1,\n" - if ",\n" in numbers: - raise ValueError("Ungültiges Zahlenformat: ',\\n' ist nicht erlaubt") + if numbers.startswith("//"): + if "\n" not in numbers: + raise ValueError("Ungültiges Format: Nicht vollständig") + delimiter_end_index = numbers.index("\n") + delimiter = numbers[2:delimiter_end_index] + numbers = numbers[delimiter_end_index + 1:] + numbers = numbers.replace(delimiter, ",") - # Trenne Zahlen anhand von Komma oder Zeilenumbruch - tokens = re.split(r",|\n", numbers) + numbers = numbers.replace("\n", ",") - # Konvertiere zu Integern und finde negative Zahlen - numbers_list = list(map(int, tokens)) - negative_numbers = [num for num in numbers_list if num < 0] + # Split the string by commas, convert each value to an integer, and sum them up + try: + numbers_list = list(map(int, numbers.split(","))) + except ValueError: + raise ValueError("Ungültiges Zahlenformat: Enthält nicht-numerische Werte") + negative_numbers = [num for num in numbers_list if num < 0] if negative_numbers: raise ValueError(f"Negative nicht erlaubt: {negative_numbers}") -- GitLab