Created
November 30, 2023 01:45
-
-
Save olets/f66aca02e993fa4aadfe0af31663b755 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function numsFromNumsAndRanges(numsAndRanges) { | |
const nums = new Set(); | |
const numsAtStartOfRanges = new Set(); | |
const numsAtEndOfRanges = new Set(); | |
if (!numsAndRanges.length > 0) { | |
return { nums, numsAtStartOfRanges, numsAtEndOfRanges }; | |
} | |
for (const numOrRange of numsAndRanges.split(',')) { | |
if (numOrRange.includes('-')) { | |
// range | |
let [first, last] = numOrRange.split('-'); | |
numsAtStartOfRanges.add(Number(first)); | |
numsAtEndOfRanges.add(Number(last)); | |
let numsInRange = Array.from({ length: Number(last) - Number(first) + 1 }, (_, i) => Number(first) + i); | |
for (const num of numsInRange) { | |
nums.add(num); | |
} | |
continue; | |
} | |
// num | |
nums.add(Number(numOrRange)); | |
numsAtStartOfRanges.add(Number(numOrRange)); | |
numsAtEndOfRanges.add(Number(numOrRange)); | |
} | |
return { nums, numsAtStartOfRanges, numsAtEndOfRanges }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment