diff --git a/big_o_exercise/readme.md b/big_o_exercise/readme.md index c732b1d..3cd95b3 100644 --- a/big_o_exercise/readme.md +++ b/big_o_exercise/readme.md @@ -4,16 +4,16 @@ Simplify the following big O expressions as much as possible: -1. `O(n + 10)` -2. `O(100 * n)` -3. `O(25)` -4. `O(n^2 + n^3)` -5. `O(n + n + n + n)` -6. `O(1000 * log(n) + n)` -7. `O(1000 * n * log(n) + n)` -8. `O(2^n + n^2)` -9. `O(5 + 3 + 1)` -10. `O(n + n^(1/2) + n^2 + n * log(n)^10)` +1. `O(n + 10)` === O(n) +2. `O(100 * n)` === O(n) +3. `O(25)` === O(1) +4. `O(n^2 + n^3)` === O(n^3) +5. `O(n + n + n + n)`=== O(n) +6. `O(1000 * log(n) + n)` === O(log n) +7. `O(1000 * n * log(n) + n)` === O(n log n) +8. `O(2^n + n^2)` === O(2^n) +9. `O(5 + 3 + 1)` === O(1) +10. `O(n + n^(1/2) + n^2 + n * log(n)^10)` === O(n^2) ### Part 2 @@ -21,7 +21,7 @@ Determine the time and space complexities for each of the following functions. I ```js -// 1. +// 1. time = O(n) space = O(1) function logUpTo(n) { for (var i = 1; i <= n; i++) { @@ -29,7 +29,7 @@ function logUpTo(n) { } } -// 2. +// 2. time = O(1) space = O(1) function logAtMost10(n) { for (var i = 1; i <= Math.min(n, 10); i++) { @@ -37,7 +37,7 @@ function logAtMost10(n) { } } -// 3. +// 3. time = O(n) space = O(1) function logAtLeast10(n) { for (var i = 1; i <= Math.max(n, 10); i++) { @@ -45,7 +45,7 @@ function logAtLeast10(n) { } } -// 4. +// 4. time = O(n) space = O(n) function onlyElementsAtEvenIndex(array) { var newArray = Array(Math.ceil(array.length / 2)); @@ -57,7 +57,7 @@ function onlyElementsAtEvenIndex(array) { return newArray; } -// 5. +// 5. time = O(n^2) space = O(n) function subtotals(array) { var subtotalArray = Array(array.length); diff --git a/recursion_exercise/readme.md b/recursion_exercise/readme.md index da02ec5..8b98e63 100644 --- a/recursion_exercise/readme.md +++ b/recursion_exercise/readme.md @@ -2,7 +2,7 @@ ## Getting started -For this exercise you **MUST** use recursion to solve these problems. Some of them can be done without, but it is essential that you practice recursion and make the tests pass. +For this exercise you **MUST** use recursion to solve these problems. Some of them can be done without, but it is essential that you practice recursion and make the tests pass. - Write a function called `productOfArray` which takes in an array of numbers and returns the product of them all @@ -11,6 +11,27 @@ productOfArray([1,2,3]) // 6 productOfArray([1,2,3,10]) // 60 ``` +- Write a function called `collectStrings` that searches for a string values in a nested object. It returns an array of the values. + +```javascript +var obj = { + stuff: "foo", + data: { + val: { + thing: { + info: "bar", + moreInfo: { + evenMoreInfo: { + weMadeIt: "baz" + } + } + } + } + } +} + +collectStrings(obj); //["foo", "bar", "baz"] +``` - Write a function called `contains` that searches for a value in a nested object. It returns true if the object contains that value. ```javascript @@ -47,7 +68,7 @@ search([1,2,3,4,5],5) // 4 search([1,2,3,4,5],15) // -1 ``` -- Refactor your search function to use a faster algorithm called binary search [https://www.youtube.com/watch?v=JQhciTuD3E8](https://www.youtube.com/watch?v=JQhciTuD3E8). +- Refactor your search function to use a faster algorithm called binary search [https://www.youtube.com/watch?v=JQhciTuD3E8](https://www.youtube.com/watch?v=JQhciTuD3E8). ```javascript binarySearch([1,2,3,4,5],5) // 4 @@ -84,4 +105,4 @@ stringifyNumbers() /*/ ``` -Complete [this](https://www.codewars.com/kata/mutual-recursion/train/javascript) codewars problem! \ No newline at end of file +Complete [this](https://www.codewars.com/kata/mutual-recursion/train/javascript) codewars problem! diff --git a/recursion_exercise/recursion.js b/recursion_exercise/recursion.js index e69de29..4be0114 100644 --- a/recursion_exercise/recursion.js +++ b/recursion_exercise/recursion.js @@ -0,0 +1,75 @@ +function productOfArray(arr) { + if(arr.length === 1) return arr[0]; + else { + return productOfArray(arr.slice(1)) * arr[0]; + } +} + +function collectStrings(obj) { + var strlist = []; + + for(let key in obj){ + if(typeof obj[key] === 'string') strlist.push(obj[key]); + else if(typeof obj[key] === 'object') { + strlist = strlist.concat(collectStrings(obj[key])); + } + } + + return strlist; +} + +function contains(obj, val) { + + for(let key in obj){ + if(obj[key] === val) return true; + else if(typeof obj[key] === 'object') { + if(contains(obj[key], val)) return true; + } + } + + return false; +} + +function search(arr, val) { + if(arr[0] === val) return 0; + else if(arr.length > 1){ + let index = search(arr.slice(1), val); + if(index > -1) return index + 1; + } + + return -1 +} + +function binarySearch(arr, val) { + + function bihelp(a, v, lo, hi) { + if(hi < lo) return -1; + + let mid = Math.floor((hi-lo)/2) + lo; + if(val < a[mid]) return bihelp(a, v, lo, mid-1); + else if(val > a[mid]) return bihelp(a, v, mid+1, hi); + else return mid; + + } + + return bihelp(arr, val, 0, arr.length-1); +} + +function stringifyNumbers(obj) { + var newObj = {}; + if(Array.isArray(obj)) newObj = []; + + for(let key in obj){ + + if(typeof obj[key] === 'number') { + newObj[key] = ('' + obj[key]); + } else if(typeof obj[key] === 'object') { + newObj[key] = stringifyNumbers(obj[key]); + } else { + newObj[key] = obj[key]; + } + + } + + return newObj; +} diff --git a/recursion_exercise/recursionSpec.js b/recursion_exercise/recursionSpec.js index 67bd73a..985f3bc 100644 --- a/recursion_exercise/recursionSpec.js +++ b/recursion_exercise/recursionSpec.js @@ -25,7 +25,17 @@ describe("#collectStrings", function(){ } } } + var obj2 = { + key1: 'hello', + key2: 34, + key5: { + key1: 'beautiful'}, + key3: [1,2,3,{ + key1: 'world'}], + key4: '!'} expect(collectStrings(obj)).to.deep.equal(["foo", "bar", "baz"]) + expect(collectStrings(obj2)) + .to.deep.equal(['hello', 'beautiful', 'world', '!']) }); }); @@ -168,4 +178,4 @@ describe("#stringifyNumbers", function(){ } expect(stringifyNumbers(obj)).to.deep.equal(answer) }); -}); \ No newline at end of file +});