Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rich solutions to hash tables #6

Open
wants to merge 1 commit into
base: rich-solutions
Choose a base branch
from
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
16 changes: 15 additions & 1 deletion exercises/k9-data-structures/lists/hashTable/hashTable.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ describe("Generic hash table tests", () => {
expect(hashTable.length()).toBe(0);

hashTable.setItem("someKey", "someValue");
hashTable.setItem("testKey", "testValue");
hashTable.setItem("anotherKey", "anotherValue")

expect(hashTable.length()).toBe(1);
expect(hashTable.length()).toBe(3);
});


Expand Down Expand Up @@ -61,6 +63,8 @@ describe("Generic hash table tests", () => {
hashTable.setItem("someKey", "someValue");
expect(hashTable.length()).toBe(1);

console.log(hashTable.hashTable)

expect(hashTable.delete("someOtherKey")).toEqual(false);
expect(hashTable.length()).toBe(1);
});
Expand All @@ -73,4 +77,14 @@ describe("Generic hash table tests", () => {
expect(hashTable.length()).toBe(2);
});

test("re order list by remainders", () => {
const hashTable = new HashTable();
const array = [10, 15, 12, 14, 17, 99, 43, 55]
for (const key of array) {
const item = key % 10;
hashTable.setItem(key, item)
}
expect(hashTable.orderedByRemainders(array)).toStrictEqual([10, 12, 43, 14, 15, 55, 17, 99])
})

});
35 changes: 31 additions & 4 deletions exercises/k9-data-structures/lists/hashTable/problem.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ export class HashTable {

//The hashing function - this is a simple implementation of a hash to help with the time / space complexity.
#hash(key) {
return key.toString().length % this.hashTable.length;
if (typeof key === 'string') {
return key.toString().length % this.hashTable.length;
} else if (typeof key === 'number' && key % 1 === 0) {
return key % this.hashTable.length;
}

}

//Sets the key value item at a particular index (based on the hash method used).
Expand All @@ -26,14 +31,14 @@ export class HashTable {

if (this.hashTable[index]) {
for(let item of this.hashTable[index]) {
if (item[0] === key)
return item;
if (item[0] === key)
return item;
}
}

return null;
}

// searches for key and returns index
search(key) {
const index = this.#hash(key);
if (this.hashTable[index] && this.hashTable[index].find(item => item[0] === key))
Expand Down Expand Up @@ -64,9 +69,31 @@ export class HashTable {
get() {
return this.hashTable;
}

orderedByRemainders() {

// for (const key of array) {
// const item = key % 10;
// this.setItem(key, item)
// }

let finalArray = []

for (let item of this.hashTable) {
if (item){
for (let array of item) {
finalArray.push(array[0]);
}
}

}

return finalArray
}
}



/*
Display the ascending values for the array, based on the remainder of dividing each entry to 10.
Provide the time / space complexity for the algorithm implemented.
Expand Down