-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashTables_intDict.py
105 lines (89 loc) · 3.06 KB
/
hashTables_intDict.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import random
class intDict(object):
"""A dictionary with integer keys"""
def __init__(self, numBuckets):
"""Create an empty dictionary"""
self.buckets = []
self.numBuckets = numBuckets
for i in range(numBuckets):
self.buckets.append([])
def addEntry(self, dictKey, dictVal):
"""Assumes dictKey an int. Adds an entry."""
hashBucket = self.buckets[dictKey%self.numBuckets]
for i in range(len(hashBucket)):
if hashBucket[i][0] == dictKey:
hashBucket[i] = (dictKey, dictVal)
return
hashBucket.append((dictKey, dictVal))
def getValue(self, dictKey):
"""Assumes dictKey an int. Returns entry associated
with the key dictKey"""
hashBucket = self.buckets[dictKey%self.numBuckets]
for e in hashBucket:
if e[0] == dictKey:
return e[1]
return None
def __str__(self):
res = '{'
for b in self.buckets:
for t in b:
res = res + str(t[0]) + ':' + str(t[1]) + ','
return res[:-1] + '}' #res[:-1] removes the last comma
def collision_prob(numBuckets, numInsertions):
'''
Given the number of buckets and the number of items to insert,
calculates the probability of a collision.
'''
prob = 1.0
for i in range(1, numInsertions):
prob = prob * ((numBuckets - i) / float(numBuckets))
return 1 - prob
def reverse_collision(numBuckets, probCol):
'''
Given a number of buckets, calculated the number of
insertions needed to reach a given probability of
collision.
'''
probCheck = 0.0
prob = 1.0
numInsertions = 1
if probCheck < probCol:
for i in range(1, numInsertions):
prob = prob * ((numBuckets - i) / float(numBuckets))
probCheck = 1 - prob
print probCheck
numInsertions += 1
else:
return numInsertions
def sim_insertions(numBuckets, numInsertions):
'''
Run a simulation for numInsertions insertions into the hash table.
'''
choices = range(numBuckets)
used = []
for i in range(numInsertions):
hashVal = random.choice(choices)
if hashVal in used:
return False
else:
used.append(hashVal)
return True
def observe_prob(numBuckets, numInsertions, numTrials):
'''
Given the number of buckets and the number of items to insert,
runs a simulation numTrials times to observe the probability of a collision.
'''
probs = []
for t in range(numTrials):
probs.append(sim_insertions(numBuckets, numInsertions))
return 1 - sum(probs)/float(numTrials)
def main():
hash_table = intDict(25)
hash_table.addEntry(15, 'a')
# random.seed(1) # Uncomment for consistent results
for i in range(20):
hash_table.addEntry(int(random.random() * (10 ** 9)), i)
hash_table.addEntry(15, 'b')
print hash_table.buckets #evil
print '\n', 'hash_table =', hash_table
print hash_table.getValue(15)