Skip to content

Commit 3c150a2

Browse files
committedDec 27, 2018
Maps and Hashing completed
1 parent 6925362 commit 3c150a2

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed
 

‎4-maps-hashing/Hashing-notes.md

+9
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,12 @@ What number would you recommend his function to divide by to speed it up?
4646
87 creates collisions.
4747
125 is divisible by 5 and hence also creates collisions.
4848
1001 is wasted space.
49+
50+
### Hash Maps and String Keys
51+
52+
Starting with a key value pair, one can run the keys through a hash function and then store the value in the bucket belonging to the hashed result of the key.
53+
54+
This can be done with strings too, most typically by using the ASCII value of the string. Python's ord() function converts a char into its ASCII value, and the chr() function converts it back.
55+
56+
Apparently the standard in Java prefers big hash tables over collisions, so they use an equation like:
57+
s[0]*31^(n-1) + s[1]*31^(n-2) + ... s[n-1]

‎4-maps-hashing/StringKeys-mine.py

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"""Write a HashTable class that stores strings
2+
in a hash table, where keys are calculated
3+
using the first two letters of the string."""
4+
5+
class HashTable(object):
6+
def __init__(self):
7+
self.table = [None]*10000
8+
9+
def store(self, string):
10+
"""Input a string that's stored in
11+
the table."""
12+
# Hash Value = (ASCII Value of First Letter * 100) + ASCII Value of Second Letter
13+
hash_val = ord(string[0])*100 + ord(string[1])
14+
if self.table[hash_val] == None:
15+
self.table[hash_val] = [string]
16+
else:
17+
self.table[hash_val].append(string)
18+
19+
def lookup(self, string):
20+
"""Return the hash value if the
21+
string is already in the table.
22+
Return -1 otherwise."""
23+
hash_val = ord(string[0])*100 + ord(string[1])
24+
if self.table[hash_val] != None:
25+
for i in self.table[hash_val]:
26+
if i == string:
27+
return hash_val
28+
return -1
29+
30+
def calculate_hash_value(self, string):
31+
"""Helper function to calulate a
32+
hash value from a string."""
33+
hash_val = ord(string[0])*100 + ord(string[1])
34+
return hash_val
35+
36+
# for myself
37+
def show_bucket(self, string):
38+
hash_val = ord(string[0])*100 + ord(string[1])
39+
if self.table[hash_val] != None:
40+
for i in self.table[hash_val]:
41+
print(i)
42+
43+
# Setup
44+
hash_table = HashTable()
45+
46+
# Test calculate_hash_value
47+
# Should be 8568
48+
print hash_table.calculate_hash_value('UDACITY')
49+
50+
# Test lookup edge case
51+
# Should be -1
52+
print hash_table.lookup('UDACITY')
53+
54+
# Test store
55+
hash_table.store('UDACITY')
56+
# Should be 8568
57+
print hash_table.lookup('UDACITY')
58+
59+
# Test store edge case
60+
hash_table.store('UDACIOUS')
61+
# Should be 8568
62+
print hash_table.lookup('UDACIOUS')
63+
hash_table.show_bucket('UDACIOUS')

0 commit comments

Comments
 (0)
Please sign in to comment.