May-02-2020, 08:59 AM
Hello,
I have the following code and I would like it to give results in about 20 seconds. Thanks...
I have the following code and I would like it to give results in about 20 seconds. Thanks...
import string import random import time random.seed(1059442) global max_load_factor max_load_factor = 0.1 #-------------------------------- def primeGreaterThan2(num): while True: if num % 2 == 1: isPrime = True for x in range(3,int(num**0.5),2): if num % x == 0: isPrime = False break if isPrime: return num num += 1 N = primeGreaterThan2(1000) #------------------------------------- #N=1000 arr = [ None for _ in range(N)] #------------------------------------- def CreatNewItem(): days = ["Mon", "Tue", "Wed" , "Thu" , "Fri", "Sat"] letters = ['W','X','Z','Y'] sl = ['1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6'] for x in letters: while True: p = random.randint(0,15) if sl[p].isdigit(): sl[p] = x break s = ''.join(sl) d = random.randint(0,5) day = days[d] money = random.randint(10,100) a = [s,day,money] return a #-------------------------------------- def Ηash(key, tablesize): sum = 0 for pos in range(len(key)): sum = sum + ord(key[pos])*10**pos h = sum % tablesize return h #-------------------------------------- def rehash(oldhash , tablesize): rh = ( oldhash + 1 ) % tablesize return rh #---------------------------------------- def put2 (arr,a,N,lenght,collisions): if float(lenght)/float(N) >= max_load_factor: (arr,N,collisions) = Resize(arr,N,lenght,collisions) key = a[0] i = Ηash(key,N) j =0 while (True): if arr[i] is None: arr[i] = a lenght = lenght + 1 break elif arr[i][0] == key: arr[i][2] = arr[i][2] + a[2] arr[i][1] = arr[i][1] + a[1] break else: if j == 0 : collisions = collisions +1 #gia na υπολογιζω τα collition μονο στην πρωτη συγκρουση j = 1 i = rehash(i,N) return (lenght,N,arr,collisions ) #---------------------------------------- def Resize(arr,N,lenght,collisions): N = primeGreaterThan2(2*N) #N=2*N collisions = 0 lenght = 0 arr2 = [ None for _ in range(N)] for p in arr: if p is not None: (lenght,N,arr2,collisions)=put2(arr2,p,N,lenght,collisions) return (arr2,N,collisions) #---------------------------------------- def Find_max_money(arr,N): max_money = 0 for i in range(0,N): if arr[i] == None: continue elif arr[i][2] > max_money: max_money = arr[i][2] key = arr[i][0] return (key,max_money) #---------------------------------------- def Find_most_visits(arr,N): max_len = 0 for i in range(0,N): if arr[i] == None: continue elif len(arr[i][1]) > max_len: max_len = len(arr[i][1]) key = arr[i][0] visits = int(max_len/3) return (key,visits) #----------------------------------------- def Find_most_visited_day(arr,N): days = ["Mon", "Tue", "Wed" , "Thu" , "Fri", "Sat"] visits = [0,0,0,0,0,0] max_v = 0 d = 0 for i in range(0,len(days)): for j in range(0,N): if arr[j] == None: continue else: v = arr[j][1].count(days[i]) visits[i] = visits[i] + v for i in range(0,len(visits)): if visits[i] > max_v: max_v = visits[i] d = i mday = days[d] return(mday,max_v) #----------------------------------------- def find_item(arr,N,key): hashed_key = Hash(key,N) if arr[hashed_key] is None: raise KeyError if arr[hashed_key][0] != key: original_key = hashed_key while arr[hashed_key][0] != key: hashed_key = rehash(hashed_key,N) if arr[hashed_key] is None: raise KeyError if hashed_key == original_key: raise KeyError return hashed_key #---------------------------------------- def get_item(arr,N,key): index = find_item(arr,N,key) return arr[index] #----------------------------------------- l = 0 collisions = 0 print("-----------\n load factor:",max_load_factor) t0 = time.time() i=0 while i!=1000000: b = CreatNewItem() (l,N,arr,collisions) = put2(arr,b,N,l,collisions) i=i+1 t1 = time.time() - t0 print('\ntime is {:0.20f}'.format(t1)) print("collisions",collisions) (card,money) = Find_max_money(arr,N) print("The card with the most money is:",card,"and the money:",money) (card2,visits) = Find_most_visits(arr,N) print("The card with the most visits is:",card2,"and the vistits:",visits) (day,max_visits) = Find_most_visited_day(arr,N) print("The day with the most visits is:",card2,"and the vistits:",visits)