Add new course
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Week 4: Programming Assignments (8 points)</title>
|
||||
<meta charset="utf-8">
|
||||
|
||||
</head>
|
||||
<body>
|
||||
<div>
|
||||
<h4><strong>Programming Assignments: Hashing</strong></h4>
|
||||
</div>
|
||||
<div>Both assignments are related to fixed sized hash tables. The hash tables store string (str) values.
|
||||
The hash value (slot) is calculated with the following hash function for strings:</div>
|
||||
<p></p>
|
||||
<pre>procedure hash(data):
|
||||
sum = 0
|
||||
for i = 0 to N-1 do
|
||||
sum += ascii(data[i])
|
||||
return sum % X
|
||||
</pre>
|
||||
<p></p>
|
||||
<div>where N is the length of the string (data), <span style="color: rgb(33, 37, 41); font-family: SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; font-size: 14px;">% </span>is the symbol for the mod operation and <span style="color: rgb(33, 37, 41); font-family: SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; font-size: 14px;">X </span>is a parameter of the hash table. The ascii value of a character can be calculated with the function <strong><a href="https://www.w3schools.com/python/ref_func_ord.asp">ord</a></strong> in Python.</div>
|
||||
<br>
|
||||
<br>
|
||||
|
||||
|
||||
<div>
|
||||
<h4><strong>Assignment 4.1: Linear Probing </strong>(4 points)</h4>
|
||||
</div>
|
||||
<div>Implement a fixed sized hash table in Python that uses <u>linear probing</u> for collision resolution.
|
||||
Create a class <strong>HashLinear</strong> which has the table size \(M\) as a input value when a object is created. The class has following methods:</div>
|
||||
<ul>
|
||||
<ul>
|
||||
<li><strong>insert(data: str)</strong>: inserts new data into the hash table, no duplicates</li>
|
||||
<li><strong>delete(data: str)</strong>: removes data from the hash table</li>
|
||||
<li><strong>print()</strong>: prints the content of the hash table (the data string in each slot separated with a space; skip empty slots; see the example below)</li>
|
||||
</ul>
|
||||
</ul>
|
||||
<div>For hashing use \(X = M\).</div>
|
||||
<p></p>
|
||||
<div>A code template with an example program (the hash table has the size of \(M=8\)): </div>
|
||||
<p></p>
|
||||
<div style="border:2px solid black">
|
||||
<pre>class HashLinear:
|
||||
# TODO
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
table = HashLinear(8)
|
||||
table.insert("BM40A1500")
|
||||
table.insert("fOo")
|
||||
table.insert("123")
|
||||
table.insert("Bar1")
|
||||
table.insert("10aaaa1")
|
||||
table.insert("BM40A1500")
|
||||
table.print() # 10aaaa1 BM40A1500 fOo 123 Bar1
|
||||
table.delete("fOo")
|
||||
table.delete("Some arbitary string which is not in the table")
|
||||
table.delete("123")
|
||||
table.print() # 10aaaa1 BM40A1500 Bar1
|
||||
</pre>
|
||||
</div>
|
||||
<div><br></div><div>Submit your solution in CodeGrade as <strong>hashlinear.py</strong>.</div>
|
||||
<br>
|
||||
<br>
|
||||
|
||||
|
||||
<div>
|
||||
<h4><strong>Assignment 4.1: Bucket Hashing </strong>(4 points)</h4>
|
||||
</div>
|
||||
<div>Implement a fixed sized hash table in Python that uses <u>bucket hashing</u> for collision resolution.
|
||||
Create a class <strong>HashBucket</strong> which has the table size \(M\) and number of equal sized buckets \(B\) as the input values when a object is created.
|
||||
The hash table has a overflow array of size \(M\). The class has the following methods:</div>
|
||||
<ul>
|
||||
<ul>
|
||||
<li><strong>insert(data)</strong>: inserts new data in the hash table, no duplicates</li>
|
||||
<li><strong>delete(data)</strong>: removes data from the hash table</li>
|
||||
<li><strong>print()</strong>: prints the content of the hash table and the overflow array (the data string in each slot followed by the data in the overflow array; slots separated with a space and empty slots skipped; see the example below)</li>
|
||||
</ul>
|
||||
</ul>
|
||||
<div>For hashing use \(X = B\). Filling the buckets starts from the top and overflow values are appended to the end of the overflow array. </div>
|
||||
<p></p>
|
||||
<div>A code template with an example program (the hash table has the size of \(M=8\) and has \(B=4\) buckets):<br></div>
|
||||
<p></p>
|
||||
<div style="border:2px solid black"><pre>class HashBucket:
|
||||
# TODO
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
table = HashBucket(8, 4)
|
||||
table.insert("BM40A1500")
|
||||
table.insert("fOo")
|
||||
table.insert("123")
|
||||
table.insert("Bar1")
|
||||
table.insert("10aaaa1")
|
||||
table.insert("BM40A1500")
|
||||
table.print() # fOo BM40A1500 123 Bar1 10aaaa1
|
||||
table.delete("fOo")
|
||||
table.delete("Some arbitary string which is not in the table")
|
||||
table.delete("123")
|
||||
table.print() # BM40A1500 Bar1 10aaaa1
|
||||
</pre>
|
||||
</div>
|
||||
<div><br></div><div>Submit your solution in CodeGrade as <strong>hashbucket.py</strong>.</div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,79 @@
|
||||
def hash(S, size):
|
||||
num = 0
|
||||
for i in range(0, len(S)):
|
||||
num += ord(S[i])
|
||||
return num % size
|
||||
|
||||
# Hash table with bucket hashing and overflow array
|
||||
# Class HashBucket which has the table size M and number of equal sized buckets B as the input values when a object is created. Size of one bucket is M/B. The hash table has a overflow array of size M.
|
||||
|
||||
|
||||
class HashBucket:
|
||||
def __init__(self, M, B):
|
||||
self.M = M
|
||||
self.B = B
|
||||
self.table = [[] for i in range(0, M)]
|
||||
self.overflow = []
|
||||
|
||||
# No duplicate strings are allowed
|
||||
def insert(self, S):
|
||||
h = hash(S, self.B)
|
||||
if S in self.table[h]:
|
||||
return
|
||||
elif S in self.overflow:
|
||||
return
|
||||
elif len(self.table[h]) < self.M / self.B:
|
||||
self.table[h].append(S)
|
||||
else:
|
||||
self.overflow.append(S)
|
||||
|
||||
def search(self, S):
|
||||
h = hash(S, self.B)
|
||||
if S in self.table[h]:
|
||||
return True
|
||||
elif S in self.overflow:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def delete(self, S):
|
||||
h = hash(S, self.B)
|
||||
if S in self.table[h]:
|
||||
self.table[h].remove(S)
|
||||
elif S in self.overflow:
|
||||
self.overflow.remove(S)
|
||||
else:
|
||||
print("String not found")
|
||||
|
||||
def print(self):
|
||||
for i in range(0, self.M):
|
||||
for j in range(0, len(self.table[i])):
|
||||
print(self.table[i][j], end=' ')
|
||||
for z in range(0, len(self.overflow)):
|
||||
print(self.overflow[z], end=' ')
|
||||
print()
|
||||
|
||||
|
||||
# Main program
|
||||
if __name__ == "__main__":
|
||||
table = HashBucket(20, 4)
|
||||
a = ["door", "billion", "how", "choice", "at", "husband", "truth", "song", "share", "develop", "Mr", "everybody", "common", "blood", "Democrat", "until", "stock", "southern", "song", "cover"
|
||||
]
|
||||
for i in a:
|
||||
table.insert(i)
|
||||
table.print()
|
||||
b = ["song", "develop", "choice", "common", "until", "how", "billion", "blood", "door", "truth"
|
||||
]
|
||||
for i in b:
|
||||
table.delete(i)
|
||||
table.print()
|
||||
table = HashBucket(8, 4)
|
||||
a = ["dinner", "while", "call", "relate",
|
||||
"be", "easy", "yourself", "decide"]
|
||||
for i in a:
|
||||
table.insert(i)
|
||||
table.print()
|
||||
b = ["relate", "call", "decide", "while"]
|
||||
for i in b:
|
||||
table.delete(i)
|
||||
table.print()
|
||||
@@ -0,0 +1,75 @@
|
||||
def hash(S, size):
|
||||
num = 0
|
||||
for i in range(0, len(S)):
|
||||
num += ord(S[i])
|
||||
return num % size
|
||||
|
||||
# Hash table with linear probing
|
||||
|
||||
|
||||
class HashLinear:
|
||||
def __init__(self, num):
|
||||
self.table = ["DELETED"] * num
|
||||
self.size = 0
|
||||
self.num = num
|
||||
|
||||
def insert(self, S):
|
||||
if self.search(S):
|
||||
return False
|
||||
h = hash(S, self.num)
|
||||
while self.table[h] != "DELETED" and self.table[h] != None:
|
||||
h = (h + 1) % self.num
|
||||
self.table[h] = S
|
||||
self.size += 1
|
||||
return True
|
||||
|
||||
def search(self, S):
|
||||
h = hash(S, self.num)
|
||||
while self.table[h] != "DELETED":
|
||||
if self.table[h] == S:
|
||||
return True
|
||||
h = (h + 1) % self.num
|
||||
return False
|
||||
|
||||
def find(self, S):
|
||||
h = hash(S, self.num)
|
||||
while self.table[h] != "DELETED":
|
||||
if self.table[h] == S:
|
||||
return h
|
||||
h = (h + 1) % self.num
|
||||
return -1
|
||||
|
||||
def delete(self, S):
|
||||
if not self.search(S):
|
||||
return False
|
||||
h = hash(S, self.num)
|
||||
if self.table[h] != S:
|
||||
while self.table[h] != S:
|
||||
h = (h + 1) % self.num
|
||||
self.table[h] = "DELETED"
|
||||
self.table[h] = None
|
||||
self.size -= 1
|
||||
return True
|
||||
|
||||
def print(self):
|
||||
for i in range(0, self.num):
|
||||
if self.table[i] != "DELETED" and self.table[i] != None:
|
||||
#print(i, self.table[i])
|
||||
print(self.table[i], end=' ')
|
||||
print()
|
||||
|
||||
def __str__(self):
|
||||
return str(self.table)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
table = HashLinear(20)
|
||||
a = ["town", "rather", "short", "toward", "employee", "player", "toward", "the", "of", "college",
|
||||
"in", "yes", "billion", "five", "wear", "last", "decade", "first", "training", "friend"]
|
||||
for i in a:
|
||||
table.insert(i)
|
||||
b = ["employee", "of", "toward", "in", "player",
|
||||
"town", "toward", "five", "rather", "yes"]
|
||||
for i in b:
|
||||
table.delete(i)
|
||||
table.print()
|
||||
Reference in New Issue
Block a user