Skip to content

Instantly share code, notes, and snippets.

@quooston
Last active March 18, 2024 03:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quooston/4d9fd813eaee624f3091c06b99975b2b to your computer and use it in GitHub Desktop.
Save quooston/4d9fd813eaee624f3091c06b99975b2b to your computer and use it in GitHub Desktop.
String Calculator
Before you start:
- Try not to read ahead.
- Do one task at a time, work incrementally.
- Only test for correct inputs, no need to test for invalid inputs.
String Calculator:
1. Create a simple StringCalculator class with one method int Add(string numbers)
1. An empty string will return 0
2. A single number will return the number
- Remember to solve things as simply as possible so that you force yourself to write tests you did not think about
- Remember to refactor after each passing test
2. Allow the Add method to handle an unknown amount of comma delimited numbers
3. Allow the Add method to handle new lines (\n) between numbers (as well as commas).
1. the following input is ok: "1\n2,3" (will equal 6)
4. Support different delimiters
1. to add a delimiter, the beginning of the string will contain a separate line that looks like this:
"//[delimiter]\n[numbers…]" for example "//;\n1;2" should return three where the new delimiter is ";"
All existing scenarios should still be supported, with ',' still the default delimeter
5. Calling Add with a negative number will throw an exception "negatives not allowed" - and the negative that was passed.if there are multiple negatives, show all of them in the exception message
----------
Continue if you can finish the steps so far in less than 30 minutes.
----------
6. Numbers bigger than 1000 should be ignored, so adding 2 + 1001 = 2
7. Delimiters can be of any length with the following format: “//[delimiter]\n” for example: “//[***]\n1***2***3” should return 6
8. Allow multiple delimiters like this: “//[delim1][delim2]\n” for example “//[*][%]\n1*2%3” should return 6.
9. make sure you can also handle multiple delimiters with length longer than one char
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment