Skip to content

Instantly share code, notes, and snippets.

@hoverlover
Created April 30, 2010 03:57
Show Gist options
  • Save hoverlover/384716 to your computer and use it in GitHub Desktop.
Save hoverlover/384716 to your computer and use it in GitHub Desktop.
# Pick up to 10 names, making sure to draw from each "hat" at least once
class Name < Struct.new(:first, :last); end
max_names = 10
hat1 = [Name.new('Abraham', 'Lincoln'), Name.new('George', 'Washington')]
hat2 = [Name.new('Thomas', 'Jefferson'), Name.new('James', 'Madison'), Name.new('Theodore', 'Roosevelt'), Name.new('John', 'Kennedy')]
hat3 = [Name.new('Calvin', 'Coolidge'), Name.new('Ronald', 'Regan'), Name.new('Woodrow', 'Wilson')]
[hat1, hat2, hat3].sort{|a,b| b.size <=> a.size}.inject{|memo, names| memo.zip(names)}.flatten.compact[0, max_names]
=> [#<struct Name first="Thomas", last="Jefferson">, #<struct Name first="Calvin", last="Coolidge">, #<struct Name first="Abraham", last="Lincoln">, #<struct Name first="James", last="Madison">, #<struct Name first="Ronald", last="Regan">, #<struct Name first="George", last="Washington">, #<struct Name first="Theodore", last="Roosevelt">, #<struct Name first="Woodrow", last="Wilson">, #<struct Name first="John", last="Kennedy">]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment