-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-random-input.js
57 lines (48 loc) · 1.57 KB
/
generate-random-input.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
(function () {
'use strict';
function randomizePoint(min, max) {
return {
x: _.random(min.x, max.x),
y: _.random(min.y, max.y)
}
}
function generateRandomArrayOfPoints(min, max, numberOfPoints) {
var points = [],
searchStructure = [],
i = 0;
function acknowledge(point) {
var index = -min.x + point.x;
searchStructure[index] = searchStructure[index] || [];
searchStructure[index].push(point.y);
}
function exists(point) {
var index = -min.x + point.x;
return (searchStructure[index] && _.includes(searchStructure[index], point.y));
}
while (i < numberOfPoints) {
let p = randomizePoint(min, max);
if (!exists(p, searchStructure)) {
points.push(p);
acknowledge(p);
i++;
}
}
return points;
}
function generateTestCases(min, max, numberOfPoints, numberOfTestCases) {
var testCases = [];
for (let i = 0; i < numberOfTestCases; i++) {
let pointArray = generateRandomArrayOfPoints(min, max, numberOfPoints);
let solution = closestPair.bruteForce(pointArray);
testCases.push({points: pointArray, solution: solution});
}
return testCases;
}
function testCasesAsJSON(min, max, numberOfPoints, numberOfTestCases) {
var tcases = generateTestCases(min, max, numberOfPoints, numberOfTestCases);
console.log(JSON.stringify(tcases));
}
window.closestPair = window.closestPair || {};
closestPair.testCasesAsJSON = testCasesAsJSON;
closestPair.generateRandomArrayOfPoints = generateRandomArrayOfPoints;
}());