-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithm.js
More file actions
169 lines (136 loc) · 6.34 KB
/
algorithm.js
File metadata and controls
169 lines (136 loc) · 6.34 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
module.exports = class algorithm {
/*
Given a specific set of numbers, this algorithm will recursively attempt
to reach a 'subset' of length 'numCharacters' which will add up to the 'sum'.
The level tells where to put each element of 'set' into 'subset'.
*/
static isSubsetSum(set, n, sum, subset, level, numCharacters) {
//console.log("set: ", set);
//console.log("subset: ", subset);
// Base case, if the 'sum' is met and the 'level' matches 'numCharacters', return 'true';
if(sum == 0 && level == numCharacters ) {
return true;
}
// Base case, return 'false' if 'sum' is met, but the array is not filled to 'numCharacters'
// Also needs to check that n == 0 so that the last possible solution can be sought out
if(sum == 0 && level < numCharacters && n == 0) {
return false;
}
// If 'level' is greater than the 'numCharacters', return 'false' to prevent Array Out of Bounds
if(level >= numCharacters) {
return false;
}
// return 'false' if there are no more elements 'n' in 'set' to look at
if(n == 0) {
return false;
}
// If the current element @n - 1 is bigger than the sum, ignore it, move onto the next number in the set
if(set[n - 1] > sum) {
//System.out.println("HERE!");
return this.isSubsetSum(set, n - 1, sum, subset, level, numCharacters);
}
// Set the current 'subset' @level to the current value of set which we are looking at
// The value will be replaced as certain conditions in each branch is not met
subset[level] = set[n - 1];
// Branches left and right until a A) the 'sum' is not met and will return 'false' or B) the 'sum' is met and will return 'true'
return this.isSubsetSum(set, n-1, sum - set[n - 1], subset, level + 1, numCharacters)
|| this.isSubsetSum(set, n - 1, sum, subset, level, numCharacters);
}
// A method which returns a random value between 'min' and 'max' rounded to the nearest 0.5
static getRandVal(min, max) {
// Change to a number to avoid NaN errors
min = Number(min);
max = Number(max);
// Debugging use console.log("Inside 'getRandVal()'");
// Debugging use console.log("min ", min);
// Debugging use console.log("max ", max);
let range = max - min;
// Debugging use console.log("range ", range);
let value = ((Math.random() * range) + min);
// Debugging use console.log("not rounded: ", value);
value = Math.round((value * 2))/2.0;
// Debugging use console.log("rounded: ", value);
return value;
}
static getQuartile(arr) {
let length = arr.length;
let lengthQ1 = 0;
let lengthQ3 = 0;
let median = -1;
// If the dataset is even, the median will be between 2 values in the dataset
if(length % 2 == 0) {
// To find the middle of an even dataset we will take the left and right value
let right = length/2;
let left = right - 1;
// Debugging use console.log("left index ", left, " is ", arr[left]);
// Debugging use console.log("right index ", right, " is ", arr[right]);
// Average the two left and right values
// Code to actually set median: median = (arr[left] + arr[right])/2;
median = (left + right)/2;
// The length of the theoretical subarray for Q1 will be the value 'right'
lengthQ1 = right;
// The start of the theoretical subarray for Q3 will be the value of 'right'
lengthQ3 = right;
}
// If the dataset is odd, the median will be the center
else {
// Get the center
let middle = (length - 1) / 2;
// Debugging use console.log("median index ", middle, " is ", arr[middle]);
// Code to actually set median: median = arr[middle];
median = middle;
// The length of the theoretical subarray for Q1 will be the value 'middle'
lengthQ1 = middle;
// The start of the theoretical subarray for Q3 will be the value of 'middle' + 1
lengthQ3 = middle + 1;
}
/*
console.log("median ", median);
console.log("lengthQ1 end ", lengthQ1);
console.log("lengthQ3 start", lengthQ3);
*/
let q1 = -1;
// If the theoretical subarray is even, Q1 will be between 2 values
if(lengthQ1 % 2 == 0) {
let right = lengthQ1/2;
let left = right - 1;
// Debugging use console.log("left index Q1 ", left, " is ", arr[left]);
// Debugging use console.log("right index Q1 ", right, " is ", arr[right]);
// Code to actually set q1: q1 = (arr[left] + arr[right])/2;
q1 = (left + right)/2;
}
// If the dataset is odd, Q1 will be the center
else {
let middle = (lengthQ1 - 1) / 2;
// Debugging use console.log("median index Q1 ", middle, " is ", arr[middle]);
// Code to actually set q1: q1 = arr[middle];
q1 = middle;
}
// Debugging use console.log("q1 ", q1);
let q3 = -1;
// If the theoretical subarray is even, Q3 will be between 2 values
if((arr.length - lengthQ3) % 2 == 0) {
let right = (arr.length + lengthQ3)/2;
let left = right - 1;
// Debugging use console.log("left index Q3 ", left, " is ", arr[left]);
// Debugging use console.log("right index Q3 ", right, " is ", arr[right]);
// Code to actually set q1: q3 = (arr[left] + arr[right])/2;
q3 = (left + right)/2;
}
// If the theoretical subarray if odd, Q3 will be the center
else {
let middle = (arr.length + lengthQ3 - 1) / 2;
// Debugging use console.log("median index Q3 ", middle, " is ", arr[middle]);
// Code to actually set q3: q3 = arr[middle];
q3 = middle;
}
/*
console.log("q3 ", q3, "\n\n");
console.log(arr);
console.log("median ", median);
console.log("q1 ", q1);
console.log("q3 ", q3);
*/
return[q1, q3];
}
}