Python program to get average heights of distinct entries

PythonServer Side ProgrammingProgramming

Suppose we have a set of heights there may be some duplicate entries as well. We have to find the average of distinct entries of these heights.

So, if the input is like heights = [96,25,83,96,33,83,24,25], then the output will be 52.2 because the unique elements are [96,25,83,33,24], so sum is 96 + 25 + 83 + 33 + 24 = 261, average is 261/5 = 52.2.

To solve this, we will follow these steps −

  • h_set := a set from heights to remove duplicates

  • return sum of h_set items / size of h_set set

Example

Let us see the following implementation to get better understanding

def solve(heights):
   h_set = set(heights)
   return sum(h_set)/len(h_set)

heights = [96,25,83,96,33,83,24,25]
print(solve(heights))

Input

[96,25,83,96,33,83,24,25]

Output

52.2
raja
Published on 12-Oct-2021 07:09:11

Advertisements