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 1 year has passed since last update.

【Python】トーナメント優勝者

Posted at

作るもの

対戦カード(competitions)と結果(results)を与えて、最終的なトーナメントの勝者であるチーム名を返す関数。

実装

tournament_winner.py
def tournamentWinner(competitions, results):
    dict = {}  # 勝利回数をカウントするための空の辞書を作成

    # 各競技の結果とその対戦を順番に処理
    for i, comp in enumerate(competitions):
        if results[i]:  # もしi番目の競技の結果がTrue(チーム1の勝利)なら
            dict[comp[0]] = dict.get(comp[0], 0) + 1  # チーム1の勝利回数を辞書に追加または1増やす
        else:  # もしi番目の競技の結果がFalse(チーム2の勝利)なら
            dict[comp[1]] = dict.get(comp[1], 0) + 1  # チーム2の勝利回数を辞書に追加または1増やす

    # 辞書の中で最も多く勝利したチーム名を返す
    return max(dict, key=dict.get)


# テスト
if __name__ == "__main__":
    competitions = [
        ["HTML", "Java"],
        ["Java", "Python"],
        ["Python", "HTML"],
    ]
    results = [0, 1, 1]
    print(tournamentWinner(competitions, results))

参考

0
0
1

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?