- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python β Alternate front β rear sum
One of the most important data types is List in the Python language. Various inbuilt methods to be available in python to manipulate the list items like append(), insert(), extend() and so on. Multiple approaches help in finding the alternate front-rear sum of an array. The array is given with n elements and the process involved are first element is added to the last element of the array, second element of an array is added to the second last element value and it continues until all the elements are added together. For example, letβs take the array,
[40, 50, 20, 30]
The alternate front β rear sum is calculated as (40+30) + (50+20) = 140
Approach
Approach 1: Using conditional statement
Approach 2: Using zip function
Approach 1: Python Program to alternate front-rear sum using conditional statement
The conditional statement is used to compare the num and ran, then the list elements undergo addition and returns the value as 60 and -50.
Algorithm
Step 1 β The list is initialized with a set of elements.
Step 2 β The variables like num is initialized to 0 and ran is set to the length of the given list minus 1.
Step 3 β Then the empty list is initialized.
Step 4 β When num is less than ran,
The flag is true, and then add the element at the indices num and ran in the input list initialized to an empty list.
The flag is false and adds the negative sum of the elements at indices num and ran in the input list initialized to an empty list.
Step 5 β Later increment the num and decrement the variable βranβ.
Step 6 β In the other case, num=ran and the element at index 1 in the input initialized to res.
Step 7 β Then the printing statement will print the alternate front-rear sum.
Example
#list is initialized with elements of strings list_1 = [10, 20, 30, 50] # variables are Initialized with values num = 0 ran = len(list_1) - 1 res = [] flag = True # while loop is used to iterate through the while loop while num < ran: if flag: res.append(list_1[num] + list_1[ran]) flag = False else: res.append((list_1[num] + list_1[ran])) flag = True num += 1 ran -= 1 # if loop uses the conditional statement to check for odd-numbered list array. if num == ran: res.append(list_1[num]) # returns the statement after the front-rear sum print("Alternating Front-Rear Sum:", *res)
Output
Alternating Front-Rear Sum: 60 50
Approach 2: Python Program to alternate front-rear sum using zip() function
In the below code, zip() function is used to combine the elements in the list from the front and rear, then calculate the alternating front-rear sum. The reversed() function is used to iterate through the list in reverse order.
Algorithm
Step 1 β The list is initialized with a set of elements.
Step 2 β Then the empty list is initialized.
Step 3 β The flag is true and then zip() function is used along with two parameters that is the list_1 and reversed list_1. Then the current elements are added to the empty list.
Step 4 β The flag is false and then zip() function is used along with two parameters that is the list_1 and reversed list_1. Then the current element will undergo a negative sum.
Step 5 β When the length of the list is odd, add the middle element to the empty list.
Step 6 β Then the printing statement will print the alternate front-rear sum.
Example
#list is initialized with elements of strings list_1 = [10, 20, 30, 50] #initializing the empty list empty = [] use = [] flag = True #for loop is used to iterate through the list using the zip function for a, b in zip(list_1, reversed(list_1)): if flag: empty.append(a + b) flag = False else: use.append((a + b)) flag = True # if loop uses the conditional statement to check for an even numbered list array. if len(list_1) % 2 != 0: empty.append(list_1[len(list_1) // 2]) # returns the statement after the front-rear sum print("Alternating Front-Rear Sum:", *empty)
Output
Alternating Front-Rear Sum: 60 50
Conclusion
The Python program is given for calculating the rear and front alternate sum for the given array of elements. The adding of elements is easier in case of an even length of the array list and on the other hand when the elements are having odd length of the list. The final number is added at the end of the list.