Table of Contents
ToggleQuestion
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.
Example
>>> print set()
set([])
>>> print set('HackerRank')
set(['a', 'c', 'e', 'H', 'k', 'n', 'r', 'R'])
>>> print set([1,2,1,2,3,4,5,6,0,9,12,22,3])
set([0, 1, 2, 3, 4, 5, 6, 9, 12, 22])
>>> print set((1,2,3,4,5,5))
set([1, 2, 3, 4, 5])
>>> print set(set(['H','a','c','k','e','r','r','a','n','k']))
set(['a', 'c', 'r', 'e', 'H', 'k', 'n'])
>>> print set({'Hacker' : 'DOSHI', 'Rank' : 616 })
set(['Hacker', 'Rank'])
>>> print set(enumerate(['H','a','c','k','e','r','r','a','n','k']))
set([(6, 'r'), (7, 'a'), (3, 'k'), (4, 'e'), (5, 'r'), (9, 'k'), (2, 'c'), (0, 'H'), (1, 'a'), (8, 'n')])
Basically, sets are used for membership testing and eliminating duplicate entries.
Task
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.
Formula used:
Average= Sum of Distinct Heights / Total Number of Distinct Heights
Function Description
Complete the average function in the editor below.
average has the following parameters:
- int arr: an array of integers
Returns
- float: the resulting float value rounded to 3 places after the decimal
Input Format
The first line contains the integer, N, the total number of plants. The second line contains the N space separated heights of the plants.
Constraints
0<N<=1000
Sample Input
STDIN Function ----- -------- 10 arr[] size N = 10 161 182 161 154 176 170 167 171 170 174 arr = [161, 181, ..., 174]
Sample Output
169.375
Explanation
Here, set is the set containing the distinct heights. Using the sum() and len() functions, we can compute the average.
Solution
def average(array):
s=set(array)
return sum(s)/len(s)
if __name__ == '__main__':
n = int(input())
arr = list(map(int, input().split()))
result = average(arr)
print(result)
Explanation
This code defines a function named average
that calculates the average of a given array of numbers. Here’s a step-by-step explanation of the code:
- The function
average
takes an array as input. - It creates a set
s
from the array by using theset()
function. This eliminates any duplicate values in the array, as sets only store unique elements. - The sum of the elements in the set
s
is calculated using thesum()
function. - The length of the set
s
is calculated using thelen()
function. - The sum of the elements is divided by the length of the set to obtain the average.
- The average value is then returned by the function.
The code also includes a block that executes when the script is run directly (not imported as a module):
- It prompts the user to enter an integer
n
using theinput()
function. - It reads a line of space-separated numbers from the user and converts them into a list of integers using the
map()
andsplit()
functions. - The
average
function is called with the obtained list as an argument, and the result is assigned to the variableresult
. - Finally, the calculated average is printed to the console using the
print()
function.
If you find anything wrong in this Solution, feel free to reach us in the comment section.