Example of a range [1, 4]
- includes integers: 1, 2, 3
, and 4
.
Example of a list: [[1, 4], [109, 206], [400, 600]]
const Ranges = require('./ranges');
const MyRangeList = new Ranges(); // No initial data
const MyRangeList_2 = new Range([[5, 7], [109, 206], [400, 600]]); // Init with data
Initial subranges may be placed in any order, but subranges itself must be correct, i.e. first number must be less than second one.
Add subrange to the list. Intersected subranges will be joined.
Remove subrange from whole range. Intersected subranges will be eliminated.
Returns string representation of Ranges list.
Output string representation of ranges list to the console.
Returns Array of range arrays.
class Ranges {
/**
* Adds a range to the list
*/
add(range)
/**
* Removes a range from the list
*/
remove(range)
/**
* Prints out the list of ranges
*/
print()
/**
* String view of range list
*/
toString()
/**
* List of ranges
*/
list
}
npm install && npm run test
or
yarn && yarn test
// Example
const r = new Ranges();
r.add([1, 4]);
r.print();
// Should display: [1, 4]
r.add([10, 20]);
r.print();
// Should display: [1, 4] [10, 20]
r.add([10, 10]);
r.print();
// Should display: [1, 4] [10, 20]
r.add([21, 21]);
r.print();
// Should display: [1, 4] [10, 21]
r.add([2, 4]);
r.print();
// Should display: [1, 4] [10, 21]
r.add([3, 8]);
r.print();
// Should display: [1, 8] [10, 21]
r.remove([10, 10]);
r.print();
// Should display: [1, 8] [11, 21]
r.remove([10, 11]);
r.print();
// Should display: [1, 8] [12, 21]
r.remove([15, 17]);
r.print();
// Should display: [1, 8] [12, 14] [18, 21]
r.remove([3, 19]);
r.print();
// Should display: [1, 2] [20, 21]