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.

Updated on: 25-Aug-2023

58 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements