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 – Alternate front – rear sum
One of the most important data types is List in Python. Python provides various built-in methods to manipulate list items like append(), insert(), extend() and so on. Multiple approaches help in finding the alternate front-rear sum of an array.
The process involves adding the first element to the last element, the second element to the second-last element, and so on. For example, with the array [40, 50, 20, 30], the alternate front-rear sum is calculated as (40+30) + (50+20) = 140.
Using Conditional Statement
This approach uses a while loop with a conditional statement to pair elements from front and rear alternately ?
Algorithm
- Step 1 Initialize the list with elements
-
Step 2 Set
frontto 0 andrearto the last index - Step 3 Initialize an empty result list
-
Step 4 While
frontis less thanrear, add corresponding elements - Step 5 Handle the middle element for odd-length arrays
- Step 6 Print the result
Example
# Initialize list with elements
numbers = [10, 20, 30, 50]
front = 0
rear = len(numbers) - 1
result = []
# Iterate through the list from both ends
while front < rear:
result.append(numbers[front] + numbers[rear])
front += 1
rear -= 1
# Handle middle element for odd-length arrays
if front == rear:
result.append(numbers[front])
print("Alternating Front-Rear Sum:", *result)
Alternating Front-Rear Sum: 60 50
Using zip() Function
The zip() function combines elements from the original list and its reverse, making the pairing process more elegant ?
Example
# Initialize list with elements
numbers = [10, 20, 30, 50]
result = []
# Use zip to pair front and rear elements
for front, rear in zip(numbers, reversed(numbers)):
result.append(front + rear)
# Remove duplicate middle element for even-length arrays
result = result[:len(numbers)//2]
# Handle middle element for odd-length arrays
if len(numbers) % 2 != 0:
middle_index = len(numbers) // 2
result.append(numbers[middle_index])
print("Alternating Front-Rear Sum:", *result)
Alternating Front-Rear Sum: 60 50
Example with Odd-Length Array
Let's see how both approaches handle an array with odd number of elements ?
# Test with odd-length array
numbers = [5, 10, 15, 20, 25]
result = []
# Method 1: Using while loop
front = 0
rear = len(numbers) - 1
while front < rear:
result.append(numbers[front] + numbers[rear])
front += 1
rear -= 1
if front == rear:
result.append(numbers[front])
print("Result with odd array:", *result)
print("Sum breakdown: (5+25) + (10+20) + 15 =", sum(result))
Result with odd array: 30 30 15 Sum breakdown: (5+25) + (10+20) + 15 = 75
Comparison
| Method | Complexity | Readability | Best For |
|---|---|---|---|
| While Loop | O(n) | Moderate | Understanding the logic step by step |
| zip() Function | O(n) | High | Concise and Pythonic code |
Conclusion
Both approaches efficiently calculate the alternate front-rear sum with O(n) time complexity. The zip() method provides more readable code, while the conditional approach offers better understanding of the underlying logic.
