https://github.com/smartbear/string-calculator-exercise
https://github.com/smartbear/string-calculator-exercise
Last synced: 11 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/smartbear/string-calculator-exercise
- Owner: SmartBear
- Created: 2024-02-15T14:51:46.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-02-16T14:26:38.000Z (over 2 years ago)
- Last Synced: 2025-04-23T17:59:58.427Z (over 1 year ago)
- Size: 5.86 KB
- Stars: 0
- Watchers: 4
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Please fork this repository to do the exercise π
# String calculator
Create a simple calculator that takes a String and returns a integer
Signature (pseudo code):
```java
int Add(string numbers)
```
## Requirements
1. The method can take up to two numbers, separated by commas, and will return their sum as a result. So the inputs can be: ββ, β1β, β1,2β. For an empty string, it will return 0.
2. Allow the add method to handle an unknown number of arguments
3. Allow the add method to handle newlines as separators, instead of comas
β1,2\n3β should return β6β
β2,\n3β is invalid, but no need to clarify it with the program
4. Add validation to not to allow a separator at the end
For example β1,2,β should return an error (or throw an exception)
5. Allow the add method to handle different delimiters
To change the delimiter, the beginning of the input will contain a separate line that looks like this:
`//[delimiter]\n[numbers]`
- β//;\n1;3β should return β4β
- β//|\n1|2|3β should return β6β
- β//sep\n2sep5β should return β7β
- β//|\n1|2,3β is invalid and should return an error (or throw an exception) with the message ββ|β expected but β,β found at position 3.β
6. Calling add with negative numbers will return the message βNegative number(s) not allowed: β
β1,-2β is invalid and should return the message βNegative number(s) not allowed: -2β
β2,-4,-9β is invalid and should return the message βNegative number(s) not allowed: -4, -9β
7. Calling add with multiple errors will return all error messages separated by newlines.
β//|\n1|2,-3β is invalid and return the message βNegative number(s) not allowed: -3\nβ|β expected but β,β found at position 3.β
8. Numbers bigger than 1000 should be ignored, so adding 2 + 1001 = 2
## What we are going to look at
1. Code is tested
2. Code is clean and readable
3. Commits are small
4. Calculator works as intended