Task1:. from task1 import Hash_Table. def testlen(): #C
Added on -2020-06-06
| 7 pages| 1158 words| 71 views
Trusted by 2+ million users, 1000+ happy students everyday
Showing pages 1 to 3 of 7 pages
Task1:from task1 import Hash_Tabledef testlen(): #Correct test lengthtest = Hash_Table() lengthtest.__setitem__("favNumber", "5") length = lengthtest.__len__() assert length == 1, "Oops! Wrong number!"def testGetItem(): test = Hash_Table() test.__setitem__("bf", "PM") bfTest = test.__getitem__("bf") assert bfTest == "PM", "Get item Non-functional"def testSetItem(): #Correct test setItemTest = Hash_Table() setItemTest.__setitem__("favNumber", "Five") #You need to set the item first favNumber = setItemTest.__getitem__("favNumber") assert favNumber == "Five", "Set item Non-functional!"def testcontains(): test = Hash_Table() test.__setitem__("favSubject", "Maths") subjectTest = test.__contains__("favSubject") assert subjectTest == False, "incorrect input __contains__"print(myHash)if __name__ == '__main__': testSetItem() testlen() #testcontains() testGetItem() print("All tests were passed !")
Task2:Table SizeWall timeComments210000209987400000399989202361Task 3:n = 1500global collision_countcollision_count = 0class Foo(): def __eq__(self, other): global collision_count collision_count += 1 return id(self) == id(other) def __hash__(self): #return id(self) # @John Machin: yes, I know! return 1objects = [Foo() for i in xrange(n)]d = {}for o in objects: d[o] = 1print collision_count
Task 4import randomimport mathdef linear_probe(random_list, m): hash_table = [None]*m count = 0 for i in random_list: stop = False hash = i % m while not stop: if hash_table[hash] == None: hash_table[hash] = i stop = True else: hash = (hash+1) % m count +=1 return countdef quadratic_probe(random_list, m): hash_table = [None]*m count = 0 for i in random_list: j = 1 stop = False hash = i % m while not stop: if hash_table[hash] == None: hash_table[hash] = i stop = True else: hash = (hash+j) % m count +=1 j = int((math.sqrt(j)+1) ** 2) return countdef comp_lists(list1, list2):
Found this document preview useful?
You are reading a preview Upload your documents to download or Become a Desklib member to get accesss