forked from M-aljawaheri/test-driven-development-exercise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator.py
59 lines (51 loc) · 1.97 KB
/
calculator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"""
String Calculator Module
------------------------
@author: [student's name] - [student's email]
@date: [date of creation]
@description: This module is part of an Applied Software Engineering exercise at CMU-Q. It includes
a string calculator that interprets a string input and calculates the result based on
the rules defined. This exercise is intended to practice TDD (Test-Driven Development)
and learn how to handle various string parsing challenges in Python.
Purpose:
--------
The string calculator should:
1. Handle an input string that contains numbers separated by commas.
2. Return the sum of these numbers as a string.
3. Be extended through various iterations to handle newlines as separators, custom delimiters,
negative number validation, and more complex error handling.
Usage:
------
This module should be used in conjunction with a testing framework to develop each feature
incrementally using TDD practices. Students are expected to write tests before implementing
or modifying the functionality in `add`.
Example:
--------
>>> from calculator import add
>>> add("1,2")
'3'
>>> add("//;\\n1;2")
'3'
"""
def add(s: str) -> str:
"""
Adds numbers in a given string.
This function takes a single string input that can contain numbers separated by commas
and potentially other specified delimiters. It returns the sum of these numbers as a string.
Initial versions will handle basic input, with subsequent iterations to introduce handling of
different delimiters, newlines, and error conditions.
Parameters:
- s (str): A string containing the numbers to add.
Returns:
- str: The sum of the numbers as a string.
Raises:
- ValueError: If the input string is malformed or contains invalid characters or formats.
Example:
--------
>>> add("1,2")
'3'
>>> add("1,2,3")
'6'
>>>
"""
return "7" # TODO: Implement the function logic.