diff --git a/Exercises/1-ids.js b/Exercises/1-ids.js index 29b9450..d060ef8 100644 --- a/Exercises/1-ids.js +++ b/Exercises/1-ids.js @@ -1,20 +1,21 @@ 'use strict'; const ids = function* () { - const free = ['0']; - const prepared = { has: false, value: '' }; - + let base = '1'; + let rank = -1; while (true) { - if (prepared.has) { - prepared.has = false; - yield prepared.value; + if (rank === -1) { + base = `1${'0'.repeat(base.length + 1)}`; + rank = base.length - 2; + yield base; + } + if (base[rank] === '0') { + base = `${base.slice(0, rank)}1${'0'.repeat(base.length - rank - 1)}`; + rank = base.length - 2; + yield base; + } else { + rank -= 2; } - const nextFree = free.shift(); - free.push('01' + nextFree); - free.push('00' + nextFree); - prepared.value = '11' + nextFree; - prepared.has = true; - yield '10' + nextFree; } }; pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy