Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help building two methods for a program that builds a hash table. I have

ID: 3743941 • Letter: I

Question

I need help building two methods for a program that builds a hash table.

I have attached what I have already built below.                                                             class intHash(object):     """A hash table with integer keys"""         def __init__(self, numBuckets):         """Create an empty table with numBuckets buckets"""         self.buckets = []         self.numBuckets = numBuckets         for i in range(numBuckets):             self.buckets.append([])                 def addEntry(self, key, value):         """Assumes key an int. Adds an entry."""         hashBucket = self.buckets[key%self.numBuckets]             hashBucket.append((key, value))             def getValue(self, key):         """Assumes key an int.            Returns value associated with key"""         hashBucket = self.buckets[key%self.numBuckets]         values=[]         for e in hashBucket:             if e[0] == key:                 values.append(e[1])                     return values         def __str__(self):         result = '{'         for b in self.buckets:             for e in b:                 result = result + str(e[0]) + ':' + str(e[1]) + ','         return result[:-1] + '}' #result[:-1] omits the last comma             def findValue(self, value, hashkey):         """Returns '(value) Prensent' if its found in the has table using the         key hashkey and returns '(value) Not Found' if value is not in the table"""                 if valpresent:             print(str(hashkey) + " present")         else:             print(str(hashkey) + " not present")         #A Possible integer key for names def getNameKey(name):     """returns the hashkey for a string name"""     key=0     namelist=list(name)     for letter in name:         if letter != " " and letter !='.':             key=key+ord(letter)     return key         name = intHash(10) infile = open("onlyNames.txt", 'r') for line in infile:     key = getNameKey(line[:-1])     name.addEntry(key,line)     infile.close()         

Write a findValue method for intHash as specified below. def findValue(self, value, hashkey): """Returns (value) Present' if is found in the hash table using the key hashkey and returns '(value) Not Found' if value is not in the table""" Write a deleteValue method for intHash as specified below: def deleteValue(self, value, hashkey): n"Deletes value from the hash table using the key function hashkey"""

Explanation / Answer

## onlyNames.txt

Sebin
Naman
Raghav
Sebin
Sebjm
Rahul
Shobhit
Tashi
Saurabh
Sherlock

## Code.py

## Changed the print statements to return statements in the findValue method

## Added deleteKey method

## Added code to test if it works (remove them and add your code if you like)

## Changed name.addEntry(key, line) to name.addEntry(key, line[: -1]) because it was inserting a newline as well, this lead to bugs

class intHash(object):

"""A hash table with integer keys"""

def __init__(self, numBuckets):

"""Create an empty table with numBuckets buckets"""

self.buckets = []

self.numBuckets = numBuckets

for i in range(numBuckets):

self.buckets.append([])

def addEntry(self, key, value):

"""Assumes key an int. Adds an entry."""

hashBucket = self.buckets[key % self.numBuckets]

hashBucket.append((key, value))

def getValue(self, key):

"""Assumes key an int. Returns value associated with key"""

hashBucket = self.buckets[key % self.numBuckets]

values = []

for e in hashBucket:

if e[0] == key:

values.append(e[1])

return values

def __str__(self):

result = '{'

for b in self.buckets:

for e in b:

result = result + str(e[0]) + ':' + str(e[1]) + ','

return result[: -1] + '}'

#result[: -1] omits the last comma

def findValue(self, value, hashkey):

"""Returns '(value) Prensent' if its found in the has table using the key hashkey and returns '(value) Not Found' if value is not in the table"""

values = self.getValue(hashkey)

if value in values:

return (value + " present")

else:

return (value + " not present")

def deleteKey(self, value, hashKey):

"""Deletes a key from the table"""

bucket = self.buckets[hashKey % self.numBuckets]

bucket = [element for element in bucket if element != (hashKey, value)]

self.buckets[hashKey % self.numBuckets] = bucket

# A Possible integer key for names

def getNameKey(name):

"""returns the hashkey for a string name"""

key = 0

namelist = list(name)

for letter in name:

if letter != " " and letter != '.':

key = key + ord(letter)

return key

name = intHash(10)

infile = open("onlyNames.txt", 'r')

for line in infile:

key = getNameKey(line[: -1])

name.addEntry(key, line[: -1])

infile.close()

print(str(name) + " ")

print(name.findValue("Sebin", getNameKey("Sebin")) + " ")

name.deleteKey("Sebin", getNameKey("Sebin"))

print(str(name) + " ")

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote