Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions big_o_exercise/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,48 @@

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

Determine the time and space complexities for each of the following functions. If you're not sure what these functions do, copy and paste them into the console and experiment with different inputs!


```js
// 1.
// 1. time = O(n) space = O(1)

function logUpTo(n) {
for (var i = 1; i <= n; i++) {
console.log(i);
}
}

// 2.
// 2. time = O(1) space = O(1)

function logAtMost10(n) {
for (var i = 1; i <= Math.min(n, 10); i++) {
console.log(i);
}
}

// 3.
// 3. time = O(n) space = O(1)

function logAtLeast10(n) {
for (var i = 1; i <= Math.max(n, 10); i++) {
console.log(i);
}
}

// 4.
// 4. time = O(n) space = O(n)

function onlyElementsAtEvenIndex(array) {
var newArray = Array(Math.ceil(array.length / 2));
Expand All @@ -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);
Expand Down
27 changes: 24 additions & 3 deletions recursion_exercise/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand 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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -84,4 +105,4 @@ stringifyNumbers()
/*/
```

Complete [this](https://www.codewars.com/kata/mutual-recursion/train/javascript) codewars problem!
Complete [this](https://www.codewars.com/kata/mutual-recursion/train/javascript) codewars problem!
75 changes: 75 additions & 0 deletions recursion_exercise/recursion.js
Original file line number Diff line number Diff line change
@@ -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;
}
12 changes: 11 additions & 1 deletion recursion_exercise/recursionSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', '!'])
});
});

Expand Down Expand Up @@ -168,4 +178,4 @@ describe("#stringifyNumbers", function(){
}
expect(stringifyNumbers(obj)).to.deep.equal(answer)
});
});
});