In: Computer Science
# ArrayStack # TODO: push and pop using array stack with doubling technique. import numpy as np from future import print_function # Definisi class ArrayStack class ArrayStack(object): def __init__(self): self.data = np.zeros(20, dtype=np.int) self.count = 0 def isEmpty(self): pass # ubah saya def size(self): pass # ubah saya def push(self, obj): pass # ubah saya def pop(self): pass # ubah saya #-------------------------- # End of class if __name__ == "__main__": stack = ArrayStack() randn = np.random.permutation(1000) for i in range(1000): stack.push(randn[i]) #### for i in range(100): print(stack.pop()) ####
import numpy as np # from future import print_function # Definisi class ArrayStack class ArrayStack(object): def __init__(self): self.data = np.zeros(20, dtype=np.int) self.count = 0 def isEmpty(self): return self.count == 0 def size(self): return self.count def push(self, obj): if self.count == len(self.data): newData = np.zeros(2 * self.count, dtype=np.int) for i in range(len(self.data)): self.data[i] = newData[i] self.data = newData self.data.put(self.count, obj) self.count += 1 def pop(self): elem = self.data[self.count - 1] np.delete(self.data, self.count - 1) self.count -= 1 return elem # -------------------------- # End of class if __name__ == "__main__": stack = ArrayStack() randn = np.random.permutation(1000) for i in range(1000): stack.push(randn[i]) #### for i in range(100): print(stack.pop()) ####