From b44d22a0797707775c7baef9ba1eb00dcd37745f Mon Sep 17 00:00:00 2001 From: Richard Ashworth Date: Wed, 1 Nov 2023 15:43:22 +0000 Subject: [PATCH] Rich solutions to hash tables --- .../lists/hashTable/hashTable.spec.js | 16 ++++++++- .../lists/hashTable/problem.js | 35 ++++++++++++++++--- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/exercises/k9-data-structures/lists/hashTable/hashTable.spec.js b/exercises/k9-data-structures/lists/hashTable/hashTable.spec.js index 0e0ad94..9d45647 100644 --- a/exercises/k9-data-structures/lists/hashTable/hashTable.spec.js +++ b/exercises/k9-data-structures/lists/hashTable/hashTable.spec.js @@ -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); }); @@ -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); }); @@ -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]) + }) + }); diff --git a/exercises/k9-data-structures/lists/hashTable/problem.js b/exercises/k9-data-structures/lists/hashTable/problem.js index 9efdba7..002be4a 100644 --- a/exercises/k9-data-structures/lists/hashTable/problem.js +++ b/exercises/k9-data-structures/lists/hashTable/problem.js @@ -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). @@ -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)) @@ -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.