Football(CodeEval)

「チーム毎のメンバーが所属する国のリスト」を「国別のチーム一覧」に変換する問題。
日本語にすると一見複雑で、途中で何やってるかわからなくなる。
nested な配列生成が少し汚い。injectにしてもあまり変わらない。
メソッド分割をすれば良いのかもしれないが、名前が複雑になりそうなので、とりあえず最初に書いたまま。
 

CHALLENGE DESCRIPTION:

People around the world watch football matches and root for different football teams. Some people are fans of Real Madrid, some like Barcelona, some pull for Atletico Madrid, while others do not miss a single match with FC Bayern Munich. The teams would like to know people in which countries cheer for them. So, let’s help them!

INPUT SAMPLE:

The first argument is a path to a file. Each row includes a test case with lists of countries. Lists are separated by pipelines ‘|’. Each list includes football teams that people in these countries root for.

1 2 3 4 | 3 1 | 4 1
19 11 | 19 21 23 | 31 39 29

OUTPUT SAMPLE:

For each football team, print a list of countries where people root for them. Separate each team by a semicolon ‘;’ and a space. All output should be sorted.

1:1,2,3; 2:1; 3:1,2; 4:1,3;
11:1; 19:1,2; 21:2; 23:2; 29:3; 31:3; 39:3;

CONSTRAINTS:

  1. The number of countries lists can be from 3 to 20.
  2. Each list contains a different number of football teams: from 1 to 7.
  3. The number of test cases is 40.

My Code

#!/usr/bin/env ruby -w

def teams_by_country(teams)
  countries = teams.flatten.sort.uniq
  results = []
  countries.each do |country|
    result = []
    teams.each_with_index do |team, i|
      result << "#{i + 1}" if team.include?(country)
    end
    results << "#{country}:#{result.join(",")};"
  end
  results.join(" ")
end

ARGF.each_line do |line|
  teams = line.chomp.split(" | ").map { |t| t.split.map(&:to_i) }
  puts teams_by_country(teams)
end