Reverse a Number using Stacks

Stacks are one of the most fundamental data structures in computer science. By following a last in, first out (LIFO) order, stacks provide a simple yet powerful way to temporarily store data, reverse order, and implement undo functionality. In Python, lists can easily be used as stacks thanks to their inherent push and pop methods. In this article, we will explore how to leverage stacks in Python to reverse the digits of a number. By pushing digits onto a stack and then popping them off in reverse order, we can efficiently flip the order of the digits in a numeric value. We will look at stack implementations in Python, walk through the algorithm, analyze its complexity, and explore other use cases where stacks shine. By the end, you will have a solid understanding of how to harness the simple elegance of stacks to build algorithms like digit reversal in Python.

Reverse a Number using Stacks

Algorithm and Steps to Follow

Algorithm:

  1. Initialize an empty stack
  2. Convert the number to be reversed into a string
  3. Push each digit of the string onto the stack
  4. Initialize a reversed number variable to 0
  5. While the stack is not empty:
    • Pop the top digit from the stack
    • Multiply the reversed number by 10 and add the popped digit to it
  6. Convert the reversed number from an integer to a string
  7. Return the reversed string

Steps:

  1. Create an empty list to use as a stack
  2. Convert the number to a string using str()
  3. Use a for loop to iterate through each character in the string
  4. In the loop, convert the character to an integer using int()
  5. Append each integer to the stack using stack.append()
  6. Initialize a reversed_num variable to 0
  7. Use a while loop to loop as long as the stack is not empty
  8. Inside the loop:
    • Pop a digit from the stack using stack.pop()
    • Multiply reversed_num by 10
    • Add the popped digit to reversed_num
  9. Convert reversed_num back to a string using str()
  10. Return the reversed string

This algorithm utilizes the LIFO behaviour of a stack to reverse the order of the digits in the number. We can efficiently flip the numbers by pushing them onto the stack and then popping and appending them sequentially.

Python Implementation

Output:

Reverse a Number using Stacks

Explanation

  1. Defines a function called Reverse() that takes the number to be reversed as a parameter (num).
  2. Initializes an empty list called stack that will act as the stack.
  3. Converts the input number num to a string using str() and stores it in num_str. This is done so we can access each digit via indexing.
  4. Starts a for loop that iterates through each character in num_str.
  5. Inside the loop, convert the character to an integer using int() and append it to the stack using stack.append().
  6. Initializes a variable reversed_num to 0. This will hold the reversed number.
  7. It starts a while loop that will run as long as the stack is not empty.
  8. Pops the last digit from stack using stack.pop() and stores it in popped_digit. This gets the top digit in the LIFO order.
  9. Multiplies the reversed_num by 10 to shift it over before adding the next digit.
  10. Adds the popped digit to reversed_num to append it.
  11. After the while loop, convert reversed_num to a string using str().
  12. Returns the reversed string.
  13. Calls Reverse() with an example number and prints output.

So, in summary:

  • The digits are pushed onto a stack
  • Then, it popped off in the LIFO order
  • Appended to the reversed number
  • By multiplying by 10 each iteration to shift digits over

This implements the standard algorithm for reversing a number using a stack data structure in Python.

Complexity Analysis

We loop through the number's digits once to push them on the stack and once again to pop them off, so the overall time complexity is O(N), where N is the number of digits.

The space complexity is O(N) since, in the worst case, we store all N digits on the stack.






Latest Courses