-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (48 loc) · 1.32 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const argv = require('yargs')
.usage('Usage: $0 <command> [options]')
.command('stats', 'calculate stats')
.example('$0 stats -d 5', 'calculate stats for 5 digits number')
.alias('d', 'digit')
.nargs('d', 1)
.describe('d', 'digit')
.help('h')
.alias('h', 'help')
.epilog('copyright 2020')
.demandCommand(1)
.demandOption(['d'])
.argv;
const digit = parseInt(argv.digit);
const [command] = argv._;
function stats(digit) {
const Random = require('crypto-random-string');
let firstCollision = -1;
const mapCounter = {};
const totalCounter = Math.pow(10, digit);
for(let i = 0; i < totalCounter; ++i) {
const number = Random({
length: digit,
type: 'numeric',
});
if(mapCounter[number] === undefined) {
mapCounter[number] = 0;
}
mapCounter[number]++;
if(firstCollision === -1 && mapCounter[number] > 1) {
firstCollision = i + 1;
}
}
console.log(`first collision after: ${firstCollision}`);
let totalCollision = 0;
let maxCollision = 0;
for(let key in mapCounter) {
totalCollision += mapCounter[key] < 2 ? 0 : 1;
maxCollision = Math.max(maxCollision, mapCounter[key] - 1);
}
console.log(`totalCollision: ${totalCollision}`);
console.log(`maxCollision: ${maxCollision}`);
}
switch(command) {
case 'stats':
stats(digit);
break;
}