and write up, there's a example run, on how to do it.

1 answer below »
and write up, there's a example run, on how to do it.
Answered Same DayApr 28, 2021

Answer To: and write up, there's a example run, on how to do it.

Abhishek answered on Apr 30 2021
151 Votes
55809 - DS/Screenshots/O3.png
55809 - DS/Screenshots/O2.png
55809 - DS/Screenshots/O1.png
55809 - DS/Solution/prog04_hashtable_linkedlist.py
class SLLH():

class DataNode():

def __init__(self, key, val):
if not isinstance(val,str) and not isinstance(val,int):
raise Exception("data must be either interger or string")
if not isinstance(key,str) and
not isinstance(key,int):
raise Exception("key type must be either string or integer")

self.key = key
self.val = val

self.next = None

def __str__(self):
return str(self.key)+",|"+str(self.val)+"|"

def __init__(self):
self.head = None
self.sz = 0

def append(self, key, val):
if(self.sz == 0):
self.head = self.DataNode(key, val)
else:
temp = self.head
while temp.next != None:
if temp.key == key:
temp.val = val
break
temp = temp.next
if temp.key == key:
temp.val = val
else:
temp.next = self.DataNode(key, val)
self.sz += 1


def remove_at(self, idx):
if idx < 0:
raise Exception("index cannot be negative for chain LL.")

cur = self.head
cur_idx = 0

if(idx == 0 and self.head):
node = self.head
self.head = self.head.next
self.sz -=1

return str(node.val)

while(cur and cur_idx < idx -1):
cur = cur.next
cur_idx +=1

if(not cur or not cur.next):
raise Exception("Index out of bounds for chain LL.")

node = cur.next
cur.next = cur.next.next
self.sz -=1

return node


def remove(self, key):
to_be_removed = []
for i in range(self.sz):
if self.getitem_at(i).key==key:
to_be_removed.append(i)
break

if to_be_removed:
self.remove_at(to_be_removed[0])
else:
raise Exception(f"An entry with key {key} doesn't exist")

def getitem_at(self, idx):
cur = self.head
cur_idx = 0

while(cur and cur_idx < idx):
cur = cur.next
cur_idx+=1

if(cur):
return cur

raise Exception(f"Inedex {idx} out of bounds for list of length {cur_idx+1}")
def __getitem__(self, key):
for i in range(self.sz):
if self.getitem_at(i).key==key:
return self.getitem_at(i)

raise Exception(f"Key:{key} does not exist in the table.")


def __str__(self):
str_rep = ""
arrow_str = "--> "

str_rep += arrow_str

cur = self.head
while(cur):
str_rep+=str(cur)+" "
str_rep += arrow_str
cur = cur.next
str_rep += "NULL\n"

return str_rep

class ChainedHashTable():

def to_ascii_sum(self, key_str):
return sum(list(map(ord, key_str)))

def sum_digits(self, key_int):
sum = 0
for i in str(key_int):
sum += int(i)
return sum

def data_to_int(self, key):
if (type(key)) == str:
return self.to_ascii_sum(key)
elif (type(key)==int):
return self.sum_digits(key)
else:
raise Exception("key type must be either string or integer")

def mod_hashFunc(self, key):
return self.data_to_int(key)%self.slot_count

def __init__(self, slot_count):
assert slot_count > 0, Exception("table size must be greater than zero")
self.slot_count = slot_count
self.table = [SLLH() for...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here