- 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 program to get average heights of distinct entries
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
Advertisements