|
| 1 | +A set is an unordered collection of elements without duplicate entries. |
| 2 | +When printed, iterated or converted into a sequence, its elements will appear in an arbitrary order. |
| 3 | + |
| 4 | +Example |
| 5 | + |
| 6 | +>>> print set() |
| 7 | +set([]) |
| 8 | + |
| 9 | +>>> print set('HackerRank') |
| 10 | +set(['a', 'c', 'e', 'H', 'k', 'n', 'r', 'R']) |
| 11 | + |
| 12 | +>>> print set([1,2,1,2,3,4,5,6,0,9,12,22,3]) |
| 13 | +set([0, 1, 2, 3, 4, 5, 6, 9, 12, 22]) |
| 14 | + |
| 15 | +>>> print set((1,2,3,4,5,5)) |
| 16 | +set([1, 2, 3, 4, 5]) |
| 17 | + |
| 18 | +>>> print set(set(['H','a','c','k','e','r','r','a','n','k'])) |
| 19 | +set(['a', 'c', 'r', 'e', 'H', 'k', 'n']) |
| 20 | + |
| 21 | +>>> print set({'Hacker' : 'DOSHI', 'Rank' : 616 }) |
| 22 | +set(['Hacker', 'Rank']) |
| 23 | + |
| 24 | +>>> print set(enumerate(['H','a','c','k','e','r','r','a','n','k'])) |
| 25 | +set([(6, 'r'), (7, 'a'), (3, 'k'), (4, 'e'), (5, 'r'), (9, 'k'), (2, 'c'), (0, 'H'), (1, 'a'), (8, 'n')]) |
| 26 | +Basically, sets are used for membership testing and eliminating duplicate entries. |
| 27 | + |
| 28 | +Task |
| 29 | + |
| 30 | +Now, let's use our knowledge of sets and help Mickey. |
| 31 | + |
| 32 | +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. |
| 33 | + |
| 34 | +Formula used: |
| 35 | + |
| 36 | +Input Format |
| 37 | + |
| 38 | +The first line contains the integer, , the total number of plants. |
| 39 | +The second line contains the space separated heights of the plants. |
| 40 | + |
| 41 | +Constraints |
| 42 | + |
| 43 | + |
| 44 | +Output Format |
| 45 | + |
| 46 | +Output the average height value on a single line. |
| 47 | + |
| 48 | +Sample Input |
| 49 | + |
| 50 | +10 |
| 51 | +161 182 161 154 176 170 167 171 170 174 |
| 52 | +Sample Output |
| 53 | + |
| 54 | +169.375 |
| 55 | +Explanation |
| 56 | + |
| 57 | +Here, set is the set containing the distinct heights. Using the sum() and len() functions, we can compute the average. |
| 58 | + |
0 commit comments