-
Notifications
You must be signed in to change notification settings - Fork 0
/
Migratory Birds.py
79 lines (48 loc) · 2.1 KB
/
Migratory Birds.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
'''You have been asked to help study the population of birds migrating across the continent. Each type of bird you are interested in will be identified by an integer value. Each time a particular kind of bird is spotted, its id number will be added to your array of sightings. You would like to be able to find out which type of bird is most common given a list of sightings. Your task is to print the type number of that bird and if two or more types of birds are equally common, choose the type with the smallest ID number.
For example, assume your bird sightings are of types
. There are two each of types and , and one sighting of type . Pick the lower of the two types seen twice: type
.
Function Description
Complete the migratoryBirds function in the editor below. It should return the lowest type number of the most frequently sighted bird.
migratoryBirds has the following parameter(s):
arr: an array of integers representing types of birds sighted
Input Format
The first line contains an integer denoting
, the number of birds sighted and reported in the array .
The second line describes as
space-separated integers representing the type numbers of each bird sighted.
Constraints
It is guaranteed that each type is , , , , or
.
Output Format
Print the type number of the most common bird; if two or more types of birds are equally common, choose the type with the smallest ID number.
Sample Input 0
6
1 4 4 4 5 3
Sample Output 0
4'''
#!/bin/python3
import math
import os
import random
import re
import sys
import collections
from collections import Counter
# Complete the migratoryBirds function below.
def migratoryBirds(arr):
dic = collections.Counter(arr)
ele = max(dic,key=dic.get)
for key,val in dic.items():
if(val==dic[ele]):
if(key<ele):
ele = key
return ele
return max(dic,key=dic.get)
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
arr_count = int(input().strip())
arr = list(map(int, input().rstrip().split()))
result = migratoryBirds(arr)
fptr.write(str(result) + '\n')
fptr.close()