-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash_table.py
More file actions
204 lines (161 loc) · 6.25 KB
/
Copy pathhash_table.py
File metadata and controls
204 lines (161 loc) · 6.25 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
"""
Simulate a Markov Chain Algorithm to generate text based on
preceding words while manually implementing a Hashtable without
using Python dictionaries.
"""
import sys
import random
class Hashtable:
def __init__(self, size):
"""Initializes the Hashtable object with two attributes, pairs
and size.
Parameters: size is the size of the Hashtable.
Returns: None."""
self._pairs = [None] * size
self._size = size
def pairs(self):
return self._pairs
def size(self):
return self._size
def put(self, key, value):
"""Places a key-value pair into the Hashtable's table if its hash is
empty. On collision, performs linear probing to find the next
available spot in the Hashtable.
Parameters: key is the key of the key-value pair.
value is the value of the key-value pair.
Returns: None."""
hash = self._hash(key)
if self.pairs()[hash] == None:
self.pairs()[hash] = [key, value]
else:
hash -= 1
while self.pairs()[hash] != None:
if hash < 0:
hash = len(self.pairs()) - 1
hash -= 1
self.pairs()[hash] = [key, value]
def get(self, key):
"""Returns the value given the key from the Hashtable. Returns None
if the key does not exist in the Hashtable.
Parameters: key is the key whose value you want to search for.
Returns: the value of the corresponding key-value pair.
None if the key does not exist in the Hashtable."""
hash = self._hash(key)
if self.pairs()[hash] == None:
return None
elif self.pairs()[hash][0] == key:
return self.pairs()[hash][1]
hash -= 1
while self.pairs()[hash] != None and hash != self._hash(key):
if hash < 0:
hash = len(self.pairs()) - 1
if self.pairs()[hash][0] == key:
return self.pairs()[hash][1]
hash -= 1
return None
def __contains__(self, key):
"""Returns True if a give key is in the Hashtable and False if not.
Parameters: key is the key that you want to search for in the
Hashtable.
Returns: True if the key is found in the Hashtable.
False if the key is not found in the Hashtable."""
if self.get(key) == None:
return False
return True
def _hash(self, key):
"""Uses Horner's Rule to hash a given key.
Parameters: key is the key that you want to hash.
Returns: The resulting hash from the given key."""
p = 0
for c in key:
p = 31*p + ord(c)
return p % self._size
def __str__(self):
return str(self.pairs())
def create_list_from_file(sfile):
"""Takes a file and for each line in the file, splits up the words in
each line and adds it to a list.
Parameters: sfile is a file object that is being read.
Returns: a list containing all the words in sfile."""
alist = []
for line in sfile:
line = line.split()
alist += line
return alist
def put_into_hashtable(words, adict, alist):
"""Takes a list of words, a Hashtable object, and another list and places
the corresponding key-value pairs into the Hashtable.
Parameters: words is a list of all the words that you want to place
into the Hashtable.
adict is a Hashtable object.
alist is a list that will only contain a certain number of strings
within it at any given time. The strings within will be used
as the keys within the Hashtable.
Returns: None."""
for x in range(len(words)):
key = " ".join(alist)
if key in adict:
adict.get(key).append(words[x])
else:
adict.put(key, [words[x]])
alist.append(words[x])
alist = alist[1:]
def generate_output_list(alist, adict, num_words):
"""Takes a list, a Hashtable object, and a specified length and creates
a list of words that will be converted into a sentence.
Parameters: alist is a list of strings that will be used as the keys to
search for values within the Hashtable.
adict is a Hashtable object.
num_words is the total number of words that will be generated.
Returns: a list that contains the randomly generated words."""
output = []
i = 0
while len(output) != num_words:
key = " ".join(alist)
val = adict.get(key)
if len(val) > 1:
output.append(val[random.randint(0, len(val) - 1)])
elif len(val) == 1:
output.append(val[0])
alist.append(output[i])
alist = alist[1:]
i += 1
return output
def generate_sentence(output):
"""Takes a list of strings and adds every 10 of them together.
Once the length of the string exceeds 10, the function goes to
a new line and adds the next 10 strings within the list.
Parameters: output is a list of strings that will be added together.
Returns: The resulting string from adding the strings within output."""
sentence = ""
astring = ""
while len(output) > 10:
astring = " ".join(output[0:10])
sentence += astring + "\n"
astring = ""
output = output[10:]
sentence += " ".join(output)
return sentence
def main():
SEED = 8
NONWORD = "@"
random.seed(SEED)
sfile = open(input(), "r")
M = int(input())
n = int(input())
num_words = int(input())
if n < 1:
print("ERROR: specified prefix size is less than one")
sys.exit(0)
if num_words < 1:
print("ERROR: specified size of the generated text is less than one")
sys.exit(0)
words = create_list_from_file(sfile)
adict = Hashtable(M)
alist = [NONWORD] * n
put_into_hashtable(words, adict, alist)
alist = [NONWORD] * n
output = generate_output_list(alist, adict, num_words)
final = generate_sentence(output)
print(final)
main()