String calculator
We use this kata to develop the fundamentals of classic TDD.
Overview
In the string calculator kata, you write a method which calculates the sum of the numbers passed to it. However, the method takes only one parameter, which is a string. You must therefore parse and convert the string before being able to perform the calculation.
Objectives
Write a program which exposes a method with the following signature:
int Add(string bowlingGameScores)
The must remain the same as you work through the following objectives:
Parse the input and return the correct sum of the numbers
If the input is empty, return 0.
If the input contains a single number, return that number.
If the input contains two numbers separated by a comma, return the sum of the two numbers, e.g.
"1,2"
returns 3.The comma delimiter cannot appear at the beginning or the end of the line, e.g.
",1,2"
or"1,2,"
The comma delimiter cannot appear more than once in a row, e.g.
"1,,2"
Handle an unknown amount of numbers
e.g.
"1,2,3,4,5,6,
...n"
Handle newline as well as commas between numbers
Numbers can be delimited by a comma, e.g.
"1,2,3"
Numbers can be delimited by a newline, e.g.
"1\n2\n3"
Numbers can be delimited by either a comma or a newline, e.g.
“1\n2,3”
Delimiters cannot appear more than one in a row, e.g.
1\n,2
Support custom delimiters
To set the delimiter, the beginning of the string should contain a separate line using this format:
“//[delimiter]\n[numbers]”
e.g.“//*\n1*2”
Delimiters can be of any length, e.g.
“//***\n1***2***3”
The first line is optional, so all existing scenarios must still be supported.
Throw an exception when passed a negative number
Passing a negative number will throw an exception with the message “Sorry, I'm not feeling very positive about this input: -n” where n is the negative number passed int.
If there are multiple negative numbers, show all of them in the exception message.
Numbers greater than 1000 are reset to 0
e.g.
"2,1001"
returns 3e.g.
"2,999"
returns 1
Support multiple custom delimiters
Custom delimiters can be delimited using this format:
“//[delimiter1]/[delimiter2]\n[numbers]”
e.g.“//*/%\n1*2%3”
Last updated