-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_stuff.py
55 lines (43 loc) · 1.79 KB
/
plot_stuff.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
import matplotlib.pyplot as plt
import string
def loadWords():
'''Returns a list containing the words from PSET 4'''
fin = open('/Users/Kamil/GDrive/Clients and Projects/Learn Python/ProblemSet4/words.txt', 'r')
wordList = []
for line in fin:
wordList.append(line.strip().lower())
return wordList
fin.close()
##--------------------------------------------------------
## The following three hash functions were given to us.
def firstHashFunction(s):
return string.ascii_lowercase.index(s[0])
def secondHashFunction(s):
return string.ascii_lowercase.index(s[-1])
def thirdHashFunction(s):
total = 0
for char in s:
total += string.ascii_lowercase.index(char)
return total % 26
##----------------------------------------------------------
def doHashing(hashFunction):
'''Takes as argument one of the three hash functions defined.
Returns a dictionary containing a frequency distribution of all integers
returned by the hash function.'''
wordList = loadWords()
hashedDict = {}
for w in wordList:
#Convert each word in the wordList to its corresponding hash.
hashedWord = hashFunction(w)
#Create a frequency distribution of all the integers returned by the hash function.
hashedDict[hashedWord] = hashedDict.get(hashedWord, 0) + 1
return hashedDict
def plotHash(hashFunction):
''' Takes as argument one of the hash functions defined. Plots frequency distribution
of all the integers returned by the hash function.'''
plt.bar(doHashing(hashFunction).keys(),doHashing(hashFunction).values(),facecolor='#9999ff', edgecolor='white')
plt.ylabel('Number of words in wordlist')
plt.xlabel('Hash')
plt.title(hashFunction)
plt.axis([0, 26, 0, 10000])
plt.show()