HackerRankInterview QuestionsPythonPython InterviewRandom

HackerRank Introduction to Sets solution in python | python question solution

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:

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 is the set containing the distinct heights. Using the sum() and len() functions, we can compute the average.

Problem solution in Python 2 programming.

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))

Problem solution in Python 3 programming.

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

Problem solution in pypy programming.

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

Problem solution in pypy3 programming.

def average(array):
# your code goes here
x=set(array)
return float(sum(x)/len(x))

Leave a Reply

10 Best Artificial Intelligence Software|artificial intelligence tools 5 nft games to earn money | Best NFT games for earn crypto Earn Money From Minting NFTs| How to mint NFT for free Top 10 Things You Need To Know About Python List | python lists functions 10 Popular PHP frameworks for web developers| best php frameworks 12 Tips On How To Become a Python Developer | python For beginner 12 Best Nodejs Frameworks for App Development in 2022 how to create google web stories, Steps to create web stories Top 10 Features in Angular 13 Every Developer Should Know | Angular 13 Features 10 Best Angular UI Libraries | angular ui components | angular Project 10 Best Web Development Frameworks in 2022 Frontend & Backend 18 Best Open-Source and Free Database Software | best database software for beginners Top 10+ Best Java IDEs & Online Java Compilers | best java ide for beginners top 10 besic to andvance java books |java books for beginer Top 5 Themes For Blogger, professional blogger theme download BEST Python Courses Online,Top 10 Courses to Learn Python in 2022 Top 13 python libraries for data science