0
0
DSA Pythonprogramming~10 mins

Dynamic Stack Using Resizable Array in DSA Python - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to add an element to the top of the stack.

DSA Python
def push(self, item):
    if self.size == len(self.array):
        self.resize()
    self.array[self.size] = [1]
    self.size += 1
Drag options to blanks, or click blank then click option'
Aitem
Bself.size
Clen(self.array)
Dself.array
Attempts:
3 left
💡 Hint
Common Mistakes
Using self.size instead of item to assign the value.
Trying to assign to self.array instead of an index.
2fill in blank
medium

Complete the code to remove and return the top element from the stack.

DSA Python
def pop(self):
    if self.size == 0:
        raise IndexError('pop from empty stack')
    self.size -= 1
    item = self.array[[1]]
    return item
Drag options to blanks, or click blank then click option'
A0
Bself.size
Clen(self.array) - 1
Dself.size - 1
Attempts:
3 left
💡 Hint
Common Mistakes
Accessing self.array[self.size - 1] which is incorrect after decrement.
Returning the wrong element index.
3fill in blank
hard

Fix the error in the resize method to double the array size correctly.

DSA Python
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
Drag options to blanks, or click blank then click option'
A//
B+
C-
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of multiplication.
Using integer division which reduces size.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps each word to its length if length is greater than 3.

DSA Python
{word: [1] for word in words if len(word) [2] 3}
Drag options to blanks, or click blank then click option'
Alen(word)
B>
C<
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong comparison operator.
Mapping to the word itself instead of its length.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase keys to values only if values are positive.

DSA Python
result = { [1]: [2] for k, [2] in data.items() if [2] [3] 0 }
Drag options to blanks, or click blank then click option'
Ak.upper()
Bv
C>
Dk
Attempts:
3 left
💡 Hint
Common Mistakes
Using the original key instead of uppercase.
Using wrong comparison operator or variable names.