PYTHON BASIC PROBLEMS SOLUTIONS(HACKERRANK) (Day-26)

preview_player
Показать описание
HII Young Coders our channel will give you Hackerrank solutions.It will be very useful for you Kindly make use of it.

PROBLEM:

PROBLEM LINK:

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/Totlal 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 size of .arr
The second line contains the N space-separated integers, .arr[i]

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, ([154,161,167,170,171,174,176,182]) set is the set containing the distinct heights. Using the sum() and len() functions, we can compute the average.

Average=1355/8 =169.375

SOLUTION:

def average(array):
m=set(array)
l=len(m)
sum0=sum(m)
ou=sum0/l
return ou
Рекомендации по теме