Open In App

Python – Redistribute Trimmed Values

Last Updated : 26 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a List, redistribute values trimmed by K.

Examples:

Input : test_list = [4, 5, 2, 8, 9, 3, 1, 2, 4], K = 2 
Output : [5.0, 11.0, 12.0, 6.0, 4.0] 
Explanation : 4, 5, 2, 4 are trimmed, totalling to 15, which dividing by 5 ( size left ) equates 3, which is added to each element.

Input : test_list = [4, 5, 2, 8, 9], K = 2 
Output : [28.0] 
Explanation : 4, 5, 8, 9 are trimmed, totalling to 26, which dividing by 1 ( size left ) equates 26, which is added to each element. 

Approach: Using slice() + sum()

In this, the total sum is computed using sum, and the list is trimmed using slice(), then trimmed weight is computed by subtraction by total weight. Redistributed by adding uniform weight to each element.

Python3




# Python3 code to demonstrate working of
# Redistribute Trimmed Values
# Using slice() + sum()
 
# initializing list
test_list = [4, 5, 2, 8, 9, 3, 1, 2, 4]
 
# printing original lists
print("The original list is : " + str(test_list))
 
# initializing K
K = 2
 
# getting full list sum
full_sum = sum(test_list)
 
# trimming list
trimd_list = test_list[K:len(test_list) - K]
 
# getting trimmed list sum
trim_sum = sum(trimd_list)
 
# getting value to add to each element
add_val = (full_sum - trim_sum) / len(trimd_list)
 
# adding values
res = [ele + add_val for ele in trimd_list]
 
# printing result
print("Redistributed list : " + str(res))


Output

The original list is : [4, 5, 2, 8, 9, 3, 1, 2, 4]
Redistributed list : [5.0, 11.0, 12.0, 6.0, 4.0]

Time Complexity: O(n)
Auxiliary Space: O(1)

Method 2: Using a for loop

  • Initialize the input list test_list.
  • Print the original list.
  • Initialize the value of K, which is the number of elements to be trimmed from both ends of the list.
  • Calculate the sum of all elements in the list using the sum() function and store it in the variable full_sum.
  • Initialize an empty list trimd_list and a variable trim_sum to store the sum of trimmed elements.
  • Use a for loop to iterate over the range K to len(test_list) – K – 1 and append each element to trimd_list.
  • Also, add the element to trim_sum.
  • Calculate the value to add to each element of trimd_list by subtracting trim_sum from full_sum and dividing it by the length of trimd_list.
  • Initialize an empty list res to store the redistributed values.
  • Use a for loop to iterate over each element of trimd_list and add the calculated value to it. Append the result to res.
  • Print the redistributed list.

Python3




# Python3 code to demonstrate working of
# Redistribute Trimmed Values
# Using for loop
 
# initializing list
test_list = [4, 5, 2, 8, 9, 3, 1, 2, 4]
 
# printing original lists
print("The original list is : " + str(test_list))
 
# initializing K
K = 2
 
# getting full list sum
full_sum = sum(test_list)
 
# initializing variables for trimmed list and its sum
trimd_list = []
trim_sum = 0
 
# trimming list and calculating its sum
for i in range(K, len(test_list) - K):
    trimd_list.append(test_list[i])
    trim_sum += test_list[i]
 
# getting value to add to each element
add_val = (full_sum - trim_sum) / len(trimd_list)
 
# adding values
res = []
for ele in trimd_list:
    res.append(ele + add_val)
 
# printing result
print("Redistributed list : " + str(res))


Output

The original list is : [4, 5, 2, 8, 9, 3, 1, 2, 4]
Redistributed list : [5.0, 11.0, 12.0, 6.0, 4.0]

Time complexity: O(n), where n is the length of the input list. 
Auxiliary space: O(n), because we are creating new lists to store the trimmed and redistributed values.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads