Advertisement
uopspop

Untitled

Jan 26th, 2022
976
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import array
  2.  
  3. class ArrayImpl3:
  4.     def __init__(self, size):
  5.         self.array = [None] * size
  6.         self.i_end = -1
  7.  
  8.     def add_by_index(self, i_add, val):
  9.        
  10.         if self.i_end + 1 == len(self.array):
  11.             self.expand_space()
  12.  
  13.         if i_add > self.i_end + 1 | i_add < 0:
  14.             return
  15.  
  16.         # step01 : move all value one slot after
  17.         for i in range(self.i_end, i_add - 1, -1):
  18.             self.array[i + 1] = self.array[i]
  19.             self.array[i] = None
  20.  
  21.         self.array[i_add] = val
  22.         self.i_end += 1
  23.        
  24.  
  25.     def add_by_value(self, val):
  26.         self.add_by_index(self.i_end + 1, val)
  27.  
  28.     def expand_space(self):
  29.         array_new = [None] * (len(self.array) * 2)
  30.        
  31.         for i in range(len(self.array)):
  32.             array_new[i] = self.array[i]
  33.  
  34.         self.array = array_new
  35.  
  36.     def search_by_index(self, index):
  37.         if index > self.i_end | index < 0:
  38.             return None
  39.         return self.array[index]
  40.  
  41.     def search_by_value(self, val):
  42.         for i in range(self.i_end):
  43.             if self.array[i] == val:
  44.                 return self.array[i]
  45.  
  46.         return None
  47.  
  48.     def remove_by_index(self, index):
  49.         if index > self.i_end | index < 0:
  50.             return
  51.         self.array[index] = None
  52.        
  53.         for i in range(self.i_end, i_add - 1, -1):
  54.             self.array[i + 1] = self.array[i]
  55.             self.array[i] = None
  56.  
  57.         self.i_end -= 1
  58.  
  59.     def remove_by_value(self, val):
  60.         # search  
  61.         for i in range(self.i_end):
  62.             if self.array[i] == val:
  63.                 self.remove_by_index(i)
  64.  
  65. if __name__ == "__main__":
  66.     # initialize
  67.     myarray = ArrayImpl3(5)
  68.    
  69.     # add O(1)
  70.     myarray.add_by_value(9)
  71.     myarray.add_by_value(11)
  72.     myarray.add_by_value(2)
  73.     myarray.add_by_value(98)
  74.     myarray.add_by_value(35)
  75.  
  76.     # add by value O(1) + expand O(n)
  77.     myarray.add_by_value(44)
  78.  
  79.     # add by index O(n)
  80.     i_add = 1
  81.     myarray.add_by_index(i_add, 50)
  82.  
  83.     # search by value O(n)
  84.     val001 = myarray.search_by_value(98)
  85.  
  86.     # search by index O(1)
  87.     val002 = myarray.search_by_index(4)
  88.  
  89.     # remove by value O(n)
  90.     myarray.remove_by_value(2)
  91.  
  92.     # remove by index O(n)
  93.     i_remove = 3
  94.     myarray.remove_by_index(i_remove)
  95.    
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement