Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

python code, pls help me fix the error, when I run this code, there is no output CODE import Assignment2 import random import time import

python code, pls help me fix the error, when I run this code, there is no output

image text in transcribed

CODE

import Assignment2 import random import time import copy import sys # Acknowledgements: # I worked with class... (if applicable) # I used the following sites ... (if applicable) def display_hash(hash_table): for i in range(len(hash_table)): print(i, hash_table[i]) print() def hash_chaining_build(arr): size = int(len(arr) / 10) hash_table = [[] for _ in range(size)] for key in arr: hash_key = hash_function(key, size) hash_table[hash_key].append(key) print(hash_table) return hash_table def hash_probing_build(arr): size = len(arr) * 2 hash_table = [None for _ in range(size)] for key in arr: hash_key = hash_function(key, size) i = 0 while hash_table[hash_key]: i += 1 hash_key = hash_function_probing(hash_key, size, i) hash_table[hash_key] = key return hash_table def hash_chaining_search(hash_table, arr): size = len(hash_table) found = [False] * len(arr) for j in range(len(arr)): key = arr[j] hash_key = hash_function(key, size) for item in hash_table[hash_key]: if item == key: found[j] = True break return found def hash_probing_search(hash_table, arr): size = len(hash_table) found = [False] * len(arr) # found = [False for i in range(size)] for j in range(len(arr)): key = arr[j] hash_key = hash_function(key, size) i = 0 while hash_table[hash_key] != key: i += 1 hash_key = hash_function_probing(hash_key, size, i) found[j] = True return found def hash_function(key, size): return key % size def hash_function_probing(hash_key, size, i): a, b, c = 5, 7, 9 hash_key = (hash_key + a * i ** 2 + b * i + c) % size return hash_key def run_algs(algs, sizes, trials): dict_algs = {} for alg in algs: dict_algs[alg[2]] = {} for size in sizes: for alg in algs: dict_algs[alg[2]][size] = 0 for trial in range(1, trials + 1): arr = random_list(size) arr_copy = copy.copy(arr) random.shuffle(arr_copy) for alg in algs: start_time = time.time() build = alg[0] ds = build(arr) search = alg[1] found = search(ds, arr_copy) if size == sizes[0]: print(alg[2]) print(ds) print(found) end_time = time.time() net_time = end_time - start_time dict_algs[alg[2]][size] += 1000 * net_time return dict_algs def random_list(size): arr = [i**3 for i in range(size)] random.shuffle(arr) return arr def mini_test(): size = 100 arr = random_list(size) ht_chaining = hash_chaining_build(arr) ht_proving = hash_probing_build(arr) display_hash(ht_chaining) display_hash(ht_proving) random.shuffle(arr) found = hash_chaining_search(ht_chaining, arr) print("Hash Chaining Search: ") print(found) found = hash_probing_search(ht_proving, arr) print("Hash Probing Search: ") print(found) def big_test(): assn = "Assignment6" sizes = [100, 1000, 10000] algs = [(hash_probing_build, hash_probing_search, "Hash Probing"), (hash_chaining_build, hash_chaining_search, "Hash Chaining")] trials = 1 title = "Runtime of String Search Algorithms (Build and Search)" dict_algs = run_algs(algs, sizes, trials) Assignment2.print_times(dict_algs) Assignment2.plot_times(dict_algs, sizes, trials, algs, title, assn + '.png') def main(): # mini_test() big_test() if __name__ == "__main__": main() 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Murach's SQL Server 2012 For Developers

Authors: Bryan Syverson, Joel Murach, Mike Murach

1st Edition

1890774693, 9781890774691

More Books

Students also viewed these Databases questions