diff --git a/src/stringcalculator.py b/src/stringcalculator.py index 9c4ba2329e6eca184f2612930ab2735b14925e77..a1463b3c7c433283b37efb5099204564f965fd7b 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}")