forked from jeremytoce/RecursionHell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhh_recursion.js
More file actions
447 lines (278 loc) · 9.01 KB
/
Copy pathhh_recursion.js
File metadata and controls
447 lines (278 loc) · 9.01 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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/// FACTORIAL COMPUTATION
// Create a function that computes the factorial of a number n. A factorial
// is the product of the integers from 1 through n. For example:
// factorial(5) ==> 120 (1 * 2 * 3 * 4 * 5)
var factorial = function (n) {
// Termination Case
if (n <= 1) {
return 1;
}
// Recursive Case
else {
return n * factorial(n - 1);
}
};
// console.log(factorial(5));
/// PALINDROME DETECTOR
// Create a function that detects if the provided string is a palindrome
// (a word that is identical in the reverse order.) Assume single letters
// are considered palindromes. The output should be either true or false.
var palindrome = function(str) {
str = str.replace(/\s/g, '').toLowerCase();
str1 = arguments[1] || str.slice(0, Math.floor(str.length / 2));
str2 = arguments[2] || str.slice(Math.ceil(str.length / 2), str.length);
n = arguments[3] || str2.length;
// Termination Case
if (n <= 1 ) {
return true;
}
// Recursive Case
if (str1[str1.length - n] === str2[n - 1]) {
return palindrome(str, str1, str2, n - 1);
} else {
return false;
}
}
// console.log(palindrome("hannah"));
// console.log(palindrome("a Man a plan a canal panama"));
// console.log(palindrome("negative"));
// console.log(palindrome("stiLL no good"));
/// STRING REVERSE
// Write a function that recursively reverses a string, i.e.:
// 'testing' => 'gnitset'
function stringReverse(str) {
var newStr = arguments[1] || "";
// Termination Case
if (newStr.length === str.length) {
return newStr;
}
// Base Case
else {
newStr += str[str.length - newStr.length - 1];
return stringReverse(str, newStr);
}
}
//console.log(stringReverse("testing"));
/// RECURSIVE MAP
// Recursively implement a map function. For example:
// console.log(map(['a','b','c'],function(value) {
// return value.toUpperCase();
// }));
// => [ 'A', 'B', 'C' ]
function map(arr) {
var newArr = arguments[2] || [];
var iterator = arguments[1] || function(value) {return value;}
if (newArr.length === arr.length) {
return newArr;
} else {
newArr.push(iterator(arr[newArr.length]));
return map(arr, iterator, newArr);
}
}
/// GREATEST COMMON DIVISOR
// Write an algorithm to find the greatest common divisor (gcd)
// of two positive numbers.
function gcd(x, y) {
// stop when you reach the lesser of the two numbers
var max = arguments[2] || 1;
var n = arguments[3] || 1;
// Termination case
if (n > Math.min(x,y)) {
return max;
}
// Base case
else {
if (x % n === 0 && y % n === 0) { max = n; }
return gcd(x, y, max, n + 1);
}
}
// console.log(gcd(5, 20));
// console.log(gcd(11, 33));
// console.log(gcd(12, 18));
/// NUMBERS BETWEEN
// Recursively implement a function that returns an array of the
// numbers in between x and y.
// x = 4, y = 8
// => [5,6,7]
function inBetween(x, y) {
var newArr = arguments[2] || [];
// termination case
if (x + 1 === y) {
return newArr;
} else {
newArr.push(x + 1);
return inBetween(x + 1, y, newArr);
}
}
console.log(inBetween(4, 8));
console.log(inBetween(-5, 10));
/*
/// ARRAY SUM
// Compute the sum of all of the numbers in an array. Assume there is
// no array depth.
// [5,2,6]
// => 13
function arraySum(arr) {
// Your code here
}
/// EXPONENT CALCULATOR
// Find the exponent of a number when the power is provided.
// 8^2
// => 64
function exp(base, exp) {
// Your code here
}
/// FIBONACCI SEQUENCE
// Write a recursive function to find the first n fibonacci numbers.
/// FIBONACCI SEQUENCE
// Write a recursive function to find the first n fibonacci numbers.
function fib(n) {
// Your code here
}
/// EVENS CHECKER
// Write a recursive function that determines if a number n is even.
function even(n) {
}
/// MERGE SORT
// Write a merge sort program in JavaScript.
function mergeSort(n) {
// Your code here
}
/// NUMBERS TO TEXT
// Write a function that takes a string as an imput and transforms
// all single-digit numbers to their respective text forms.
// "I have 5 dollars"
// => "I have five dollars"
function numToText(s) {
// Your code here
}
/// RECURSIVE MULTIPLICATION
// Construct a function that uses only addition and subtraction
// to find the product of two numbers.
// (5,6)
// => 30
function mul(x, y) {
//Your code here
}
/// RECURSIVE DIVISION
// 13. Write a function that divides two numbers without using the / operator or
// JavaScript's Math object.
var divide = function(x, y) {
};
/// POWER OF TWO
// Determine if a number is a power of two.
// powerOfTwo(1); // true
// powerOfTwo(16); // true
// powerOfTwo(10); // false
var powerOfTwo = function(n) {
};
/// MODULO
// Write a function that returns the remainder of x divided by y without using the
// modulo (%) operator.
// modulo(5,2) // 1
// modulo(17,5) // 2
// modulo(22,6) // 4
var modulo = function(x, y) {
};
/// STRING COMPARISON
// Write a function that compares each character of two strings and returns true if
// both are identical.
// compareStr('house', 'houses') // false
// compareStr('', '') // true
// compareStr('tomato', 'tomato') // true
var compareStr = function(str1, str2) {
};
/// ARRAY REVERSE
// Reverse the order of an array.
var reverseArr = function (array) {
};
/// ARRAY CREATOR
// Create a new array with a given value and length.
// buildList(0,5) // [0,0,0,0,0]
// buildList(7,3) // [7,7,7]
var buildList = function(value, length) {
};
/// OCCURENCE COUNT
// Count the occurence of a value inside a list.
// countOccurrence([2,7,4,4,1,4], 4) // 3
// countOccurrence([2,'banana',4,4,1,'banana'], 'banana') // 2
var countOccurrence = function(array, value) {
};
/// KEY IN OBJECT OCCURENCE COUNTER
// Write a function that counts the number of times a key occurs in an object.
// var testobj = {'e': {'x':'y'}, 't':{'r': {'e':'r'}, 'p': {'y':'r'}},'y':'e'};
// countKeysInObj(testobj, 'r') // 1
// countKeysInObj(testobj, 'e') // 2
var countKeysInObj = function(obj, key) {
};
/// VALUE IN OBJECT OCCURENCE COUNTER
// Write a function that counts the number of times a value occurs in an object.
// var testobj = {'e': {'x':'y'}, 't':{'r': {'e':'r'}, 'p': {'y':'r'}},'y':'e'};
// countValuesInObj(testobj, 'r') // 2
// countValuesInObj(testobj, 'e') // 1
var countValuesInObj = function(obj, value) {
};
/// KEY IN OBJECT RENAMER
// Find all keys in an object (and nested objects) by a provided name and rename
// them to a provided new name while preserving the value stored at that key.
var replaceKeysInObj = function(obj, key, newKey) {
};
/// ARRAY CAPITALIZER
// Given an array of words, return a new array containing each word capitalized.
// var words = ['i', 'am', 'learning', 'recursion'];
// capitalizedWords(words); // ['I', 'AM', 'LEARNING', 'RECURSION']
var capitalizeWords = function(input) {
};
/// ARRAY CAPITALIZER PT. II
// Given an array of strings, capitalize the first letter of each index.
// capitalizeFirst(['car', 'poop', 'banana']); // ['Car', 'Poop', 'Banana']
var capitalizeFirst = function(array) {
};
/// SUM EVEN IN NESTED OBJECT
// Return the sum of all even numbers in an object containing nested objects.
// var obj1 = {
// a: 2,
// b: {b: 2, bb: {b: 3, bb: {b: 2}}},
// c: {c: {c: 2}, cc: 'ball', ccc: 5},
// d: 1,
// e: {e: {e: 2}, ee: 'car'}
// };
// nestedEvenSum(obj1); // 10
var nestedEvenSum = function(obj) {
};
/// FLATTEN AN ARRAY
// Flatten an array containing nested arrays.
// Example: flatten([1,[2],[3,[[4]]],5]); // [1,2,3,4,5]
var flatten = function(arrays) {
};
/// LETTER TALLY
//Given a string, return an object containing tallies of each letter.
// letterTally('potato'); // {'p':1, 'o':2, 't':2, 'a':1}
var letterTally = function(str, obj) {
};
/// ELIMINATE CONSECUTIVE DUPLICATES
// Eliminate consecutive duplicates in a list. If the list contains repeated
// elements they should be replaced with a single copy of the element. The order of the
// elements should not be changed.
// Example: compress([1, 2, 2, 3, 4, 4, 5, 5, 5]) // [1, 2, 3, 4, 5]
// Example: compress([1, 2, 2, 3, 4, 4, 2, 5, 5, 5, 4, 4]) // [1, 2, 3, 4, 2, 5, 4]
var compress = function(list) {
};
/// REDUCE ZERO SERIES
// Reduce a series of zeroes to a single 0.
// minimizeZeroes([2,0,0,0,1,4]) // [2,0,1,4]
// minimizeZeroes([2,0,0,0,1,0,0,4]) // [2,0,1,0,4]
var minimizeZeroes = function(array) {
};
/// SIGN ALTERNATION
// Alternate the numbers in an array between positive and negative regardless of
// their original sign. The first number in the index always needs to be positive.
// alternateSign([2,7,8,3,1,4]) // [2,-7,8,-3,1,-4]
// alternateSign([-2,-7,8,3,-1,4]) // [2,-7,8,-3,1,-4]
var alternateSign = function(array) {
};
// http://kevvv.in/untitledrecursion-in-javascript/
// http://www.w3resource.com/javascript-exercises/javascript-recursion-functions-exercises.php
// https://www.khanacademy.org/computing/computer-science/algorithms/recursive-algorithms/a/recursion
https://github.com/JS-Challenges/recursion-prompts/blob/master/src/recursion.js
*/