Complete the code to add an element to the top of the stack.
def push(self, item): if self.size == len(self.array): self.resize() self.array[self.size] = [1] self.size += 1
self.size instead of item to assign the value.self.array instead of an index.The push method adds the given item to the top of the stack at index self.size.
Complete the code to remove and return the top element from the stack.
def pop(self): if self.size == 0: raise IndexError('pop from empty stack') self.size -= 1 item = self.array[[1]] return item
self.array[self.size - 1] which is incorrect after decrement.After decreasing self.size, the top element is at index self.size.
Fix the error in the resize method to double the array size correctly.
def resize(self): new_capacity = len(self.array) [1] 2 new_array = [None] * new_capacity for i in range(self.size): new_array[i] = self.array[i] self.array = new_array
To double the size, multiply the current length by 2.
Fill both blanks to create a dictionary comprehension that maps each word to its length if length is greater than 3.
{word: [1] for word in words if len(word) [2] 3}The dictionary maps each word to its length using len(word) only if the length is greater than 3.
Fill all three blanks to create a dictionary comprehension that maps uppercase keys to values only if values are positive.
result = { [1]: [2] for k, [2] in data.items() if [2] [3] 0 }The comprehension maps uppercase keys (k.upper()) to their values (v) only if the value is greater than zero.