-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathredo.js
305 lines (238 loc) · 6.13 KB
/
redo.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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
var permutations = function (array) {
var len = array.length;
if (len === 0) { return []; }
if (len === 1) { return [ array ]; }
var first = array.slice(0, 1);
var rest = array.slice(1);
return permutations( rest ).reduce(function (acc, cur) {
var len = cur.length;
for (var i = 0; i < len; i += 1) {
acc.push( Array.prototype.concat.call( cur.slice(0, i), first, cur.slice(i) ) );
}
acc.push( Array.prototype.concat.call( cur, first ) );
return acc;
}, []);
};
var combinations = function (array) {
var len = array.length;
var result = [];
var first, rest;
if (len === 0) { return []; }
if (len === 1) { return [ array ]; }
first = array[0];
rest = array.slice(1);
combinations( rest ).forEach(function (arr) {
result.push( arr.concat(first) );
result.push( arr );
});
result.push( [ first ] );
return result;
};
var rockPaperScissors = function (num) {
if (num <= 0) { return []; }
if (num === 1) { return [ ['rock'], ['paper'], ['scissors'] ]; }
var result = [];
rockPaperScissors(num - 1).forEach(function (arr) {
['rock', 'paper', 'scissors'].forEach(function (hand) {
result.push( Array.prototype.concat( arr, hand ) );
});
});
return result;
};
var balancedParens = function (string) {
var stack = [];
var character, popped;
var isOpening = function (character) {
return character === '(' ||
character === '[' ||
character === '{';
};
var map = {
')' : '(',
']' : '[',
'}' : '{'
};
for (var i = 0; i < string.length; i += 1) {
character = string[i];
if ( isOpening(character) ) {
stack.push( character );
} else if ( map[ character ] ) {
popped = stack.pop();
if ( map[character] !== popped ) { return false; }
}
}
return stack.length === 0;
};
var binarySearch = function (arr, num) {
var inner = function (start, end) {
var mid = Math.floor( (start + end) / 2 );
if (start >= end) { return null; }
if (num === arr[mid]) { return mid; }
if (num < arr[mid]) { return inner(start, mid); }
return inner(mid + 1, end);
};
return inner(0, arr.length);
};
var characterFrequency = function (string) {
var createHash = function (string) {
var hash = {};
for (var i = 0; i < string.length; i += 1) {
hash[ string[i] ] = hash[ string[i] ] || 0;
hash[ string[i] ] += 1;
}
return hash;
};
var createDataStructure = function (hash) {
var result = [];
for (var prop in hash) {
result.push([ prop, hash[prop] ]);
}
return result;
};
var dsHash = createDataStructure( createHash(string) );
dsHash.sort(function (a, b) {
// a and b are arrays
if (a[1] > b[1]) {
return -1;
} else if (a[1] < b[1]) {
return 1;
} else {
return a[0] >= b[0] ? 1 : -1;
}
});
return dsHash;
};
var makeChange = function (num) {
var inner = function (num, coins) {
if (num < 0) { return 0; }
if (num === 0) { return 1; }
var total = 0;
coins.forEach(function (amt) {
total += inner(num - amt, availableCoins(amt));
});
return total;
};
return inner(num, availableCoins(num));
};
var availableCoins = function (num) {
return [200, 100, 50, 20, 10, 5, 2, 1].filter(function (n) {
return num >= n;
});
};
var convertToHash = function (string) {
var hash = {};
for (var i = 0; i < string.length; i += 1) {
hash[string[i]] = true;
}
return hash;
};
var combineHash = function (hash1, hash2) {
var result = {};
for (var prop in hash1) {
if (hash2[prop]) {
result[prop] = true;
}
}
return result;
};
var commonCharacters = function () {
var args = Array.prototype.slice.call(arguments);
var first = args[0];
var result = args
.map(function (string) {
return convertToHash(string);
})
.reduce(function (acc, cur) {
return combineHash(acc, cur);
});
return first.split('')
.filter(function (character) {
var temp = result[character];
delete result[character];
return temp;
})
.join('');
};
var mixEvents = function (obj) {
var eventHash = {};
obj.on = function (e, callback) {
// check if event already exists in eventHash
eventHash[ e ] = eventHash[ e ] || [];
eventHash[ e ].push( callback );
};
obj.trigger = function (e) {
var args = Array.prototype.slice.call(arguments, 1);
var allCallbacks = eventHash[ e ];
allCallbacks.forEach(function (fn) {
fn.apply(this, args);
});
};
return obj;
};
Array.prototype.isSubsetOf = function (base) {
var thisLen = this.length;
var baseLen = base.length;
var isFound, i, j;
if (thisLen > baseLen) return false;
// 'this' array contains at most base array's number of elements
for (i = 0; i < thisLen; i += 1) {
isFound = false;
for (j = 0; j < baseLen; j += 1) {
if (this[i] === base[j]) {
isFound = true;
}
}
// check if isFound has flipped
if (!isFound) return false;
}
return true;
};
var quickSort = function (array) {
// base case
if (array.length < 2) return array;
var pivot = array.splice(0, 1)[0];
var left = [];
var right = [];
var len = array.length;
var i;
for (i = 0; i < len; i += 1) {
(array[i] <= pivot ? left : right).push( array[i] );
}
return Array.prototype.concat.call( quickSort(left), pivot, quickSort(right) );
};
var mergeSort = function (array) {
var len = array.length;
if (len < 2) return array;
var mid = Math.floor(len / 2);
var left = mergeSort( array.slice(0, mid) );
var right = mergeSort( array.slice(mid) );
return merge(array, left, right);
};
var merge = function (array, left, right) {
var leftLen = left.length;
var rightLen = right.length;
var i = 0;
var j = 0;
var k = 0;
while (j < leftLen && k < rightLen) {
if (left[j] <= right[k]) {
array[i] = left[j];
j += 1;
} else {
array[i] = right[k];
k += 1;
}
i += 1;
}
while (j < leftLen) {
array[i] = left[j];
i += 1;
j += 1;
}
while (k < rightLen) {
array[i] = right[k];
i += 1;
k += 1;
}
return array;
};