Skip to content

Instantly share code, notes, and snippets.

@BenjaminRosell
Created April 27, 2021 22:48
Show Gist options
  • Save BenjaminRosell/68648f2cefc3133b5a1e7cf6f0a780b9 to your computer and use it in GitHub Desktop.
Save BenjaminRosell/68648f2cefc3133b5a1e7cf6f0a780b9 to your computer and use it in GitHub Desktop.
HD Challenge
class DateRange {
constructor(start, end) {
this.start = start
this.end = end
}
}
let date1= new Date(2021, 4, 1, 10, 10, 10)
let date2= new Date(2021, 4, 2, 10, 10, 10)
let date3= new Date(2021, 4, 3, 10, 10, 10)
let date4= new Date(2021, 4, 4, 10, 10, 10)
let date5= new Date(2021, 4, 5, 10, 10, 10)
let date6= new Date(2021, 4, 6, 10, 10, 10)
function merge_ranges(DateRangesArray) {
if(DateRangesArray.lenght === 0) return false; //Or whatever error behavious we want...
let results = [DateRangesArray[0]] // Set the reference Date_Range
DateRangesArray.shift(); // Remove the reference Date_Range from the array to iterate
DateRangesArray.forEach(range => {
results.forEach((result, idx) => {
let previousEnd = idx === 0 ? null : results[idx-1].end
let nextStart = idx < results.length - 1 ? results[idx + 1].start: null;
// Extend the range if the new range overlaps, and ends after the begining of the result....
if(range.start < result.start && range.end > result.start) {
result.start = range.start
} else if (range.start < result.start && range.end < result.start){
//if we dont intersect on the lower end, there are two possibilities
// 1 -- There is no results range before the range, in wich case, we add a range before...
if(previousEnd === null) {
results.unshift(new DateRange(range.start, range.end))
previousEnd = range.end
} else {
// 2 -- There is another range before, in case we need to merge...
// and replace the next item with the result of the merge
results[idx - 1] = merge_ranges([range, results[idx - 1]])[0]
previousEnd = results[idx - 1].end
}
}
if (range.start > result.end) {
//if we dont intersect at the end, there are two possibilities
// 1 -- There is no range after the current range, in wich case, we add a range after...
if(nextStart === null) {
results.push(new DateRange(range.start, range.end))
nextStart = range.start
} else {
// 2 -- There is another range ahead, in case we need to merge...
// and replace the next item with the result of the merge
results[idx + 1] = merge_ranges([range, results[idx + 1]])[0]
nextStart = results[idx + 1].start
}
}
// Otherwise, if the range overlaps but finishes after the current range and if it does not overlap with the next, extend
if((range.start < result.end && range.end > result.end) && (nextStart === null || range.end < nextStart )) {
result.end = range.end
}
})
})
return results;
}
function test_sample_1() {
console.log('\n********* Testing the FIRST example : **********\n');
let array = [new DateRange(date1, date3), new DateRange(date2, date4)]
const result = merge_ranges(array)
if (result[0] instanceof DateRange) {
console.log('Got appropriate object instance')
if(result[0].start !== date1)
console.log(' X Houston, we have a problem : First Date')
else
console.log('First date is ok')
if(result[0].end !== date4)
console.log(' X Houston, we have a problem : Second date')
else
console.log('Second date is Ok')
}
console.log(result)
console.log('\n\n')
}
function test_sample_2() {
console.log('\n********* Testing the SECOND example : **********\n')
let array = [new DateRange(date1, date3), new DateRange(date2, date5), new DateRange(date4, date6)]
const result = merge_ranges(array)
if (result[0] instanceof DateRange) {
console.log('Got appropriate object instance')
if(result[0].start !== date1)
console.log(' X Houston, we have a problem : First Date')
else
console.log('First date is ok')
if(result[0].end !== date6)
console.log(' X Houston, we have a problem : Second date')
else
console.log('Second date is Ok')
}
console.log(result)
console.log('\n\n')
}
function test_sample_3() {
console.log('\n********* Testing the THIRD example : **********\n');
let array = [new DateRange(date1, date2), new DateRange(date3, date5), new DateRange(date4, date6)]
const result = merge_ranges(array)
if (result.length !== 2) console.log('Not the right number of elements in response');
if (result[0] instanceof DateRange) {
console.log('Got appropriate object instance')
if(result[0].start !== date1)
console.log(' X Houston, we have a problem : First Date')
else
console.log('First date is ok')
if(result[0].end !== date2)
console.log(' X Houston, we have a problem : Second date')
else
console.log('Second date is Ok')
}
if (result[1] instanceof DateRange) {
console.log('Got appropriate object instance')
if(result[1].start !== date3)
console.log(' X Houston, we have a problem : Second range - First Date')
else
console.log('First date is ok')
if(result[1].end !== date6)
console.log(' X Houston, we have a problem : Second range - Second date')
else
console.log('Second date is Ok')
}
console.log(result);
console.log('\n\n')
}
function test_sample_4() {
console.log('\n********* Testing the FOURTH example : **********\n');
let array = [new DateRange(date1, date2), new DateRange(date3, date4), new DateRange(date5, date6)]
const result = merge_ranges(array)
if (result.length !== 3) console.log('Not the right number of elements in response');
if (result[0] instanceof DateRange) {
console.log('Got appropriate object instance')
if(result[0].start !== date1)
console.log(' X Houston, we have a problem : First Date')
else
console.log('First date is ok')
if(result[0].end !== date2)
console.log(' X Houston, we have a problem : Second date')
else
console.log('Second date is Ok')
}
if (result[1] instanceof DateRange) {
console.log('Got appropriate object instance')
if(result[1].start !== date3)
console.log(' X Houston, we have a problem : Second range - First Date')
else
console.log('First date is ok')
if(result[1].end !== date4)
console.log(' X Houston, we have a problem : Second range - Second date')
else
console.log('Second date is Ok')
}
if (result[2] instanceof DateRange) {
console.log('Got appropriate object instance')
if(result[2].start !== date5)
console.log(' X Houston, we have a problem : Third range - First Date')
else
console.log('First date is ok')
if(result[2].end !== date6)
console.log(' X Houston, we have a problem : Third range - Second date')
else
console.log('Second date is Ok')
}
console.log(result)
console.log('\n\n')
}
function test_sample_5() {
console.log('\n********* Testing the FIFTH example : **********\n');
let array = [new DateRange(date1, date3), new DateRange(date2, date6), new DateRange(date4, date5)]
const result = merge_ranges(array)
if (result.length !== 1) console.log('Not the right number of elements in response');
if (result[0] instanceof DateRange) {
console.log('Got appropriate object instance')
if(result[0].start !== date1)
console.log(' X Houston, we have a problem : First Date')
else
console.log('First date is ok')
if(result[0].end !== date6)
console.log(' X Houston, we have a problem : Second date')
else
console.log('Second date is Ok')
}
console.log(result)
console.log('\n\n')
}
function test_sample_6() {
console.log('\n********* Testing the SIXTH example : **********\n');
let array = [new DateRange(date1, date2), new DateRange(date3, date5), new DateRange(date4, date6)]
const result = merge_ranges(array)
if (result.length !== 2) console.log('Not the right number of elements in response');
if (result[0] instanceof DateRange) {
console.log('Got appropriate object instance')
if(result[0].start !== date1)
console.log(' X Houston, we have a problem : First Date')
else
console.log('First date is ok')
if(result[0].end !== date2)
console.log(' X Houston, we have a problem : Second date')
else
console.log('Second date is Ok')
}
if (result[1] instanceof DateRange) {
console.log('Got appropriate object instance')
if(result[1].start !== date3)
console.log(' X Houston, we have a problem : First Date')
else
console.log('First date is ok')
if(result[1].end !== date6)
console.log(' X Houston, we have a problem : Second date')
else
console.log('Second date is Ok')
}
console.log(result)
console.log('\n\n')
}
test_sample_1()
test_sample_2()
test_sample_3()
test_sample_4()
test_sample_5()
test_sample_6()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment