A set is an unordered collection of elements without duplicate entries.
When printed, iterated or converted into a sequence, its elements will appear in an arbitrary order.
Basically, sets are used for membership testing and eliminating duplicate entries.
Now, let's use our knowledge of sets and help Mickey.
Ms. Gabriel Williams is a botany professor at District College. One day, she asked her student Mickey to compute the average of all the plants with distinct heights in her greenhouse.
Average = Sum of Distinct Heights / Total Number of Distinct Heights
Complete the average function in the editor below.
average has the following parameters:
- int arr: an array of integers
- float: the resulting float value rounded to 3 places after the decimal
The first line contains the integer, N, the size of arr.
The second line contains the N space-separated integers, arr[i].
Here, set [161, 167, 170, 171, 174, 176, 182, 154] is the set containing the distinct heights. Using the sum() and len() functions, we can compute the average.
Average = 1355 / 8 = 169.375