-
Notifications
You must be signed in to change notification settings - Fork 0
/
HashTableFunctions.java
59 lines (52 loc) · 1.47 KB
/
HashTableFunctions.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* @author Lehasa Seoe (SXXLEH001)
* 14 May 2020
* This class hashes simple textual data
*/
class HashTableFunctions
{
int hashTableSize;
String[] hashTableArray;
// constructor
/**
* This is the constructor and it initializes the instance variables
* @param size it is the size of the array we use
* @param array It is an array for our hash table
*/
public HashTableFunctions ( int size, String [] array )
{
hashTableSize = size;
hashTableArray = array;
}
// hash function
/**
* This is our hash function
* @param s this is the string to be hashed
*/
public int hash ( String s )
{
int sum = 0;
// WRITE YOUR CODE HERE
sum= s.charAt(0) +2*s.charAt(1)+ 3*s.charAt(2); /* I used a bash script to obtain the 3-tuple (1,2,3)
that gives a perfect hash function since you never
get the same tuple for these words, given the choice of the function i chose*/
return ((sum%37)+37)%37;
}
// return True if the hash table contains string s
// return False if the hash table does not contain string s
/**
* This is our find method
* @param s this is the string to be found
*/
boolean find ( String s )
{
boolean found = false;
// WRITE YOUR CODE HERE
for (int i=0 ; i< hashTableSize ; i++){
if (hashTableArray[i].equals(s)) {
found = true;
}
}
return found;
}
}