From dd3edd19ae1ca4dd8e3dcafc0b4651630385e9a5 Mon Sep 17 00:00:00 2001 From: Dmitry Tselinko Date: Sun, 12 Jan 2020 21:07:38 +0100 Subject: [PATCH] ids implementation --- Exercises/1-ids.js | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) 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