From bb3e0d6bfabbac55da52c9725f4ed569f7cdbdb8 Mon Sep 17 00:00:00 2001 From: Mike Smith <89040888+smiggiddy@users.noreply.github.com> Date: Thu, 25 Jan 2024 13:12:29 -0500 Subject: [PATCH 1/3] feat: recursive sorting --- csci/fib.js | 39 +++++++++++++++++++++++++++++++++++++++ csci/mergeSort.js | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 csci/fib.js create mode 100644 csci/mergeSort.js diff --git a/csci/fib.js b/csci/fib.js new file mode 100644 index 0000000..51b7623 --- /dev/null +++ b/csci/fib.js @@ -0,0 +1,39 @@ +function loopFib(seq) { + let first = 0; + let second = 1; + let final = []; + + if (seq < 1) return first; + + for (let i = 1; i < seq; i++) { + if (first === 0) { + final.push(first); + first = 1; + } else { + let temp = second; + second = first + second; + first = temp; + } + final.push(first); + } + + console.log(final); +} + +function recursiveFib(seq) { + if (seq === 1) { + return [0]; + } else if (seq === 2) { + // if (seq < 2) { + return [0, 1]; + } else { + let arr = recursiveFib(seq - 1); + arr.push(arr[arr.length - 1] + arr[arr.length - 2]); + return arr; + } +} + +let num = process.argv[2]; + +loopFib(num); +console.log(recursiveFib(num)); diff --git a/csci/mergeSort.js b/csci/mergeSort.js new file mode 100644 index 0000000..2c1b8ba --- /dev/null +++ b/csci/mergeSort.js @@ -0,0 +1,33 @@ +function mergeSort(arr) { + if (arr.length <= 1) { + return arr; + } else { + let half = Math.round(arr.length / 2); + let leftSide = mergeSort(arr.slice(0, half)); + let rightSide = mergeSort(arr.slice(half, arr.length)); + + let newArr = mergeArray(leftSide, rightSide); + + return newArr; + } +} + +function mergeArray(left, right) { + let sorted = []; + + while (left.length && right.length) { + if (left[0] < right[0]) { + sorted.push(left.shift()); + } else { + sorted.push(right.shift()); + } + } + return sorted.concat(left, right); +} +let testCaseOne = [2, 1, 3, 9, 4, 5, 23, 10]; + +let testCaseTWo = [23, 5, 3, 1, 8, 7, 2, 4]; +console.log(`Test Case No1: ${testCaseOne}`); +console.log(mergeSort(testCaseOne)); +console.log(`Test Case No2: ${testCaseTWo}`); +console.log(mergeSort(testCaseTWo)); From 0a61e2153249713c598ffeb8edfe983a253ad4bb Mon Sep 17 00:00:00 2001 From: Mike Smith <89040888+smiggiddy@users.noreply.github.com> Date: Sun, 28 Jan 2024 20:12:25 -0500 Subject: [PATCH 2/3] feat: linked list project --- csci/linkedList.js | 211 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 211 insertions(+) create mode 100644 csci/linkedList.js diff --git a/csci/linkedList.js b/csci/linkedList.js new file mode 100644 index 0000000..e69542a --- /dev/null +++ b/csci/linkedList.js @@ -0,0 +1,211 @@ +class LinkedList { + constructor() { + this.size = 0; + this.head = null; + this.tail = null; + } + + getHead() { + return this.head; + } + getTail() { + return this.tail; + } + + getSize() { + return this.size; + } + + append(value) { + let newNode = new Node(value); + if (this.tail == null) { + this.tail = newNode; + } else { + const temp = this.tail; + this.tail = newNode; + this.tail.nextNode = temp; + } + if (this.head == null) { + this.head = newNode; + } + + this.size += 1; + } + + prepend(value) { + let newNode = new Node(value); + if (this.head == null) { + this.head = newNode; + if (this.tail == null) { + this.tail = newNode; + } + } else { + const temp = this.head; + temp.nextNode = newNode; + this.head = newNode; + } + this.size += 1; + } + + at(index) { + function nodeAtIndex(index, count, node) { + if (index === count) { + return node; + } else if (node.nextNode == null) { + return null; + } else { + return nodeAtIndex(index, count + 1, node.nextNode); + } + } + + return nodeAtIndex(index, 0, this.tail); + } + pop() { + if (this.tail.nextNode == null) { + this.tail = null; + this.head = null; + } else { + this.tail = this.tail.nextNode; + } + this.size -= 1; + } + + contains(value) { + function searchNodes(node, value) { + if (node.value === value) { + return true; + } else if (node.nextNode == null) { + return false; + } else { + return searchNodes(node.nextNode, value); + } + } + + return searchNodes(this.tail, value); + } + + find(value) { + let index = 0; + function searchNodes(node, value, index) { + if (node.value === value) { + return index; + } else if (node.nextNode == null) { + return null; + } else { + return searchNodes(node.nextNode, value, index + 1); + } + } + + return searchNodes(this.tail, value, index); + } + + insertAt(value, index) { + let previousNode = null; + let newNode = new Node(value); + let currentNode = this.tail; + let count = 0; + + if (index === 0) { + this.append(value); + return; + } + + while (currentNode != null) { + if (count == index) { + if (currentNode.nextNode == null) { + this.prepend(value); + return; + } else { + let nextNode = currentNode; + previousNode.nextNode = newNode; + newNode.nextNode = nextNode; + return; + } + } + + previousNode = currentNode; + currentNode = currentNode.nextNode; + count += 1; + } + } + + removeAt(index) { + let previousNode = null; + let currentNode = this.tail; + let count = 0; + + if (index === 0) { + this.pop(); + return; + } + + while (currentNode != null) { + if (count == index) { + if (currentNode.nextNode == null) { + previousNode.nextNode = null; + return; + } else { + previousNode.nextNode = currentNode.nextNode; + return; + } + } + + previousNode = currentNode; + currentNode = currentNode.nextNode; + count += 1; + } + } + + toString() { + let nodes = []; + let current = this.tail; + + while (current != null) { + nodes.push(current.value); + current = current.nextNode; + } + + console.log(nodes.join(" -> ") + " -> null"); + } +} + +class Node { + constructor(value) { + this.value = value; + this.nextNode = null; + } +} + +let list = new LinkedList(); + +list.append("test 5"); +list.prepend("test 8"); +list.append("test 1"); +list.append("test 2"); +list.append("test 3"); +list.append("test 4"); +list.prepend("Deez Nuts"); +list.toString(); +console.log(list.find("Deez Nuts")); +console.log(list.find("test ")); +console.log(list.contains("test 3")); + +console.log(list.size); +console.log(list.at(1).value); +console.log("popping last element"); +list.pop(); +list.toString(); +list.insertAt("inserted 3", 3); +// list.insertAt("inserted 1", 1); +list.toString(); +list.insertAt("inserted 1", 1); +list.toString(); +list.removeAt(6); +console.log("Removing element 6"); +list.toString(); +list.removeAt(1); +list.removeAt(3); +list.removeAt(0); +list.removeAt(3); +console.log("Removing element 1"); +list.toString(); From 73b1aea699be9fffb4a98a4a038c6c9929a07cd3 Mon Sep 17 00:00:00 2001 From: Mike Smith <89040888+smiggiddy@users.noreply.github.com> Date: Mon, 29 Jan 2024 09:41:28 -0500 Subject: [PATCH 3/3] chore: update gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index fd72422..46e2e6a 100644 --- a/.gitignore +++ b/.gitignore @@ -129,3 +129,4 @@ dist .yarn/install-state.gz .pnp.* +.DS_STORE