<\/span><\/h2>\n\n\n\nA set is an unordered collection of elements without duplicate entries.<\/p>\n\n\n\n
When printed, iterated or converted into a sequence, its elements will appear in an arbitrary order.<\/p>\n\n\n\n
Example<\/strong><\/p>\n\n\n\n>>> print set()\nset([])\n\n>>> print set('HackerRank')\nset(['a', 'c', 'e', 'H', 'k', 'n', 'r', 'R'])\n\n>>> print set([1,2,1,2,3,4,5,6,0,9,12,22,3])\nset([0, 1, 2, 3, 4, 5, 6, 9, 12, 22])\n\n>>> print set((1,2,3,4,5,5))\nset([1, 2, 3, 4, 5])\n\n>>> print set(set(['H','a','c','k','e','r','r','a','n','k']))\nset(['a', 'c', 'r', 'e', 'H', 'k', 'n'])\n\n>>> print set({'Hacker' : 'DOSHI', 'Rank' : 616 })\nset(['Hacker', 'Rank'])\n\n>>> print set(enumerate(['H','a','c','k','e','r','r','a','n','k']))\nset([(6, 'r'), (7, 'a'), (3, 'k'), (4, 'e'), (5, 'r'), (9, 'k'), (2, 'c'), (0, 'H'), (1, 'a'), (8, 'n')])<\/pre>\n\n\n\nBasically, sets are used for membership testing and eliminating duplicate entries.<\/p>\n\n\n\n
<\/span>Task<\/strong><\/span><\/h2>\n\n\n\nNow, let’s use our knowledge of sets and help Mickey.<\/p>\n\n\n\n
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.<\/p>\n\n\n\n
Formula used:<\/p>\n\n\n\n
Average = Sum of Distinct Heights \/ Total Number of Distinct Heights<\/strong><\/p>\n\n\n\n<\/span>Input Format<\/strong><\/span><\/h2>\n\n\n\nThe first line contains the integer, N<\/strong><\/em>, the total number of plants.<\/p>\n\n\n\nThe second line contains the N<\/em><\/strong> space separated heights of the plants.<\/p>\n\n\n\n<\/span>Constraints<\/strong><\/span><\/h2>\n\n\n\n0 < N <= 100<\/strong><\/p>\n\n\n\n<\/span>Output Format <\/strong><\/span><\/h2>\n\n\n\nOutput the average height value on a single line.<\/p>\n\n\n\n
Sample Input<\/strong><\/p>\n\n\n\n10\n161 182 161 154 176 170 167 171 170 174<\/pre>\n\n\n\nSample Output<\/strong><\/p>\n\n\n\n169.375<\/pre>\n\n\n\nExplanation<\/strong><\/p>\n\n\n\nHere, set ([154, 161, 167, 170, 171, 174, 176, 182]) <\/strong>is the set containing the distinct heights. Using the sum() and len() functions, we can compute the average.<\/p>\n\n\n\nAverage = 1355 \/ 8 = 169.375<\/strong><\/p>\n\n\n\n<\/span>Solution – Introduction to Sets in Python – Hacker Rank Solution <\/strong><\/span><\/h2>\n\n\n\n<\/span>Python 3 <\/strong><\/span><\/h3>\n\n\n\ndef average(array):\n # your code goes here\n array = set(array)\n return sum(array) \/ len(array)\n\nif __name__ == '__main__':\n n = int(input())\n arr = list(map(int, input().split()))\n result = average(arr)\n print(result)<\/pre>\n\n\n\n