- Start with the simplest test case of an empty string and move to one and two numbers.
- Remember to solve problems in a simple manner so that you force yourself to write tests you did not think about.
- Remember to refactor after each passing test.
-
Create a simple String calculator with a method signature like this:
int add(String numbers)
- Input: a string of comma-separated numbers.
- Output: an integer, sum of the numbers.
-
Examples:
- Input:
""
, Output:0
- Input:
"1"
, Output:1
- Input:
"1,5"
, Output:6
- Allow the add method to handle any amount of numbers.
- Input:
-
Allow the add method to handle new lines between numbers (instead of commas).
- Example:
"1\n2,3"
should return6
.
- Example:
-
Support different delimiters:
- To change the delimiter, the beginning of the string will contain a separate line that looks like this:
"//[delimiter]\n[numbers…]"
. - Example:
"//;\n1;2"
where the delimiter is";"
should return3
.
- To change the delimiter, the beginning of the string will contain a separate line that looks like this:
-
Negative number handling:
- Calling
add
with a negative number will throw an exception:"negative numbers not allowed <negative_number>"
. - If there are multiple negative numbers, show all of them in the exception message, separated by commas.
- Calling
-
Ignore numbers greater than 1000:
- Numbers greater than 1000 should be ignored in the sum calculation.
- Example:
"2,1001"
should return2
.
-
Using
*
as delimiter return Multiplication :- When you use
*
as delimiter it should return multiplication. - Example:
"//*\n3*2*5"
should return30
.
- When you use
-
Delimiters of any length:
- Delimiters can be of any length if enclosed in square brackets.
- Example:
"//[***]\n1***2***3"
where the delimiter is"***"
should return6
.
-
Track how many times
add()
is invoked:- Implement a method
getCalledCount()
that returns the number of timesadd()
method was called.
- Implement a method
-
Support multiple delimiters:
- Allow multiple delimiters to be specified using square brackets.
- Example:
"//[*][%]\n1*2%3"
should return6
.
- Handle multiple delimiters with length longer than one character:
- Delimiters can also be longer than one character.
- Example:
"//[**][%%]\n1**2%%3"
should return6
.
Remember to follow the Test-Driven Development (TDD) cycle:
- Write a failing test.
- Write the simplest code to make it pass.
- Refactor the code while keeping all tests passing.