Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fcd1cfc

Browse files
Add task 1 - Generate ids
1 parent 1fdfc82 commit fcd1cfc

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

‎Exercises/1-ids.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
const ids = function* () {
4+
const free = ['0'];
5+
const prepared = { has: false, value: '' };
6+
7+
while (true) {
8+
if (prepared.has) {
9+
prepared.has = false;
10+
yield prepared.value;
11+
}
12+
const nextFree = free.shift();
13+
free.push('01' + nextFree);
14+
free.push('00' + nextFree);
15+
prepared.value = '11' + nextFree;
16+
prepared.has = true;
17+
yield '10' + nextFree;
18+
}
19+
};
20+
21+
module.exports = { ids };

‎Exercises/1-ids.test

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
({
2+
name: 'ids',
3+
length: [100, 1000],
4+
test: ids => {
5+
{
6+
const array = [];
7+
for (const id of ids()) {
8+
if (id.length > 10) break;
9+
array.push(id);
10+
}
11+
array.sort((a, b) => parseInt(a, 2) - parseInt(b, 2));
12+
const expected = [
13+
'100',
14+
'110',
15+
'10000',
16+
'10010',
17+
'11000',
18+
'11010',
19+
'1000000',
20+
'1000010',
21+
'1001000',
22+
'1001010',
23+
'1100000',
24+
'1100010',
25+
'1101000',
26+
'1101010',
27+
'100000000',
28+
'100000010',
29+
'100001000',
30+
'100001010',
31+
'100100000',
32+
'100100010',
33+
'100101000',
34+
'100101010',
35+
'110000000',
36+
'110000010',
37+
'110001000',
38+
'110001010',
39+
'110100000',
40+
'110100010',
41+
'110101000',
42+
'110101010',
43+
];
44+
al = array.length;
45+
el = expected.length;
46+
if (al !== el) {
47+
const msg = `Result length is ${al} instead of expected ${el}`;
48+
throw new Error(msg);
49+
}
50+
for (let i = 0; i < al; i++) {
51+
const x = array[i];
52+
const y = expected[i];
53+
if (array[i] !== expected[i]) {
54+
const msg = `Element result[${i}] === ${x} instead of expected ${y}`;
55+
throw new Error(msg);
56+
}
57+
}
58+
}
59+
}
60+
})

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /