0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【python】最も多い鳥の種類を求めるプログラム

Posted at

#【python】最も多い鳥の種類を求めるプログラム

自分用のメモです。

▼設問

  • 目の前を通った鳥の種類の番号(正の整数)を順番にlistにいれる。
  • 最も多く目撃された鳥の種類を返す。
  • ただし、複数の種類が同数回目撃された場合は、一番小さな種類の番号を返す。

URL

▼sample input

arr = [1,1,1,4,4,4,5,3]

▼sample output

1

▼my answer

def migratoryBirds(arr):
    x=0
    minType=0

    #各種類番号の数を数える
    for i in set(arr):
        if x<arr.count(i):
            x=arr.count(i)
            minType=i
        
     #目撃回数が同じ場合は、小さい種類番号を優先する
        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()

**・sightings** 目撃(動物) 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.

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?