Write a program that, given a set of numbers, returns the gaps between them. A gap should be represented as the first missing number and the last one separated by a dash.
Example: "2-3"
The user should be able to add and remove numbers and recalculate the gaps accordingly.
That means you have to implement two methods:
- insert(number)
- remove(number)
For example, after inserting numbers 2, 5, 7 the system should return [ "1-1" , "3-4" , "6-6" ] Then, after removing number 5 the system should return [ "1-1" , "3-6" ]
Both methods accept one number and return an array of 'gaps' represented as strings, as shown above.
Take one step at a time!
- Insert number 1. Verify that there's no gaps
- Insert number 2. Verify that there's one gap 1-1
- Insert number 1 then 2. Verify that there's no gaps
- Insert numbers 4 then 1. Verify that there's one gap: 2-3
- Insert numbers 1, 4 then 2. Verify that there's one gap: 3-3
- Insert numbers 1, 4 then 3. Verify that there's one gap: 2-2
- Insert numbers 1, 7 then 4. Verify that there's two gaps: 2-3 and 5-6
- Insert numbers 1, 3 then 2. Verify that there's no gaps
- Insert number 1, remove number 1. Verify that there's no gaps
- Insert number 5, remove number 5. Verify that there's no gaps
- Insert numbers 5, 6, 7, remove number 6. Verify that there's two gaps: 1-4 and 6-6
- Insert numbers 5, 6, 7, remove number 5. Verify that there's one gap: 1-5
- Insert numbers 1, 2, 5, remove number 2. Verify that there's one gap: 2-4
- Insert numbers 1, 3, 5, remove number 3. Verify that there's one gap: 2-4
- Insert numbers 1, 5, remove number 5. Verify that there's no gaps
- Output the one-sized gaps as one single number, so 1-1 becomes 1