From 3582e68daece6db3690cfe1feb69743542ee13ec Mon Sep 17 00:00:00 2001 From: Kyle Wehrung Date: Sat, 29 Apr 2023 04:54:07 -0600 Subject: [PATCH 01/10] definitely not scared of recursion --- .../javascript/recursive_count.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/02-week-2--recursion/01-day-1--recursive-counting/javascript/recursive_count.js b/02-week-2--recursion/01-day-1--recursive-counting/javascript/recursive_count.js index d995b9c7..d2fc4fd3 100644 --- a/02-week-2--recursion/01-day-1--recursive-counting/javascript/recursive_count.js +++ b/02-week-2--recursion/01-day-1--recursive-counting/javascript/recursive_count.js @@ -1,5 +1,10 @@ function recursiveCount(num = 0) { - // type your code here + if (num <= 9) { + console.log(num) + + + recursiveCount(num + 1) + } } if (require.main === module) { @@ -11,3 +16,7 @@ module.exports = recursiveCount; // OPTIONAL // Please add your pseudocode to this file // And a written explanation of your solution + +//I make an if statement to establish the base count. +//I then console.log(num) to print num as it returns up the stack +//I make the recursive call of recursiveCount(num + 1) From 69f1b26dd5a7450205dcd068f215ced399e49c78 Mon Sep 17 00:00:00 2001 From: Kyle Wehrung Date: Sat, 29 Apr 2023 05:40:25 -0600 Subject: [PATCH 02/10] ooohh fun stuff --- .../javascript/recursive_search.js | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/02-week-2--recursion/02-day-2--recursive-search/javascript/recursive_search.js b/02-week-2--recursion/02-day-2--recursive-search/javascript/recursive_search.js index 7ff60062..582d060f 100644 --- a/02-week-2--recursion/02-day-2--recursive-search/javascript/recursive_search.js +++ b/02-week-2--recursion/02-day-2--recursive-search/javascript/recursive_search.js @@ -1,5 +1,15 @@ function recursiveSearch(arr, target) { - // type your code here + if (arr.length === 0) { + return false + } + const currentItem = arr[0] + if (currentItem === target) { + return true + } + + const remainingItems = arr.slice(1) + return recursiveSearch(remainingItems, target) + } if (require.main === module) { @@ -16,4 +26,13 @@ if (require.main === module) { module.exports = recursiveSearch; // Please add your pseudocode to this file + +//if array.length = 0 +//return false +//if element[i] in array = target +//return true +//someVariable = array.slice +//recursiveSearch(someVariable, target) + + // And a written explanation of your solution From 44d01661e7422d20d193c5ec74c8de4375d296d1 Mon Sep 17 00:00:00 2001 From: Kyle Wehrung Date: Sat, 29 Apr 2023 22:37:48 -0600 Subject: [PATCH 03/10] fun! --- .../javascript/find_shortest_string_recursive.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/02-week-2--recursion/04-day-4--recursive-find-shortest-string/javascript/find_shortest_string_recursive.js b/02-week-2--recursion/04-day-4--recursive-find-shortest-string/javascript/find_shortest_string_recursive.js index 92c47e89..45553f0f 100644 --- a/02-week-2--recursion/04-day-4--recursive-find-shortest-string/javascript/find_shortest_string_recursive.js +++ b/02-week-2--recursion/04-day-4--recursive-find-shortest-string/javascript/find_shortest_string_recursive.js @@ -1,5 +1,12 @@ function findShortestStringRecursive(arr) { - // type your code here + if (arr.length === 1) { + return arr[0] + } + + const result = findShortestStringRecursive(arr.slice(1)) + + return arr[0].length <= result.length ? arr[0] : result + } if (require.main === module) { From 6a0e9b22f8f804da4296a90fb1e8852e08bbe212 Mon Sep 17 00:00:00 2001 From: Kyle Wehrung Date: Sun, 30 Apr 2023 00:01:27 -0600 Subject: [PATCH 04/10] recursion? less scary? --- .../javascript/selection_sort_recursive.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/02-week-2--recursion/05-day-5--recursive-selection-sort/javascript/selection_sort_recursive.js b/02-week-2--recursion/05-day-5--recursive-selection-sort/javascript/selection_sort_recursive.js index bf1c20b6..de7540ae 100644 --- a/02-week-2--recursion/05-day-5--recursive-selection-sort/javascript/selection_sort_recursive.js +++ b/02-week-2--recursion/05-day-5--recursive-selection-sort/javascript/selection_sort_recursive.js @@ -1,5 +1,15 @@ function selectionSortRecursive(arr) { - // type your code here +if (arr.length < 1) { + return [] +} + + const smallestNum = Math.min(...arr) + const index = arr.indexOf(smallestNum) + arr.splice(index, 1) + const result = selectionSortRecursive(arr) + result.unshift(smallestNum) + return result + } if (require.main === module) { From ffc36c6744cc965a9556fcc4e86362bcdc14519a Mon Sep 17 00:00:00 2001 From: Kyle Wehrung Date: Sun, 30 Apr 2023 00:04:30 -0600 Subject: [PATCH 05/10] good times --- .../javascript/recursive_search.js | 18 +++++++++++++----- .../javascript/fibonacci_recursive.js | 7 ++++++- .../find_shortest_string_recursive.js | 4 ++-- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/02-week-2--recursion/02-day-2--recursive-search/javascript/recursive_search.js b/02-week-2--recursion/02-day-2--recursive-search/javascript/recursive_search.js index 582d060f..04238d31 100644 --- a/02-week-2--recursion/02-day-2--recursive-search/javascript/recursive_search.js +++ b/02-week-2--recursion/02-day-2--recursive-search/javascript/recursive_search.js @@ -2,13 +2,13 @@ function recursiveSearch(arr, target) { if (arr.length === 0) { return false } - const currentItem = arr[0] - if (currentItem === target) { + + if (arr[0] === target) { return true } - - const remainingItems = arr.slice(1) - return recursiveSearch(remainingItems, target) + + return recursiveSearch(arr.slice(1), target) + } @@ -25,6 +25,14 @@ if (require.main === module) { module.exports = recursiveSearch; + + + + + + + + // Please add your pseudocode to this file //if array.length = 0 diff --git a/02-week-2--recursion/03-day-3--recursive-fibonacci-series/javascript/fibonacci_recursive.js b/02-week-2--recursion/03-day-3--recursive-fibonacci-series/javascript/fibonacci_recursive.js index 7d118da6..016d817f 100644 --- a/02-week-2--recursion/03-day-3--recursive-fibonacci-series/javascript/fibonacci_recursive.js +++ b/02-week-2--recursion/03-day-3--recursive-fibonacci-series/javascript/fibonacci_recursive.js @@ -1,5 +1,10 @@ function fibonacci(n) { - // type your code here + if (n < 2) { + return n + } + + return fibonacci(n - 1) + fibonacci(n - 2) + } if (require.main === module) { diff --git a/02-week-2--recursion/04-day-4--recursive-find-shortest-string/javascript/find_shortest_string_recursive.js b/02-week-2--recursion/04-day-4--recursive-find-shortest-string/javascript/find_shortest_string_recursive.js index 45553f0f..ed87ed75 100644 --- a/02-week-2--recursion/04-day-4--recursive-find-shortest-string/javascript/find_shortest_string_recursive.js +++ b/02-week-2--recursion/04-day-4--recursive-find-shortest-string/javascript/find_shortest_string_recursive.js @@ -4,8 +4,8 @@ function findShortestStringRecursive(arr) { } const result = findShortestStringRecursive(arr.slice(1)) - - return arr[0].length <= result.length ? arr[0] : result + + return arr[0] <= result.length ? arr[0] : result } From 4fa23e36eacb2df1fd26ba3803022fccdf1f334a Mon Sep 17 00:00:00 2001 From: Kyle Wehrung Date: Sun, 30 Apr 2023 02:00:20 -0600 Subject: [PATCH 06/10] Getting better? --- .../javascript/stack.js | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/06-week-5--big-o-continued/00-days-1-to-2--implement-a-stack-class/javascript/stack.js b/06-week-5--big-o-continued/00-days-1-to-2--implement-a-stack-class/javascript/stack.js index 62df33d9..89154862 100644 --- a/06-week-5--big-o-continued/00-days-1-to-2--implement-a-stack-class/javascript/stack.js +++ b/06-week-5--big-o-continued/00-days-1-to-2--implement-a-stack-class/javascript/stack.js @@ -9,46 +9,58 @@ class Stack { // add item to top of stack if not full // if full throw error push(item) { - + if (!this.isFull()) { + this.stack.push(item) + } else { + throw new Error("Stack is full!") + } } // remove item from top of stack and return it pop() { - + return this.stack.pop() } // return item at top of stack without removing it peek() { - + return this.stack[this.size() -1] } // return true if stack is empty, otherwise false isEmpty() { - + return this.size() === 0 } // return true if stack is full, otherwise false isFull() { - + return this.size() === this.limit } // return number of items in stack size() { - + return this.stack.length } // return -1 if item not in stack, otherwise integer representing // how far it is from the top search(target) { + for (let i = -1; i >= -this.size(); --i) { + if (this.stack[this.size() + i] === target) { + return Math.abs(i) - 1; + } + } + return -1; } + // print contents of stack: do not return the stack itself! print() { - + console.log(this.stack.join(' <- ')) } } + if (require.main === module) { // add your own tests in here } From 288e34bc1c24a99716ad354b62a580f0024d929f Mon Sep 17 00:00:00 2001 From: Kyle Wehrung Date: Wed, 10 May 2023 16:49:04 -0600 Subject: [PATCH 07/10] Queuesgit add . --- .../javascript/queue.js | 30 ++++++++++++++----- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/06-week-5--big-o-continued/01-days-3-to-4--implement-a-queue-class/javascript/queue.js b/06-week-5--big-o-continued/01-days-3-to-4--implement-a-queue-class/javascript/queue.js index 06654e80..583e9202 100644 --- a/06-week-5--big-o-continued/01-days-3-to-4--implement-a-queue-class/javascript/queue.js +++ b/06-week-5--big-o-continued/01-days-3-to-4--implement-a-queue-class/javascript/queue.js @@ -9,43 +9,59 @@ class Queue { // add item to rear of queue if not full // if full throw error enqueue(item) { + if (!this.isFull()) { + this.queue.push(item) + } else { + throw new Error("Queue is full!") + } } + // remove item from front of queue and return it dequeue() { - + return this.queue.shift() } + // return item at front of queue without removing it peek() { - + return this.queue[0] } // return true if queue is empty, otherwise false isEmpty() { - + if (this.size() === 0) { + return true + } else { + return false + } } // return true if queue is full, otherwise false isFull() { - + return this.size() === this.limit } // return number of items in queue size() { - + return this.queue.length } // return -1 if item not in queue, otherwise integer representing // how far it is from the front search(target) { - + if( this.queue.indexOf(target) > -1) { + return this.queue.indexOf(target) + } else { + return -1 + } + } // print contents of queue: do not return the queue itself! print() { - + console.log(this.queue.join(' <- ')) } } From d68c7241489d66a9d7d10949b77ba818549423ba Mon Sep 17 00:00:00 2001 From: Kyle Wehrung Date: Thu, 11 May 2023 13:13:15 -0600 Subject: [PATCH 08/10] Mostly makes sense --- .../javascript/my_set.js | 33 ++++++++++++++++--- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/06-week-5--big-o-continued/02-day-5--implement-a-set/javascript/my_set.js b/06-week-5--big-o-continued/02-day-5--implement-a-set/javascript/my_set.js index 21032e5e..f6f30905 100644 --- a/06-week-5--big-o-continued/02-day-5--implement-a-set/javascript/my_set.js +++ b/06-week-5--big-o-continued/02-day-5--implement-a-set/javascript/my_set.js @@ -3,38 +3,61 @@ class MySet { // if an iterable is provided only its unique values should be in data // strings and arrays will need to be broken down by their elements/characters constructor(iterable) { + if (!(iterable === undefined || + Array.isArray(iterable) || + typeof iterable === "string")) { + throw new Error("MySet only accepts iterable or nothing on initialization") + } + + + this.data = {}; + + if (iterable) { + for (const e of iterable) { + this.data[e] = true + } + } + + + } // return number of elements in MySet size() { - + return this.entries().length } // add an item to MySet as is // don't worry about arrays here! // return the MySet instance add(item) { - + this.data[item] = true + return this } // delete an item from MySet // don't worry about arrays here! // return true if successful, otherwise false delete(item) { + if (this.has(item)) { + delete this.data[item] + return true + } else { + return false + } } - // return true if in MySet, otherwise false // don't worry about arrays here! has(item) { - + return !!this.data[item] } // return data as an array // don't worry about arrays here! entries() { - + return Object.keys(this.data) } } From 165bbbb16b8d54d9a6badb4adb8326928beb8668 Mon Sep 17 00:00:00 2001 From: Kyle Wehrung Date: Wed, 31 May 2023 16:29:19 -0600 Subject: [PATCH 09/10] slowly learning it --- .../javascript/binary_tree.js | 29 +++++++++++++++---- 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/10-week-8--searching/01-day-4--manual-binary-tree/javascript/binary_tree.js b/10-week-8--searching/01-day-4--manual-binary-tree/javascript/binary_tree.js index 14dd9232..826fdbfe 100644 --- a/10-week-8--searching/01-day-4--manual-binary-tree/javascript/binary_tree.js +++ b/10-week-8--searching/01-day-4--manual-binary-tree/javascript/binary_tree.js @@ -1,23 +1,40 @@ class Node { - constructor() { - // add your Node class code + constructor(value, left = null, right = null) { + this.value = value; + this.left = left; + this.right = right; } } // list = [1, 4, 7] function oneToSeven() { - // manually create the BST - // then return the root node + const left = new Node(1); + const right = new Node(7); + + return new Node(4, left, right); } // list = [10, 40, 45, 46, 50] function tenToFifty() { - + const tenNode = new Node(10); + const fortyNode = new Node(40, tenNode); + const fortySixNode = new Node(46); + const fiftyNode = new Node(50, fortySixNode); + + return new Node(45, fortyNode, fiftyNode); } // list = [-20, -19, -17, -15, 0, 1, 2, 10] function negativeToPositive() { - + const nTwenty = new Node(-20); + const nSeventeen = new Node(-17); + const nNineteen = new Node(-19, nTwenty, nSeventeen); + const one = new Node(1); + const zero = new Node(0, null, one); + const ten = new Node(10); + const two = new Node(2, zero, ten); + + return new Node(-15, nNineteen, two); } if (require.main === module) { From 6b9e44809d9d20b38b11b3a5c45e93b2fe51d2cd Mon Sep 17 00:00:00 2001 From: Kyle Wehrung Date: Fri, 2 Jun 2023 13:38:54 -0600 Subject: [PATCH 10/10] math, push, slice --- .../javascript/selection_sort.js | 27 +++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/01-week-1--starter-algorithms/03-day-4--selection-sort/javascript/selection_sort.js b/01-week-1--starter-algorithms/03-day-4--selection-sort/javascript/selection_sort.js index 230c8030..61abb553 100644 --- a/01-week-1--starter-algorithms/03-day-4--selection-sort/javascript/selection_sort.js +++ b/01-week-1--starter-algorithms/03-day-4--selection-sort/javascript/selection_sort.js @@ -1,6 +1,20 @@ function selectionSort(arr) { - // type your code here -} + newArr = [] + + while (arr.length !== 0) { + minny = Math.min(...arr) + index = arr.indexOf(minny) + + newArr.push(minny) + arr.splice(index, 1) + console.log(newArr) + + } + + } + + + if (require.main === module) { // add your own tests in here @@ -20,4 +34,13 @@ if (require.main === module) { module.exports = selectionSort; // Please add your pseudocode to this file + +//create new array to push things into +//create a while loop that goes until arr is 0 +//find smallest value in array +//find index of smallest value +//push min value to new array +//remove that value using slice +//some elm = Math.min, somewhere? + // And a written explanation of your solution