Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6a1186c

Browse files
committed
add basic hash function which takes only strings
1 parent 3f1c179 commit 6a1186c

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed

‎dist/datastructures/HashTable.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
class HashTable {
4+
constructor() {
5+
this.hash = (value, max) => {
6+
// naive hash fn for illustrative purposes
7+
if (max <= 0)
8+
throw TypeError('Max hash value must be a positive integer');
9+
return value.split('').reduce((a, b) => a * b.charCodeAt(0), 1) % max;
10+
};
11+
}
12+
}
13+
exports.default = HashTable;

‎dist/datastructures/HashTable.test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"use strict";
2+
var __importDefault = (this && this.__importDefault) || function (mod) {
3+
return (mod && mod.__esModule) ? mod : { "default": mod };
4+
};
5+
Object.defineProperty(exports, "__esModule", { value: true });
6+
const HashTable_1 = __importDefault(require("./HashTable"));
7+
let HT = new HashTable_1.default();
8+
beforeEach(() => HT = new HashTable_1.default());
9+
describe('basic hash table', () => {
10+
test('hash fn exists', () => {
11+
expect(typeof HT.hash).toBe('function');
12+
});
13+
test('hash fn takes string and returns number between 0 and specified max', () => {
14+
const max = 50;
15+
expect(HT.hash('test', max)).toBeLessThan(50);
16+
});
17+
test('hash fn throws error if max argument is <= 0', () => {
18+
let max = 0;
19+
try {
20+
HT.hash('test', max);
21+
}
22+
catch (e) {
23+
expect(e.message).toBe("Max hash value must be a positive integer");
24+
}
25+
max = -1;
26+
try {
27+
HT.hash('test', max);
28+
}
29+
catch (e) {
30+
expect(e.message).toBe("Max hash value must be a positive integer");
31+
}
32+
});
33+
});

‎src/datastructures/HashTable.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import HashTable from './HashTable';
2+
3+
let HT = new HashTable<string>();
4+
beforeEach(() => HT = new HashTable<string>());
5+
6+
describe('basic hash table', () => {
7+
test('hash fn exists', () => {
8+
expect(typeof HT.hash).toBe('function');
9+
});
10+
test('hash fn takes string and returns number between 0 and specified max', () => {
11+
const max = 50;
12+
expect(HT.hash('test', max)).toBeLessThan(50);
13+
});
14+
test('hash fn throws error if max argument is <= 0', () => {
15+
let max = 0;
16+
try {
17+
HT.hash('test', max);
18+
} catch (e) {
19+
expect(e.message).toBe("Max hash value must be a positive integer");
20+
}
21+
max = -1;
22+
try {
23+
HT.hash('test', max);
24+
} catch (e) {
25+
expect(e.message).toBe("Max hash value must be a positive integer");
26+
}
27+
});
28+
})

‎src/datastructures/HashTable.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class HashTable<T> {
2+
constructor() {
3+
4+
}
5+
hash = (value: string, max: number): number => {
6+
// naive hash fn for illustrative purposes
7+
if (max <= 0) throw TypeError('Max hash value must be a positive integer');
8+
return value.split('').reduce((a, b) => a * b.charCodeAt(0), 1) % max;
9+
}
10+
}
11+
12+
export default HashTable;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /