Last active
May 7, 2016 04:40
-
-
Save stopdropandrew/bf52a06a603f763bbea9a760be7d193e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'csv' | |
coach_years=CSV.read('coaches.csv')[1..-1] | |
def parse_coach_year(coach_year) | |
start_year, end_year_suffix = coach_year[0].split('-') | |
end_year_prefix = start_year < '2000' ? '19' : '20' | |
end_year = "#{end_year_prefix}#{end_year_suffix}" | |
return coach_year[1], start_year.to_i, end_year.to_i | |
end | |
i = 0 | |
stints = coach_years.sort_by(&:reverse).each_with_object([]) do |coach_year, coach_stints| | |
coach, start_year, end_year = parse_coach_year(coach_year) | |
last_stint = coach_stints.last | |
if [coach, start_year] == last_stint&.fetch_values(:coach, :end_year) | |
last_stint[:end_year] = end_year | |
else | |
coach_stints << { coach: coach, start_year: start_year, end_year: end_year } | |
end | |
end | |
stints.sort_by{|s| [s[:start_year], s[:end_year]]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment