Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to push a number onto the stack.
DSA Rust
stack.[1](num);Attempts:
3 left
💡 Hint
Common Mistakes
Using pop instead of push
Using peek which only views the top element
✗ Incorrect
To add an element to the stack, we use the push method.
2fill in blank
mediumComplete the code to pop the top element from the stack.
DSA Rust
let top = stack.[1]().unwrap();Attempts:
3 left
💡 Hint
Common Mistakes
Using push instead of pop
Using peek which does not remove the element
✗ Incorrect
To remove and get the top element from the stack, we use pop.
3fill in blank
hardFix the error in the code to convert a character digit to an integer.
DSA Rust
let num = (ch as u8 - b'0') as [1];
Attempts:
3 left
💡 Hint
Common Mistakes
Using char which is a character, not a number
Using String which is text
✗ Incorrect
We convert the byte difference to i32 to use it as a number.
4fill in blank
hardFill both blanks to perform the correct arithmetic operation on two numbers.
DSA Rust
let result = a [1] b; // addition let result = a [2] b; // multiplication
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' for addition
Using '/' for multiplication
✗ Incorrect
Use '+' for addition and '*' for multiplication.
5fill in blank
hardFill all three blanks to correctly evaluate a postfix expression step by step.
DSA Rust
for ch in expression.chars() { if ch.is_digit(10) { stack.[1]((ch as u8 - b'0') as i32); } else { let b = stack.[2]().unwrap(); let a = stack.[3]().unwrap(); let res = match ch { '+' => a + b, '-' => a - b, '*' => a * b, '/' => a / b, _ => 0, }; stack.push(res); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using peek instead of pop to get operands
Using remove which is not a stack method
✗ Incorrect
Push digits, pop two numbers for operation, then push result back.
