Introduction to Sets Hacker Rank solution – Queslers

Problem: Introduction to Sets Hacker Rank solution

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:

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, , the size of .
The second line contains the  space-separated integers, .

Constraints

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

Introduction to Sets Hacker Rank solution in python 2

n = float(raw_input())
plant = set()
sum_p = 0
for x in raw_input().split(' '):
  if x not in plant:
    plant.add(x)
    sum_p += float(x)
print sum_p/float(len(plant))

Introduction to Sets Hacker Rank solution in python 3

def average(array):
    return sum(set(array))/len(set(array))

Introduction to Sets Hacker Rank solution in pypy

def average(array):
    s = set(array)
    return sum(s)/len(s)

Introduction to Sets Hacker Rank solution in pypy 3

def average(array):
    # your code goes here
    x=set(array)
    return float(sum(x)/len(x))
Introduction to Sets Hacker Rank Solution Review:

In our experience, we suggest you solve this Introduction to Sets Hacker Rank Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

Introduction to Sets problem is available on Hacker Rank for Free, if you are stuck anywhere between a compilation, just visit Queslers to get Introduction to Sets Hacker Rank Solution

Conclusion:

I hope this Introduction to Sets Hacker Rank Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Hacker Rank, Leetcode, Codechef, Codeforce Solution.

This Problem is intended for audiences of all experiences who are interested in learning Programming in a business context; there are no prerequisites.

Keep Learning!

More Hacker Rank Problem & Solutions >>

Staircase Hacker Rank Solution

A Very Big Sum Hacker Rank Solution

Diagonal Difference Hacker Rank Solution

Nested Lists Hacker Rank Solution

Lists Hacker Rank Solution

Leave a Reply

Your email address will not be published. Required fields are marked *