-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtape-question.js
executable file
·132 lines (109 loc) · 3.08 KB
/
tape-question.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
'use strict';
var bigArr = [], hugeArr = []
, exampleArr = [3, 1, 2, 4, 3]
, simpleArr = [ 1, 2, 4, 3];
/**
* create a number within the maximum required range.
* @returns {number}
*/
function generateNumber(){
return Math.round(Math.random() * 2000) - 1000;
}
(function generateArrs(){
// create an array with the maximum required length (with numbers of max range).
for(var i = 0; i < 100000; i++){
hugeArr.push(generateNumber());
}
// create an array with a ridiculously large length (with numbers of max range).
for(var i = 0; i < 10000000; i++){
bigArr.push(generateNumber());
}
})();
// Solutions:
/**
* @param {Boolean} - all - whether to try all arrays.
*/
function solutionA(all){
/**
* A valid solution to the tape problem.
* @param {Array} - Arr - The array to perform the operation on.
* @returns {number} - The least possible difference.
*/
function solution(Arr){
var result = 200000
, forward = 0
, backward = 0
, backwardArr = []
, temp;
for(var i = Arr.length - 1; i >= 0; i--){
backward += Arr[i];
backwardArr.push(backward);
}
for(var j = 0; j < Arr.length; j++){
forward += Arr[j];
backward = backwardArr[Arr.length - (j + 2)];
temp = absoluteDifference(forward, backward);
if(temp < result && temp > -1){
result = temp;
}
}
return result;
}
console.log( solution(simpleArr) );
console.log( solution(exampleArr) );
if(all === true){
console.log( solution(hugeArr) );
console.log( solution(bigArr) );
}
}
/**
* find the absolute difference between two numbers
* @param {number} - a
* @param {number} - b
* @returns {number}
*/
function absoluteDifference(a, b){
if(typeof a === 'undefined' || typeof b === 'undefined'){ return -1; }
if( a - b < 0){
return b - a;
}
return a - b;
}
/**
* calculate the sum of all values in an array.
* @param arr
* @returns {number}
*/
function sum(arr){
var i = arr.length;
var result = 0;
while(i--){
result += arr[i];
}
return result;
}
function solutionB(all){
/**
* A valid solution to the tape problem.
* @param {Array} - Arr - The array to perform the operation on.
* @returns {number} - The least possible difference.
*/
function solution(Arr){
var solutionArrs = [];
for(var j = 1; j < Arr.length; j++){
var slicedA = Arr.slice(0, j)
, slicedB = Arr.slice(j, Arr.length);
solutionArrs.push(absoluteDifference(sum(slicedA), sum(slicedB)));
}
solutionArrs.sort(function(a, b){ return a - b; });
return solutionArrs[0];
}
console.log( solution(simpleArr) );
console.log( solution(exampleArr) );
if(all === true){
console.log( solution(hugeArr) );
console.log( solution(bigArr) );
}
}
module.exports.solutionA = solutionA;
module.exports.solutionB = solutionB;