Hanwool Codes RSS Tag Admin Write Guestbook
2023-02-24 04:02:50

Introduction to Sets | HackerRank

 

Introduction to Sets | HackerRank

Use the set tool to compute the average.

www.hackerrank.com

 

Goal

 

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.

 

Task : You are given array of all the plants of height. You need to compute the average.

 

Solution

 

You need to use set data structure to solve this task.

 

Here is a step-by-step explanation of how this function works:


First, the input list is converted to a set using the set() function.

This removes any duplicate values from the input list, as sets only store unique values.

The length of the resulting set is computed using the len() function and stored in the total_length variable.
The sum of the values in the set is computed using the sum() function and stored in the total_values variable.
The average is computed by dividing the total_values by the total_length, and the result is returned.

 

# Introduction to Sets

def average(array):
    # your code goes here
    set_array = set(array)
    total_length = len(set_array)
    total_values = sum(set_array)
    
    return total_values / total_length
        


if __name__ == '__main__':
    n = int(input())
    arr = list(map(int, input().split()))
    result = average(arr)
    print(result)
Hanwool Codes. Designed by 코딩재개발.