Dies ist mein eigenes Memo.
▼ Frage
▼sample input
python
arr = [1,1,1,4,4,4,5,3]
▼sample output
python
1
▼my answer
python
def migratoryBirds(arr):
    x=0
    minType=0
    #Zählen Sie die Nummer jeder Typennummer
    for i in set(arr):
        if x<arr.count(i):
            x=arr.count(i)
            minType=i
        
     #Wenn die Anzahl der Sichtungen gleich ist, geben Sie der kleineren Typennummer Priorität
        elif x==arr.count(i):
            x=arr.count(i)
            if minType>i:
                minType=i
    return minType
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()
You would like to be able to find out which type of bird is most common given a list of sightings.
Recommended Posts