Reverse a Number using StacksStacks 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. Algorithm and Steps to FollowAlgorithm:- Initialize an empty stack
- Convert the number to be reversed into a string
- Push each digit of the string onto the stack
- Initialize a reversed number variable to 0
- 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
- Convert the reversed number from an integer to a string
- Return the reversed string
Steps:- Create an empty list to use as a stack
- Convert the number to a string using str()
- Use a for loop to iterate through each character in the string
- In the loop, convert the character to an integer using int()
- Append each integer to the stack using stack.append()
- Initialize a reversed_num variable to 0
- Use a while loop to loop as long as the stack is not empty
- Inside the loop:
- Pop a digit from the stack using stack.pop()
- Multiply reversed_num by 10
- Add the popped digit to reversed_num
- Convert reversed_num back to a string using str()
- 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 ImplementationOutput: Explanation - Defines a function called Reverse() that takes the number to be reversed as a parameter (num).
- Initializes an empty list called stack that will act as the stack.
- 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.
- Starts a for loop that iterates through each character in num_str.
- Inside the loop, convert the character to an integer using int() and append it to the stack using stack.append().
- Initializes a variable reversed_num to 0. This will hold the reversed number.
- It starts a while loop that will run as long as the stack is not empty.
- Pops the last digit from stack using stack.pop() and stores it in popped_digit. This gets the top digit in the LIFO order.
- Multiplies the reversed_num by 10 to shift it over before adding the next digit.
- Adds the popped digit to reversed_num to append it.
- After the while loop, convert reversed_num to a string using str().
- Returns the reversed string.
- 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.
|