bwukki

Random week 5 stuff

Oct 30th, 2018
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.73 KB | None | 0 0
  1. class hash_table:
  2.     def __init__(self,size):
  3.         self.data = [None for i in range (size)]
  4.         self.size = size
  5.         self.num_items = 0
  6.  
  7.     def resize(self):
  8.         temp = hash_table(self.size*2)
  9.         for i in self.data:
  10.             if i is not None:
  11.                 temp.insert(i)
  12.         self.data = temp.data
  13.         self.size = temp.size
  14.  
  15.     def insert(self,input):
  16.         if self.num_items / self.size > 0.5:
  17.             self.resize()
  18.         hashed_location = self.hash(input)
  19.         if self.data[hashed_location] is None: #if the location is empty, insert there
  20.             self.data[hashed_location] = input
  21.             self.num_items +=1
  22.         else:
  23.             starting_point = hashed_location+1
  24.             end_point = self.size
  25.             found = self.find_replace_empty(input,starting_point,end_point) #otherwise, go from that point to the end of the list looking for an empty slot
  26.             if not found:
  27.                 starting_point = 0
  28.                 end_point = hashed_location
  29.                 found = self.find_replace_empty(input,starting_point,hashed_location) #if there is still no more empty slots, start from the beginning of the list
  30.             if not found:
  31.                 raise Exception("Something went wrong my dude") #oh fuck the list is full
  32.  
  33.     def find_replace_empty(self,input,lower,upper):
  34.         found = False
  35.         i = lower
  36.         while i < upper and not found:
  37.             if self.data[i] is None:
  38.                 self.data[i] = input
  39.                 self.num_items += 1
  40.                 found = True
  41.             i += 1
  42.         return found
  43.  
  44.     def __contains__(self, item):
  45.         return self.search(item)
  46.  
  47.     def delete(self, item): #note that the special case if this were chained is that you would need to shift items on the chain if one got deleted anywhere but the end
  48.         item_location = self.get_loc(item)
  49.         if item_location is not False:
  50.             self.data[item_location] = None
  51.  
  52.  
  53.     def get_loc(self,input):
  54.         if self.data[self.hash(input)] is None:
  55.             return False
  56.         elif self.data[self.hash(input)] == input:
  57.             return self.hash(input)
  58.         else:
  59.             found = False
  60.             i = self.hash(input)+1
  61.             while not found and i < self.size: #fringe case, refactor?
  62.                 if self.data[i] is None:
  63.                     return False
  64.                 elif self.data[i] == input:
  65.                     return i
  66.                 i += 1
  67.             i = 0
  68.             while not found and i < self.hash(input): #fringe case, refactor?
  69.                 if self.data[i] is None:
  70.                     return False
  71.                 elif self.data[i] == input:
  72.                     return i
  73.                 i += 1
  74.             raise Exception("Oh fuck somehow the hash table is full")
  75.  
  76.     def search(self,input):
  77.         if self.data[self.hash(input)] is None:
  78.             return False
  79.         elif self.data[self.hash(input)] == input:
  80.             return True
  81.         else:
  82.             found = False
  83.             i = self.hash(input)+1
  84.             while not found and i < self.size: #fringe case, refactor?
  85.                 if self.data[i] is None:
  86.                     return False
  87.                 elif self.data[i] == input:
  88.                     return True
  89.                 i += 1
  90.             i = 0
  91.             while not found and i < self.hash(input): #fringe case, refactor?
  92.                 if self.data[i] is None:
  93.                     return False
  94.                 elif self.data[i] == input:
  95.                     return True
  96.                 i += 1
  97.             raise Exception("Oh fuck somehow the hash table is full")
  98.  
  99.     def hash(self,input):
  100.         nums = [ord(char) for char in input]
  101.         result = 0
  102.         for i in range(len(nums)):
  103.             result += (i+1) * nums[i]
  104.         return result % self.size
  105.         return 4
  106.  
  107. x = hash_table(20)
  108. for i in range(15):
  109.     x.insert("penis")
  110. print(x.data)
  111. print(x.size)
  112.  
  113.  
  114. import random
  115. import time
  116. #print(random.sample(range(1000),5))
  117.  
  118. def binary_prototype(a_list,item):
  119.     return binary_search_recursive(a_list,item,0,len(a_list)-1)
  120.  
  121. def binary_search_recursive(a_list,item,start,end):
  122.     midpoint = start + (end - start) // 2
  123.     if a_list[midpoint] == item:
  124.         return True
  125.     if end - start == 0:
  126.         return False
  127.     if a_list[midpoint] > item:
  128.         return binary_search_recursive(a_list,item,start,midpoint-1)
  129.     if a_list[midpoint] < item:
  130.        return binary_search_recursive(a_list,item,midpoint+1,end)
  131.  
  132.  
  133. start_time = time.time()
  134. x = [i for i in range(10000000)]
  135. print(binary_prototype(x,102))
  136. print("Time to run binary search recursive no slice = ", time.time()-start_time)
  137.  
  138. #15 and 16 todo...
Add Comment
Please, Sign In to add comment