-
Notifications
You must be signed in to change notification settings - Fork 0
/
numberOfDiscIntersection.spec.js
71 lines (64 loc) · 1.49 KB
/
numberOfDiscIntersection.spec.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const solution = require("./numberOfDiscIntersection")
const tests = [
{
enter: [1, 5, 2, 1, 4, 0],
expected: 11
}, {
enter: [1, 1, 1],
expected: 3
}, {
enter: [1, 0, 1, 0, 1],
expected: 6
}, {
enter: new Array(10000).fill(0),
expected: 0
}
]
tests.forEach(test => {
const startTime = new Date().getTime()
const sol = solution(test.enter)
if (sol !== test.expected) {
console.error(`Error: expected ${test.expected} but return ${sol}`)
} else {
console.log('OK')
}
console.log(`${(new Date().getTime() - startTime)*.001}s`)
})
function generateArray(L, odd) {
let arr = []
for (let i = 0 ; i < (L-1)/2 ; i++) {
const val = Math.floor(Math.random() * L)
arr.push(val)
arr.push(val)
}
arr.push(odd)
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array
}
return shuffleArray(arr)
}
function generateArraySmallInt(L, max) {
let arr = []
for (let i = 0 ; i < L; i++) {
arr.push(Math.floor((Math.random()) * max))
}
return arr
}
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array
}
function generateArrayInt3same(L) {
let arr = []
for (let i = 0 ; i < L / 3; i++) {
arr.push(i, i, i)
}
return shuffleArray(arr)
}