中級 1件解決済み 正答率100.0%

Tournament Standings

Rank teams from match records.

このチャレンジで学べること

  • Parse match results from text
  • Aggregate points per team in a dictionary
  • Sort by points, then by name

問題

翻訳を表示

Rank teams from match records. Each record has the form First;Second;result, with no extra spaces. Result is win, loss, or draw from First’s perspective. A win earns 3 points, a draw earns 1 per team, and a loss earns 0. Count matches played, wins, draws, losses, and points for each team that appears. Sort by descending points, then ascending team name (ASCII order).

Return one semicolon-separated row per team: Name;Played;Won;Drawn;Lost;Points. Do not include a header or alignment spaces.

Implement tournament_standings(matches).

Input

matches: Match records, in any order.

Output

Return ranked rows with the six fields described above. Write numbers without leading zeros.

制約

0…10,000 valid records. Team names contain 1…30 ASCII letters and are case-sensitive. Each match has two different names. Repeated matches are separate games. Empty input gives an empty result.

例

例 1
入力:
matches = ["Falcons;Bears;win", "Bears;Owls;draw"]

期待される出力:
expected = ["Falcons;1;1;0;0;3", "Bears;2;0;1;1;1", "Owls;1;0;1;0;1"]
入力:
std::vector<std::string> matches = {"Falcons;Bears;win", "Bears;Owls;draw"};

期待される出力:
std::vector<std::string> expected = {"Falcons;1;1;0;0;3", "Bears;2;0;1;1;1", "Owls;1;0;1;0;1"};
入力:
string[] matches = new string[] {"Falcons;Bears;win", "Bears;Owls;draw"};

期待される出力:
string[] expected = new string[] {"Falcons;1;1;0;0;3", "Bears;2;0;1;1;1", "Owls;1;0;1;0;1"};

解説

Falcons have 3 points from their win. Bears and Owls each have 1 point from the draw, so Bears come first alphabetically. Bears played twice and lost once.

例 2
入力:
matches = ["Zebras;Ants;loss"]

期待される出力:
expected = ["Ants;1;1;0;0;3", "Zebras;1;0;0;1;0"]
入力:
std::vector<std::string> matches = {"Zebras;Ants;loss"};

期待される出力:
std::vector<std::string> expected = {"Ants;1;1;0;0;3", "Zebras;1;0;0;1;0"};
入力:
string[] matches = new string[] {"Zebras;Ants;loss"};

期待される出力:
string[] expected = new string[] {"Ants;1;1;0;0;3", "Zebras;1;0;0;1;0"};

解説

Loss is from Zebras’ perspective: Ants won and receive 3 points. Zebras receive 0.

解き方のヒント

まずは自分で考えてから、ヒントを1つずつ開いてみましょう。

ヒント 1

Accumulate stats for every team seen in the match list, even if it only loses.

ヒント 2

Each record updates two teams at once, and the result is always from the first team's perspective.

ヒント 3

After collecting totals, sort by points descending and then by team name ascending.

目安の計算量

O(m log t) time, O(t) extra space

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

ランキング

全体ランキング

コーダーごとのいちばん良い合格解答です。速く、メモリが少なく、評価が高い解答ほど上位になります。

# コーダー 言語 時間 メモリ AI 評価 解答
1 Н"Никита "Programmer" Python 968 ms - 5/5 コードを見る

他の人の解答から学ぶ

コミュニティの解答

ネタバレ注意: この下に解答が1件あります まずは自分で挑戦してみましょう。他の開発者のコードやAIレビューも見ることができます。 サインインして課題を解くと議論に参加できます。