Created
January 25, 2023 09:19
-
-
Save ChrisDobby/d2a2baa93aa2b7af73b2c9d7bcf230bf to your computer and use it in GitHub Desktop.
You are given a list of positive integers which represents some range of integers which has been truncated. Find the missing bits, insert ellipses to show that that part has been truncated, and print it. If the consecutive values differ by exactly two, then insert the missing value.
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
const fillGap = (num: number, prev: number) => (num - prev === 2 ? `${num - 1},${num}` : `...,${num}`) | |
const missingBits = (numbers: number[]) => | |
`[${numbers | |
.map((num, index, arr) => ({ num, prev: arr[index - 1] })) | |
.map(({ num, prev }) => (num - prev === 1 ? num : fillGap(num, prev))) | |
.join(',')}]` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment