Skip to content
Snippets Groups Projects
Commit b1801dc1 authored by Alma Berisha's avatar Alma Berisha
Browse files

init6

parent 71d54014
Branches test-student1
No related tags found
No related merge requests found
from abc import ABC, abstractmethod
import unittest
class IStringCalculator(ABC):
@abstractmethod
def add(self, numbers: str) -> int:
pass
class StringCalculator(IStringCalculator):
def add(self, numbers: str) -> int:
if not numbers:
return 0
# Überprüfe, ob ein benutzerdefiniertes Trennzeichen angegeben ist
if numbers.startswith("//"):
delimiter_line_end = numbers.find("\n")
delimiter = numbers[2:delimiter_line_end] # Extrahiere das Trennzeichen
numbers = numbers[delimiter_line_end + 1:] # Entferne die erste Zeile mit dem Trennzeichen
else:
delimiter = ',' # Standard-Trennzeichen ist Komma, wenn keine Zeile mit dem Trennzeichen vorhanden ist
# Ersetze alle Vorkommen des Trennzeichens und teile die Eingabe
numbers = numbers.replace("\n", delimiter)
nums = numbers.split(delimiter)
# Filtere alle Zahlen, die größer als 1000 sind
nums = [int(num) for num in nums if int(num) <= 1000]
# Prüfe auf negative Zahlen
negatives = [num for num in nums if num < 0]
if negatives:
# Wenn negative Zahlen vorhanden sind, werfe eine Ausnahme
raise ValueError(f"Negatives not allowed: {', '.join(map(str, negatives))}")
# Berechne die Summe der Zahlen, die <= 1000 sind
return sum(nums)
class TestStringCalculator(unittest.TestCase):
def setUp(self):
self.calculator = StringCalculator()
def test_empty_string(self):
self.assertEqual(self.calculator.add(""), 0)
def test_single_number(self):
self.assertEqual(self.calculator.add("1"), 1)
def test_two_numbers(self):
self.assertEqual(self.calculator.add("1,2"), 3)
def test_multiple_numbers(self):
self.assertEqual(self.calculator.add("1,2,3,4,5"), 15)
def test_numbers_with_newline(self):
self.assertEqual(self.calculator.add("1\n2,3"), 6)
def test_numbers_with_multiple_newlines(self):
self.assertEqual(self.calculator.add("1\n2\n3\n4\n5"), 15)
def test_negative_number(self):
with self.assertRaises(ValueError) as context:
self.calculator.add("1,-2,3")
self.assertEqual(str(context.exception), "Negatives not allowed: -2")
def test_multiple_negative_numbers(self):
with self.assertRaises(ValueError) as context:
self.calculator.add("1,-2,-3,4")
self.assertEqual(str(context.exception), "Negatives not allowed: -2, -3")
def test_custom_delimiter(self):
self.assertEqual(self.calculator.add("//;\n1;2"), 3)
def test_custom_delimiter_with_newline(self):
self.assertEqual(self.calculator.add("//;\n1;2\n3"), 6)
def test_custom_delimiter_with_multiple_numbers(self):
self.assertEqual(self.calculator.add("//|\n1|2|3|4"), 10)
def test_numbers_greater_than_1000(self):
self.assertEqual(self.calculator.add("2,1001"), 2)
def test_numbers_greater_than_1000_with_custom_delimiter(self):
self.assertEqual(self.calculator.add("//;\n2;1001"), 2)
def test_long_delimiter(self):
self.assertEqual(self.calculator.add("//[***]\n1***2***3"), 6)
def test_long_delimiter_with_multiple_numbers(self):
self.assertEqual(self.calculator.add("//[---]\n1---2---3---4"), 10)
if __name__ == "__main__":
unittest.main()
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment