Skip to content

Instantly share code, notes, and snippets.

@caged caged/coaches.csv
Last active May 7, 2016

Embed
What would you like to do?
Using Ruby, sort each coach by consecutive season tenures. Some seasons have two coaches which implies that the coach was fired/removed mid-season. The result should be something like [[name, start_season, end_season], [name, start_season, end_season]] where the order is by ascending start_season.
season coach
1994-95 Bill Blair
1995-96 Bill Blair
1990-91 Bill Musselman
1989-90 Bill Musselman
2006-07 Dwane Casey
2005-06 Dwane Casey
1999-00 Flip Saunders
2014-15 Flip Saunders
2004-05 Flip Saunders
2003-04 Flip Saunders
2002-03 Flip Saunders
2001-02 Flip Saunders
1995-96 Flip Saunders
1996-97 Flip Saunders
1997-98 Flip Saunders
1998-99 Flip Saunders
2000-01 Flip Saunders
1992-93 Jimmy Rodgers
1991-92 Jimmy Rodgers
2008-09 Kevin McHale
2004-05 Kevin McHale
2009-10 Kurt Rambis
2010-11 Kurt Rambis
2008-09 Randy Wittman
2006-07 Randy Wittman
2007-08 Randy Wittman
2013-14 Rick Adelman
2012-13 Rick Adelman
2011-12 Rick Adelman
2015-16 Sam Mitchell
1992-93 Sidney Lowe
1993-94 Sidney Lowe
@donpdonp

This comment has been minimized.

Copy link

commented May 7, 2016

How does this look

{:name=>"Bill Musselman", :startyear=>1989, :endyear=>1991}
{:name=>"Jimmy Rodgers", :startyear=>1991, :endyear=>1993}
{:name=>"Sidney Lowe", :startyear=>1992, :endyear=>1994}
{:name=>"Bill Blair", :startyear=>1994, :endyear=>1996}
{:name=>"Flip Saunders", :startyear=>1995, :endyear=>1900}
{:name=>"Flip Saunders", :startyear=>2000, :endyear=>2005}
{:name=>"Kevin McHale", :startyear=>2004, :endyear=>2005}
{:name=>"Dwane Casey", :startyear=>2005, :endyear=>2007}
{:name=>"Randy Wittman", :startyear=>2006, :endyear=>2009}
{:name=>"Kevin McHale", :startyear=>2008, :endyear=>2009}
{:name=>"Kurt Rambis", :startyear=>2009, :endyear=>2011}
{:name=>"Rick Adelman", :startyear=>2011, :endyear=>2014}
{:name=>"Flip Saunders", :startyear=>2014, :endyear=>2015}
{:name=>"Sam Mitchell", :startyear=>2015, :endyear=>2016}
require 'csv'

coaches = CSV.read('coaches.csv', {headers: true}).map do |row| 
  startyear, endyear = row[0].split('-')
  {name: row[1], 
   startyear: startyear.to_i , 
   endyear: "#{startyear.to_s[0,2]}#{endyear}".to_i} 
end

def merged(record)
  record.sort_by{|season| season[:startyear]}.reduce([]) do |memo, season|
    if memo.size == 0
      memo << season
    else
      if memo.last[:endyear] == season[:startyear]
        memo.last[:endyear] = season[:endyear]
      else
        memo << season
      end
    end
    memo
  end
end

puts coaches.group_by{|c| c[:name]}
       .map{|name, record| merged(record)}.flatten
@donpdonp

This comment has been minimized.

Copy link

commented May 7, 2016

there's a Y2K problem with Flip Sanders :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.