Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python - Given a list of integers that represents a decimal value, increment the last element by 1
When working with a list of integers that represents a decimal value, we sometimes need to increment it by 1. This is similar to adding 1 to a number, but we need to handle carry operations when digits are 9.
Problem Understanding
Given a list like [1, 2, 3] representing the number 123, we want to increment it to get [1, 2, 4] representing 124. If the last digit is 9, we need to handle carries properly.
Using Recursive Approach
Here's a recursive solution that handles the carry operation when incrementing ?
def increment_num(digits, index):
# If the current digit is less than 9, simply increment it
if digits[index] < 9:
digits[index] += 1
return
# If digit is 9, set it to 0 and carry over
digits[index] = 0
# If we're at the first digit and it becomes 0, we need a new digit
if index == 0:
digits.insert(0, 1)
return
# Recursively handle the carry for the previous digit
increment_num(digits, index - 1)
# Example 1: Normal increment
digits = [1, 2, 3]
print("Original list:", digits)
increment_num(digits, len(digits) - 1)
print("After increment:", digits)
Original list: [1, 2, 3] After increment: [1, 2, 4]
Handling Carry Operations
When the last digit is 9, we need to handle the carry operation ?
def increment_num(digits, index):
if digits[index] < 9:
digits[index] += 1
return
digits[index] = 0
if index == 0:
digits.insert(0, 1)
return
increment_num(digits, index - 1)
# Example 2: With carry operation
digits = [1, 2, 9]
print("Original list:", digits)
increment_num(digits, len(digits) - 1)
print("After increment:", digits)
# Example 3: Multiple carries
digits = [9, 9, 9]
print("\nOriginal list:", digits)
increment_num(digits, len(digits) - 1)
print("After increment:", digits)
Original list: [1, 2, 9] After increment: [1, 3, 0] Original list: [9, 9, 9] After increment: [1, 0, 0, 0]
Iterative Approach
We can also solve this problem using an iterative approach ?
def increment_iterative(digits):
# Start from the last digit
carry = 1
for i in range(len(digits) - 1, -1, -1):
total = digits[i] + carry
digits[i] = total % 10
carry = total // 10
# If no carry, we're done
if carry == 0:
break
# If there's still a carry, add it to the front
if carry:
digits.insert(0, carry)
# Test the iterative approach
digits = [9, 8, 7]
print("Original list:", digits)
increment_iterative(digits)
print("After increment:", digits)
Original list: [9, 8, 7] After increment: [9, 8, 8]
Comparison
| Approach | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Recursive | O(n) | O(n) | Understanding the logic |
| Iterative | O(n) | O(1) | Production code |
Conclusion
Both recursive and iterative approaches work well for incrementing a list representing a decimal number. The iterative approach is more efficient in terms of space complexity, while the recursive approach is easier to understand conceptually.
