any numbers ±1
đŸ¤” Most people interpreted that as only a single number could vary at a time, others took the example too literally as the full number +- 1...
So I thought...
What if any numberS (plural) means any number of digits can vary...?
const neighbourSingle = (num) => [...new Set([ clamp(num-1), num, clamp(num+1) ])]; const clamp = (num) => Math.min(Math.max(num, 0), 9); /* neighbourSingle(5) === [4, 5, 6] neighbourSingle(0) === [0, 1] */ const neighbours = (phoneString) => { return phoneString.split("").reduce((a,b) => a.flatMap(c=> neighbourSingle(parseInt(b)).map(d=> c+d)) ,[""]) } /* neighbours("55") (9) ["44", "45", "46", "54", "55", "56", "64", "65", "66"] neighbours("555") (27) ["444", "445", "446", "454", "455", "456", "464", "465", "466", "544", "545", "546", "554", "555", "556", "564", "565", "566", "644", "645", "646", "654", "655", "656", "664", "665", "666"] neighbours("5555555555") (59049) ["4444444444", "4444444445", "4444444446", "4444444454", "4444444455", "4444444456", "4444444464", "4444444465", "4444444466", "4444444544", "4444444545", "4444444546", "4444444554", "4444444555", "4444444556", "4444444564", "4444444565", "4444444566", "4444444644", "4444444645", "4444444646", "4444444654", "4444444655", "4444444656", "4444444664", "4444444665", "4444444666", "4444445444", "4444445445", "4444445446", "4444445454", "4444445455", "4444445456", "4444445464", "4444445465", "4444445466", "4444445544", "4444445545", "4444445546", "4444445554", "4444445555", "4444445556", "4444445564", "4444445565", "4444445566", "4444445644", "4444445645", "4444445646", "4444445654", "4444445655", "4444445656", "4444445664", "4444445665", "4444445666", "4444446444", "4444446445", "4444446446", "4444446454", "4444446455", "4444446456", "4444446464", "4444446465", "4444446466", "4444446544", "4444446545", "4444446546", "4444446554", "4444446555", "4444446556", "4444446564", "4444446565", "4444446566", "4444446644", "4444446645", "4444446646", "4444446654", "4444446655", "4444446656", "4444446664", "4444446665", "4444446666", "4444454444", "4444454445", "4444454446", "4444454454", "4444454455", "4444454456", "4444454464", "4444454465", "4444454466", "4444454544", "4444454545", "4444454546", "4444454554", "4444454555", "4444454556", "4444454564", "4444454565", "4444454566", "4444454644", …] */
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
đŸ¤”
Most people interpreted that as only a single number could vary at a time, others took the example too literally as the full number +- 1...
So I thought...
What if any numberS (plural) means any number of digits can vary...?