This repository has been archived on 2025-12-15. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
2024-09-20 14:17:13 +03:00

106 lines
4.6 KiB
HTML

<!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, &quot;Liberation Mono&quot;, &quot;Courier New&quot;, monospace; font-size: 14px;">% </span>is the symbol for the mod operation and&nbsp;<span style="color: rgb(33, 37, 41); font-family: SFMono-Regular, Menlo, Monaco, Consolas, &quot;Liberation Mono&quot;, &quot;Courier New&quot;, 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>