Movatterモバイル変換


[0]ホーム

URL:


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

Commit1526e42

Browse files
committed
Added Simple Hash & Better Hash to Hash Table 🎨
1 parent42857b1 commit1526e42

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

‎examples/datastructures/hash-table.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
classHashTable{
2+
constructor(){
3+
this.table=newArray(137);
4+
}
5+
6+
simpleHash(word){
7+
lettotal=0;
8+
for(leti=0;i<word.length;i++)
9+
total+=word.charCodeAt(i);
10+
returntotal%this.table.length;
11+
}
12+
13+
betterHash(word){
14+
lettotal=0;
15+
for(leti=0;i<word.length;i++)
16+
total=37*total+word.charCodeAt(i);
17+
returntotal%this.table.length;
18+
}
19+
20+
put(value,whichHash){
21+
letpos;
22+
if(whichHash=="simpleHash")
23+
pos=this.simpleHash(value);
24+
if(whichHash=="betterHash")
25+
pos=this.betterHash(value);
26+
this.table[pos]=value;
27+
}
28+
29+
get(value,whichHash){
30+
lethash;
31+
if(whichHash=="simpleHash")
32+
hash=this.simpleHash(value);
33+
if(whichHash=="betterHash")
34+
hash=this.betterHash(value);
35+
returnthis.table[hash];
36+
}
37+
38+
showAll(){
39+
letstr="";
40+
for(leti=0;i<this.table.length;i++)
41+
if(this.table[i]!=undefined)
42+
str+=i+" : "+this.table[i]+"\n";
43+
returnstr;
44+
}
45+
}
46+
47+
module.exports=HashTable;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
constexpect=require('chai').expect;
2+
constHashTable=require('../../examples/datastructures/hash-table');
3+
4+
describe('=> HASH TABLE',function(){
5+
letnames;
6+
before(function(){
7+
names=newHashTable();
8+
});
9+
10+
it('should create an Empty Hash Table',function(done){
11+
expect(names.table).to.deep.equal([]);
12+
done();
13+
});
14+
15+
describe('=> simpleHash',function(){
16+
before(function(){
17+
names=newHashTable();
18+
});
19+
20+
it('should put only 1 value of "Clayton" & "Raymond" in Hash Table bcz of Simple Hash',function(done){
21+
names.put("Clayton","simpleHash");
22+
names.put("Raymond","simpleHash");
23+
expect(names.showAll()).to.equal("45 : Raymond\n");
24+
done();
25+
});
26+
27+
after(function(){
28+
names=newHashTable();
29+
});
30+
});
31+
32+
describe('=> betterHash',function(){
33+
it('should put both values of "Clayton" & "Raymond" in Hash Table bcz of Simple Hash',function(done){
34+
names.put("Clayton","betterHash");
35+
names.put("Raymond","betterHash");
36+
expect(names.showAll()).to.equal("88 : Clayton\n92 : Raymond\n");
37+
done();
38+
});
39+
});
40+
41+
it('should get value from Hash Table',function(){
42+
names.put("New","betterHash");
43+
expect(names.get("New","betterHash")).to.equal("New");
44+
expect(names.get("xxx","betterHash")).to.equal(undefined);
45+
});
46+
47+
});

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp