Introduction to Sets - Python- HackerRank

admin
By -
0

 Introduction to Sets

In this post we going to solving simple [problem from hackerrank python series problems  where we just  finding average from formula  and for this we using set to  remove all duplicate items from list.


For this problems we have all things provided we jus  need to  complete this function. and using this fomula to  apply here-

Average = Total Sum of Elements from Array List /  Size of Array

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.

Problem-Hackerrank

Solution-


Here We have to use  Set() because set remove  duplicate items  from list  

Code-

def average(array):
    # your code goes here
    unique_values = set(array)  # Convert list
    #set to remove duplicates
    total = sum(unique_values)
    average = total / len(unique_values)
    return average

Post a Comment

0Comments

Put Your Thought or Query Here

Post a Comment (0)