mirror of
https://gitea.smigz.com/smiggiddy/odin-codeprojects.git
synced 2024-12-25 06:00:43 -05:00
merge: csci branch
This commit is contained in:
commit
b8167603d6
7 changed files with 1924 additions and 480 deletions
13
csci/.eslintrc
Normal file
13
csci/.eslintrc
Normal file
|
@ -0,0 +1,13 @@
|
|||
module.exports = {
|
||||
env: {
|
||||
browser: true,
|
||||
es2021: true,
|
||||
node: true,
|
||||
},
|
||||
extends: ["eslint:recommended", "prettier"],
|
||||
parserOptions: {
|
||||
ecmaVersion: "latest",
|
||||
sourceType: "module",
|
||||
},
|
||||
};
|
||||
|
4
csci/.prettierrc
Normal file
4
csci/.prettierrc
Normal file
|
@ -0,0 +1,4 @@
|
|||
{
|
||||
"tabWidth": 4,
|
||||
"singleQuote": true
|
||||
}
|
|
@ -1,215 +1,236 @@
|
|||
class LinkedList {
|
||||
constructor() {
|
||||
this.size = 0;
|
||||
this.tail = null;
|
||||
this.head = null;
|
||||
}
|
||||
|
||||
|
||||
append(value) {
|
||||
let newNode = new Node(value);
|
||||
if (this.head == null) {
|
||||
this.head = newNode;
|
||||
} else {
|
||||
const temp = this.head;
|
||||
this.head = newNode;
|
||||
this.head.nextNode = temp;
|
||||
}
|
||||
if (this.tail == null) {
|
||||
this.tail = newNode;
|
||||
|
||||
constructor() {
|
||||
this.size = 0;
|
||||
this.tail = null;
|
||||
this.head = null;
|
||||
}
|
||||
|
||||
this.size += 1;
|
||||
}
|
||||
|
||||
prepend(value) {
|
||||
let newNode = new Node(value);
|
||||
|
||||
if (this.tail == null) {
|
||||
this.tail = newNode;
|
||||
if (this.head == null) {
|
||||
this.head = newNode;
|
||||
}
|
||||
} else {
|
||||
const temp = this.tail;
|
||||
temp.nextNode = newNode;
|
||||
this.tail = newNode;
|
||||
|
||||
getHead() {
|
||||
return this.tail;
|
||||
}
|
||||
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);
|
||||
}
|
||||
getTail() {
|
||||
return this.head;
|
||||
}
|
||||
|
||||
return nodeAtIndex(index, 0, this.head);
|
||||
}
|
||||
pop() {
|
||||
if (this.head.nextNode == null) {
|
||||
this.head = null;
|
||||
this.tail = null;
|
||||
} else {
|
||||
this.head = this.head.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);
|
||||
}
|
||||
getSize() {
|
||||
return this.size;
|
||||
}
|
||||
|
||||
return searchNodes(this.head, 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.head, value, index);
|
||||
|
||||
}
|
||||
|
||||
insertAt(value, index) {
|
||||
let previousNode = null;
|
||||
let newNode = new Node(value);
|
||||
let currentNode = this.head;
|
||||
|
||||
let count = 0;
|
||||
|
||||
if (index === 0) {
|
||||
this.append(value);
|
||||
return;
|
||||
}
|
||||
|
||||
while (currentNode != null) {
|
||||
if (count == index) {
|
||||
if (currentNode.nextNode == null) {
|
||||
this.prepend(value);
|
||||
return;
|
||||
append(value) {
|
||||
let newNode = new Node(value);
|
||||
if (this.head == null) {
|
||||
this.head = newNode;
|
||||
} else {
|
||||
let nextNode = currentNode;
|
||||
previousNode.nextNode = newNode;
|
||||
newNode.nextNode = nextNode;
|
||||
return;
|
||||
const temp = this.head;
|
||||
this.head = newNode;
|
||||
this.head.nextNode = temp;
|
||||
}
|
||||
if (this.tail == null) {
|
||||
this.tail = newNode;
|
||||
}
|
||||
}
|
||||
|
||||
previousNode = currentNode;
|
||||
currentNode = currentNode.nextNode;
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
|
||||
removeAt(index) {
|
||||
let previousNode = null;
|
||||
let currentNode = this.head;
|
||||
|
||||
let count = 0;
|
||||
|
||||
if (index === 0) {
|
||||
this.pop();
|
||||
return;
|
||||
this.size += 1;
|
||||
}
|
||||
|
||||
while (currentNode != null) {
|
||||
if (count == index) {
|
||||
if (currentNode.nextNode == null) {
|
||||
previousNode.nextNode = null;
|
||||
return;
|
||||
prepend(value) {
|
||||
let newNode = new Node(value);
|
||||
if (this.tail == null) {
|
||||
this.tail = newNode;
|
||||
if (this.head == null) {
|
||||
this.head = newNode;
|
||||
}
|
||||
} else {
|
||||
previousNode.nextNode = currentNode.nextNode;
|
||||
return;
|
||||
const temp = this.tail;
|
||||
temp.nextNode = newNode;
|
||||
this.tail = newNode;
|
||||
}
|
||||
}
|
||||
|
||||
previousNode = currentNode;
|
||||
currentNode = currentNode.nextNode;
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
|
||||
toString() {
|
||||
let nodes = [];
|
||||
let current = this.head;
|
||||
|
||||
|
||||
while (current != null) {
|
||||
nodes.push(current.value);
|
||||
current = current.nextNode;
|
||||
this.size += 1;
|
||||
}
|
||||
|
||||
console.log(nodes.join(" -> ") + " -> null");
|
||||
}
|
||||
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.head);
|
||||
}
|
||||
pop() {
|
||||
if (this.head.nextNode == null) {
|
||||
this.head = null;
|
||||
this.tail = null;
|
||||
} else {
|
||||
this.head = this.head.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.head, 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.head, value, index);
|
||||
}
|
||||
|
||||
insertAt(value, index) {
|
||||
let previousNode = null;
|
||||
let newNode = new Node(value);
|
||||
let currentNode = this.head;
|
||||
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.head;
|
||||
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.head;
|
||||
|
||||
while (current != null) {
|
||||
nodes.push(current.value);
|
||||
current = current.nextNode;
|
||||
}
|
||||
|
||||
console.log(nodes.join(' -> ') + ' -> null');
|
||||
}
|
||||
|
||||
remove(value) {
|
||||
let count = 0;
|
||||
let previousNode = null;
|
||||
let currentNode = this.head;
|
||||
|
||||
if (this.head.value === value) {
|
||||
this.pop();
|
||||
return;
|
||||
}
|
||||
|
||||
while (currentNode) {
|
||||
if (currentNode.value === value) {
|
||||
previousNode.nextNode = currentNode.nextNode;
|
||||
return;
|
||||
}
|
||||
previousNode = currentNode;
|
||||
currentNode = currentNode.nextNode;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Node {
|
||||
constructor(value) {
|
||||
this.value = value;
|
||||
this.nextNode = null;
|
||||
}
|
||||
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();
|
||||
console.log('removing inserted elements')
|
||||
list.removeAt(1);
|
||||
list.removeAt(3);
|
||||
list.toString();
|
||||
console.log("Removing element 1");
|
||||
list.removeAt(0);
|
||||
list.toString();
|
||||
console.log("Removing element Deez Nuts");
|
||||
list.removeAt(3);
|
||||
|
||||
list.toString();
|
||||
// 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();
|
||||
// console.log('removing inserted elements');
|
||||
// list.removeAt(1);
|
||||
// list.removeAt(3);
|
||||
// list.toString();
|
||||
// console.log('Removing element 1');
|
||||
// list.removeAt(0);
|
||||
// list.toString();
|
||||
// console.log('Removing element Deez Nuts');
|
||||
// list.removeAt(3);
|
||||
// list.toString();
|
||||
export { LinkedList };
|
||||
|
|
1262
csci/package-lock.json
generated
Normal file
1262
csci/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load diff
17
csci/package.json
Normal file
17
csci/package.json
Normal file
|
@ -0,0 +1,17 @@
|
|||
{
|
||||
"name": "csci",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "doublyLinkedList.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"devDependencies": {
|
||||
"eslint-config-prettier": "^9.1.0",
|
||||
"prettier": "3.2.4"
|
||||
},
|
||||
"type": "module"
|
||||
}
|
|
@ -1,266 +1,266 @@
|
|||
// https://github.com/TheOdinProject/curriculum/issues/27103
|
||||
|
||||
class Node {
|
||||
constructor(key, value) {
|
||||
this.key = key;
|
||||
this.value = value;
|
||||
this.next = null;
|
||||
}
|
||||
constructor(key, value) {
|
||||
this.key = key;
|
||||
this.value = value;
|
||||
this.next = null;
|
||||
}
|
||||
}
|
||||
|
||||
class LinkedList {
|
||||
constructor() {
|
||||
this.size = 0;
|
||||
this.head = null;
|
||||
this.tail = null;
|
||||
}
|
||||
|
||||
append(key, value) {
|
||||
let newNode = new Node(key, value);
|
||||
if (this.head == null) {
|
||||
this.head = newNode;
|
||||
} else {
|
||||
const temp = this.head;
|
||||
this.head = newNode;
|
||||
this.head.next = temp;
|
||||
}
|
||||
if (this.tail == null) {
|
||||
this.tail = newNode;
|
||||
}
|
||||
|
||||
this.size += 1;
|
||||
}
|
||||
pop() {
|
||||
if (this.head.next == null) {
|
||||
this.head = null;
|
||||
this.tail = null;
|
||||
} else {
|
||||
this.head = this.head.next;
|
||||
}
|
||||
this.size -= 1;
|
||||
}
|
||||
|
||||
contains(key) {
|
||||
function searchNodes(node, key) {
|
||||
if (node.key === key) {
|
||||
return true;
|
||||
} else if (node.next == null) {
|
||||
return false;
|
||||
} else {
|
||||
return searchNodes(node.next);
|
||||
}
|
||||
}
|
||||
|
||||
return searchNodes(this.head, key);
|
||||
}
|
||||
|
||||
replace(key, value) {
|
||||
function replace(node) {
|
||||
if (node.key === key) {
|
||||
node.value = value;
|
||||
} else {
|
||||
return replace(node.next);
|
||||
}
|
||||
}
|
||||
replace(this.head, key);
|
||||
}
|
||||
|
||||
find(key) {
|
||||
function searchNodes(node, key) {
|
||||
if (node.key === key) {
|
||||
return node.value;
|
||||
} else if (node.next == null) {
|
||||
return null;
|
||||
} else {
|
||||
return searchNodes(node.next, key);
|
||||
}
|
||||
}
|
||||
|
||||
return searchNodes(this.head, key);
|
||||
}
|
||||
remove(key) {
|
||||
let lastNode = this.head;
|
||||
let currentNode = this.head;
|
||||
|
||||
if (currentNode.key === key) {
|
||||
if (currentNode.next == null) {
|
||||
constructor() {
|
||||
this.size = 0;
|
||||
this.head = null;
|
||||
this.tail = null;
|
||||
} else {
|
||||
this.size -= 1;
|
||||
this.head = this.head.next;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
while (currentNode) {
|
||||
if (currentNode.key === key) {
|
||||
lastNode.next = currentNode.next;
|
||||
this.size -= 1;
|
||||
return true;
|
||||
}
|
||||
lastNode = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
append(key, value) {
|
||||
let newNode = new Node(key, value);
|
||||
if (this.head == null) {
|
||||
this.head = newNode;
|
||||
} else {
|
||||
const temp = this.head;
|
||||
this.head = newNode;
|
||||
this.head.next = temp;
|
||||
}
|
||||
if (this.tail == null) {
|
||||
this.tail = newNode;
|
||||
}
|
||||
|
||||
this.size += 1;
|
||||
}
|
||||
pop() {
|
||||
if (this.head.next == null) {
|
||||
this.head = null;
|
||||
this.tail = null;
|
||||
} else {
|
||||
this.head = this.head.next;
|
||||
}
|
||||
this.size -= 1;
|
||||
}
|
||||
|
||||
contains(key) {
|
||||
function searchNodes(node, key) {
|
||||
if (node.key === key) {
|
||||
return true;
|
||||
} else if (node.next == null) {
|
||||
return false;
|
||||
} else {
|
||||
return searchNodes(node.next);
|
||||
}
|
||||
}
|
||||
|
||||
return searchNodes(this.head, key);
|
||||
}
|
||||
|
||||
replace(key, value) {
|
||||
function replace(node) {
|
||||
if (node.key === key) {
|
||||
node.value = value;
|
||||
} else {
|
||||
return replace(node.next);
|
||||
}
|
||||
}
|
||||
replace(this.head, key);
|
||||
}
|
||||
|
||||
find(key) {
|
||||
function searchNodes(node, key) {
|
||||
if (node.key === key) {
|
||||
return node.value;
|
||||
} else if (node.next == null) {
|
||||
return null;
|
||||
} else {
|
||||
return searchNodes(node.next, key);
|
||||
}
|
||||
}
|
||||
|
||||
return searchNodes(this.head, key);
|
||||
}
|
||||
remove(key) {
|
||||
let lastNode = this.head;
|
||||
let currentNode = this.head;
|
||||
|
||||
if (currentNode.key === key) {
|
||||
if (currentNode.next == null) {
|
||||
this.size = 0;
|
||||
this.head = null;
|
||||
this.tail = null;
|
||||
} else {
|
||||
this.size -= 1;
|
||||
this.head = this.head.next;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
while (currentNode) {
|
||||
if (currentNode.key === key) {
|
||||
lastNode.next = currentNode.next;
|
||||
this.size -= 1;
|
||||
return true;
|
||||
}
|
||||
lastNode = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
class HashMap {
|
||||
constructor(capacity = 8) {
|
||||
this.initialCapacity = capacity;
|
||||
this.capacity = capacity;
|
||||
this.buckets = Array(capacity);
|
||||
this.capacity = this.buckets.length;
|
||||
this.loadFactor = 0.75;
|
||||
}
|
||||
|
||||
stringToNumber(string) {
|
||||
let hashCode = 0;
|
||||
const primeNumber = 31;
|
||||
|
||||
for (let i = 0; i < string.length; i++) {
|
||||
hashCode = primeNumber * hashCode + string.charCodeAt(i);
|
||||
constructor(capacity = 8) {
|
||||
this.initialCapacity = capacity;
|
||||
this.capacity = capacity;
|
||||
this.buckets = Array(capacity);
|
||||
this.capacity = this.buckets.length;
|
||||
this.loadFactor = 0.75;
|
||||
}
|
||||
|
||||
return hashCode;
|
||||
}
|
||||
stringToNumber(string) {
|
||||
let hashCode = 0;
|
||||
const primeNumber = 31;
|
||||
|
||||
hash(key) {
|
||||
return this.stringToNumber(key);
|
||||
}
|
||||
|
||||
capacityWatcher() {
|
||||
let consumed = this.buckets.filter(Object).length;
|
||||
if (consumed >= this.capacity * this.loadFactor) {
|
||||
this.capacity = Math.round(1.25 * this.capacity);
|
||||
}
|
||||
}
|
||||
|
||||
set(key, value) {
|
||||
let _hash = this.hash(key);
|
||||
let bucketIndex = _hash % this.capacity;
|
||||
this.capacityWatcher();
|
||||
if (this.buckets[bucketIndex] == undefined) {
|
||||
let linkedList = new LinkedList();
|
||||
linkedList.append(key, value);
|
||||
|
||||
this.buckets[bucketIndex] = linkedList;
|
||||
} else {
|
||||
let keyExists = this.buckets[bucketIndex].contains(key);
|
||||
|
||||
if (keyExists) {
|
||||
this.buckets[bucketIndex].replace(key, value);
|
||||
} else {
|
||||
this.buckets[bucketIndex].append(key, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
get(key) {
|
||||
for (let i = 0; i < this.buckets.length; i++) {
|
||||
if (this.buckets[i]) {
|
||||
let value = this.buckets[i].find(key);
|
||||
if (value) return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
has(key) {
|
||||
for (let i = 0; i < this.buckets.length; i++) {
|
||||
if (this.buckets[i]) {
|
||||
let value = this.buckets[i].find(key);
|
||||
if (value) return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
remove(key) {
|
||||
for (let i = 0; i < this.buckets.length; i++) {
|
||||
if (this.buckets[i]) {
|
||||
let removed = this.buckets[i].remove(key);
|
||||
if (this.buckets[i].size === 0) this.buckets[i] = undefined;
|
||||
if (removed) return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
length() {
|
||||
let count = 0;
|
||||
|
||||
for (let i = 0; i < this.buckets.length; i++) {
|
||||
if (this.buckets[i]) {
|
||||
count += this.buckets[i].size;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
clear() {
|
||||
this.capacity = this.initialCapacity;
|
||||
this.buckets = Array(this.capacity);
|
||||
}
|
||||
keys() {
|
||||
let _keys = [];
|
||||
|
||||
for (let i = 0; i < this.buckets.length; i++) {
|
||||
if (this.buckets[i]) {
|
||||
let currentNode = this.buckets[i].head;
|
||||
while (currentNode) {
|
||||
_keys.push(currentNode.key);
|
||||
currentNode = currentNode.next;
|
||||
for (let i = 0; i < string.length; i++) {
|
||||
hashCode = primeNumber * hashCode + string.charCodeAt(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
return _keys;
|
||||
}
|
||||
values() {
|
||||
let _values = [];
|
||||
|
||||
for (let i = 0; i < this.buckets.length; i++) {
|
||||
if (this.buckets[i]) {
|
||||
let currentNode = this.buckets[i].head;
|
||||
while (currentNode) {
|
||||
_values.push(currentNode.value);
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
}
|
||||
return hashCode;
|
||||
}
|
||||
return _values;
|
||||
}
|
||||
entries() {
|
||||
let _entries = [];
|
||||
|
||||
for (let i = 0; i < this.buckets.length; i++) {
|
||||
if (this.buckets[i]) {
|
||||
let currentNode = this.buckets[i].head;
|
||||
while (currentNode) {
|
||||
_entries.push([currentNode.key, currentNode.value]);
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
}
|
||||
hash(key) {
|
||||
return this.stringToNumber(key);
|
||||
}
|
||||
|
||||
capacityWatcher() {
|
||||
let consumed = this.buckets.filter(Object).length;
|
||||
if (consumed >= this.capacity * this.loadFactor) {
|
||||
this.capacity = Math.round(1.25 * this.capacity);
|
||||
}
|
||||
}
|
||||
|
||||
set(key, value) {
|
||||
let _hash = this.hash(key);
|
||||
let bucketIndex = _hash % this.capacity;
|
||||
this.capacityWatcher();
|
||||
if (this.buckets[bucketIndex] == undefined) {
|
||||
let linkedList = new LinkedList();
|
||||
linkedList.append(key, value);
|
||||
|
||||
this.buckets[bucketIndex] = linkedList;
|
||||
} else {
|
||||
let keyExists = this.buckets[bucketIndex].contains(key);
|
||||
|
||||
if (keyExists) {
|
||||
this.buckets[bucketIndex].replace(key, value);
|
||||
} else {
|
||||
this.buckets[bucketIndex].append(key, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
get(key) {
|
||||
for (let i = 0; i < this.buckets.length; i++) {
|
||||
if (this.buckets[i]) {
|
||||
let value = this.buckets[i].find(key);
|
||||
if (value) return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
has(key) {
|
||||
for (let i = 0; i < this.buckets.length; i++) {
|
||||
if (this.buckets[i]) {
|
||||
let value = this.buckets[i].find(key);
|
||||
if (value) return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
remove(key) {
|
||||
for (let i = 0; i < this.buckets.length; i++) {
|
||||
if (this.buckets[i]) {
|
||||
let removed = this.buckets[i].remove(key);
|
||||
if (this.buckets[i].size === 0) this.buckets[i] = undefined;
|
||||
if (removed) return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
length() {
|
||||
let count = 0;
|
||||
|
||||
for (let i = 0; i < this.buckets.length; i++) {
|
||||
if (this.buckets[i]) {
|
||||
count += this.buckets[i].size;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
clear() {
|
||||
this.capacity = this.initialCapacity;
|
||||
this.buckets = Array(this.capacity);
|
||||
}
|
||||
keys() {
|
||||
let _keys = [];
|
||||
|
||||
for (let i = 0; i < this.buckets.length; i++) {
|
||||
if (this.buckets[i]) {
|
||||
let currentNode = this.buckets[i].head;
|
||||
while (currentNode) {
|
||||
_keys.push(currentNode.key);
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
}
|
||||
}
|
||||
return _keys;
|
||||
}
|
||||
values() {
|
||||
let _values = [];
|
||||
|
||||
for (let i = 0; i < this.buckets.length; i++) {
|
||||
if (this.buckets[i]) {
|
||||
let currentNode = this.buckets[i].head;
|
||||
while (currentNode) {
|
||||
_values.push(currentNode.value);
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
}
|
||||
}
|
||||
return _values;
|
||||
}
|
||||
entries() {
|
||||
let _entries = [];
|
||||
|
||||
for (let i = 0; i < this.buckets.length; i++) {
|
||||
if (this.buckets[i]) {
|
||||
let currentNode = this.buckets[i].head;
|
||||
while (currentNode) {
|
||||
_entries.push([currentNode.key, currentNode.value]);
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
}
|
||||
}
|
||||
return _entries;
|
||||
}
|
||||
return _entries;
|
||||
}
|
||||
}
|
||||
|
||||
let t = new HashMap();
|
||||
|
||||
// Starter Data
|
||||
t.set("football", "redskins");
|
||||
t.set("framework", "laptop");
|
||||
t.set("framework 2", "sucks");
|
||||
t.set("deliver", "me");
|
||||
t.set("alpha", "omega");
|
||||
t.set("hello", "world");
|
||||
t.set("hello", "smigs");
|
||||
t.set("helo", "world");
|
||||
t.set("omg kevin that's wild", "world");
|
||||
t.set("mike", "world");
|
||||
t.set("zebra", "zoo");
|
||||
t.set("linux", "tux");
|
||||
t.set("c major", "c e g");
|
||||
t.remove("zebra");
|
||||
t.remove("hello");
|
||||
t.set('football', 'redskins');
|
||||
t.set('framework', 'laptop');
|
||||
t.set('framework 2', 'sucks');
|
||||
t.set('deliver', 'me');
|
||||
t.set('alpha', 'omega');
|
||||
t.set('hello', 'world');
|
||||
t.set('hello', 'smigs');
|
||||
t.set('helo', 'world');
|
||||
t.set("omg kevin that's wild", 'world');
|
||||
t.set('mike', 'world');
|
||||
t.set('zebra', 'zoo');
|
||||
t.set('linux', 'tux');
|
||||
t.set('c major', 'c e g');
|
||||
t.remove('zebra');
|
||||
t.remove('hello');
|
||||
console.log(t.length());
|
||||
// Print the Keys
|
||||
console.log(t.keys());
|
||||
|
@ -271,57 +271,57 @@ console.log(t.entries());
|
|||
|
||||
// clear the hashmap
|
||||
t.clear();
|
||||
console.log("Stuff was cleared");
|
||||
console.log("new data set");
|
||||
t.set("football", "redskins");
|
||||
t.set("framework", "laptop");
|
||||
t.set("framework 2", "sucks");
|
||||
t.set("deliver", "me");
|
||||
t.set("alpha", "omega");
|
||||
t.set("hello", "world");
|
||||
t.set("hello", "smigs");
|
||||
t.set("helo", "world");
|
||||
t.set("omg kevin that's wild", "world");
|
||||
t.set("mike", "world");
|
||||
t.set("zebra", "zoo");
|
||||
t.set("linux", "tux");
|
||||
t.set("c major", "c e g");
|
||||
t.set("user_role", "DevOps Engineer");
|
||||
t.set("interests", "Blogging, Influencing, Stocks, Generating Income");
|
||||
t.set("goals", "Build following / grow community, Learn and grow, Technology");
|
||||
t.set("preferred_name", "Smig");
|
||||
t.set("data_request", "Create key value pairs for an arbitrary data set");
|
||||
t.set("format", "t.set('key', 'value')");
|
||||
t.set("entries", "30");
|
||||
t.set("entry_1", "value_1");
|
||||
t.set("entry_2", "value_2");
|
||||
t.set("entry_3", "value_3");
|
||||
t.set("entry_4", "value_4");
|
||||
t.set("entry_5", "value_5");
|
||||
t.set("entry_6", "value_6");
|
||||
t.set("entry_7", "value_7");
|
||||
t.set("entry_8", "value_8");
|
||||
t.set("entry_9", "value_9");
|
||||
t.set("entry_10", "value_10");
|
||||
t.set("entry_11", "value_11");
|
||||
t.set("entry_12", "value_12");
|
||||
t.set("entry_13", "value_13");
|
||||
t.set("entry_14", "value_14");
|
||||
t.set("entry_15", "value_15");
|
||||
t.set("entry_16", "value_16");
|
||||
t.set("entry_17", "value_17");
|
||||
t.set("entry_18", "value_18");
|
||||
t.set("entry_19", "value_19");
|
||||
t.set("entry_20", "value_20");
|
||||
t.set("entry_21", "value_21");
|
||||
t.set("entry_22", "value_22");
|
||||
t.set("entry_23", "value_23");
|
||||
t.set("entry_24", "value_24");
|
||||
t.set("entry_25", "value_25");
|
||||
t.set("entry_26", "value_26");
|
||||
t.set("entry_27", "value_27");
|
||||
t.set("entry_28", "value_28");
|
||||
t.set("entry_29", "value_29");
|
||||
t.set("entry_30", "value_30");
|
||||
console.log('Stuff was cleared');
|
||||
console.log('new data set');
|
||||
t.set('football', 'redskins');
|
||||
t.set('framework', 'laptop');
|
||||
t.set('framework 2', 'sucks');
|
||||
t.set('deliver', 'me');
|
||||
t.set('alpha', 'omega');
|
||||
t.set('hello', 'world');
|
||||
t.set('hello', 'smigs');
|
||||
t.set('helo', 'world');
|
||||
t.set("omg kevin that's wild", 'world');
|
||||
t.set('mike', 'world');
|
||||
t.set('zebra', 'zoo');
|
||||
t.set('linux', 'tux');
|
||||
t.set('c major', 'c e g');
|
||||
t.set('user_role', 'DevOps Engineer');
|
||||
t.set('interests', 'Blogging, Influencing, Stocks, Generating Income');
|
||||
t.set('goals', 'Build following / grow community, Learn and grow, Technology');
|
||||
t.set('preferred_name', 'Smig');
|
||||
t.set('data_request', 'Create key value pairs for an arbitrary data set');
|
||||
t.set('format', "t.set('key', 'value')");
|
||||
t.set('entries', '30');
|
||||
t.set('entry_1', 'value_1');
|
||||
t.set('entry_2', 'value_2');
|
||||
t.set('entry_3', 'value_3');
|
||||
t.set('entry_4', 'value_4');
|
||||
t.set('entry_5', 'value_5');
|
||||
t.set('entry_6', 'value_6');
|
||||
t.set('entry_7', 'value_7');
|
||||
t.set('entry_8', 'value_8');
|
||||
t.set('entry_9', 'value_9');
|
||||
t.set('entry_10', 'value_10');
|
||||
t.set('entry_11', 'value_11');
|
||||
t.set('entry_12', 'value_12');
|
||||
t.set('entry_13', 'value_13');
|
||||
t.set('entry_14', 'value_14');
|
||||
t.set('entry_15', 'value_15');
|
||||
t.set('entry_16', 'value_16');
|
||||
t.set('entry_17', 'value_17');
|
||||
t.set('entry_18', 'value_18');
|
||||
t.set('entry_19', 'value_19');
|
||||
t.set('entry_20', 'value_20');
|
||||
t.set('entry_21', 'value_21');
|
||||
t.set('entry_22', 'value_22');
|
||||
t.set('entry_23', 'value_23');
|
||||
t.set('entry_24', 'value_24');
|
||||
t.set('entry_25', 'value_25');
|
||||
t.set('entry_26', 'value_26');
|
||||
t.set('entry_27', 'value_27');
|
||||
t.set('entry_28', 'value_28');
|
||||
t.set('entry_29', 'value_29');
|
||||
t.set('entry_30', 'value_30');
|
||||
console.log(t.keys());
|
||||
console.log(t.entries());
|
||||
|
|
127
csci/projectHashSet.js
Normal file
127
csci/projectHashSet.js
Normal file
|
@ -0,0 +1,127 @@
|
|||
// import { LinkedList } from './linkedList';
|
||||
import { LinkedList } from './linkedList.js';
|
||||
|
||||
class HashSet {
|
||||
constructor(capacity = 8) {
|
||||
this.initialCapacity = capacity;
|
||||
this.capacity = capacity;
|
||||
this.buckets = Array(capacity);
|
||||
this.capacity = this.buckets.length;
|
||||
this.loadFactor = 0.75;
|
||||
}
|
||||
|
||||
stringToNumber(string) {
|
||||
let hashCode = 0;
|
||||
const primeNumber = 31;
|
||||
|
||||
for (let i = 0; i < string.length; i++) {
|
||||
hashCode = primeNumber * hashCode + string.charCodeAt(i);
|
||||
}
|
||||
|
||||
return hashCode;
|
||||
}
|
||||
|
||||
hash(key) {
|
||||
return this.stringToNumber(key);
|
||||
}
|
||||
|
||||
capacityWatcher() {
|
||||
let consumed = this.buckets.filter(Object).length;
|
||||
if (consumed >= this.capacity * this.loadFactor) {
|
||||
this.capacity = Math.round(1.25 * this.capacity);
|
||||
}
|
||||
}
|
||||
|
||||
set(key) {
|
||||
let _hash = this.hash(key);
|
||||
let bucketIndex = _hash % this.capacity;
|
||||
|
||||
// Check if we need to add more buckets
|
||||
this.capacityWatcher();
|
||||
|
||||
if (
|
||||
this.buckets[bucketIndex] == undefined ||
|
||||
this.buckets[bucketIndex] == null
|
||||
) {
|
||||
let linkedList = new LinkedList();
|
||||
linkedList.append(key);
|
||||
|
||||
this.buckets[bucketIndex] = linkedList;
|
||||
} else {
|
||||
let keyExists = this.buckets[bucketIndex].contains(key);
|
||||
|
||||
if (keyExists) {
|
||||
// no dups
|
||||
return;
|
||||
} else {
|
||||
this.buckets[bucketIndex].append(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
get(key) {
|
||||
for (let i = 0; i < this.buckets.length; i++) {
|
||||
if (this.buckets[i]) {
|
||||
let _key = this.buckets[i].find(key);
|
||||
if (_key) return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
remove(key) {
|
||||
for (let i = 0; i < this.buckets.length; i++) {
|
||||
if (this.buckets[i]) {
|
||||
let removed = this.buckets[i].remove(key);
|
||||
if (this.buckets[i].size === 0) this.buckets[i] = undefined;
|
||||
if (removed) return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
length() {
|
||||
let count = 0;
|
||||
|
||||
for (let i = 0; i < this.buckets.length; i++) {
|
||||
if (this.buckets[i]) {
|
||||
count += this.buckets[i].size;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
clear() {
|
||||
this.capacity = this.initialCapacity;
|
||||
this.buckets = Array(this.capacity);
|
||||
}
|
||||
|
||||
keys() {
|
||||
let _keys = [];
|
||||
|
||||
for (let i = 0; i < this.buckets.length; i++) {
|
||||
if (this.buckets[i]) {
|
||||
let currentNode = this.buckets[i].head;
|
||||
while (currentNode) {
|
||||
_keys.push(currentNode.value);
|
||||
currentNode = currentNode.nextNode;
|
||||
}
|
||||
}
|
||||
}
|
||||
return _keys;
|
||||
}
|
||||
}
|
||||
|
||||
const _set = new HashSet();
|
||||
_set.set('test');
|
||||
_set.set('test');
|
||||
_set.set('hello');
|
||||
_set.set('deeeez');
|
||||
_set.set('deeez');
|
||||
_set.set('deez');
|
||||
_set.set('dez');
|
||||
_set.set('trueskii');
|
||||
_set.set('mike');
|
||||
console.log(_set.keys());
|
||||
_set.remove('test');
|
||||
console.log(_set.buckets);
|
Loading…
Reference in a new issue