Skip to content

Instantly share code, notes, and snippets.

@DawnPaladin
Last active January 8, 2017 03:34
Show Gist options
  • Save DawnPaladin/3817249702b20acc420d9eed08e8dc9f to your computer and use it in GitHub Desktop.
Save DawnPaladin/3817249702b20acc420d9eed08e8dc9f to your computer and use it in GitHub Desktop.
pile = sock_pile([:black, :white, :red], 2)
def advanced(sock_pile)
sock_book = Hash.new {|h,k| h[k] = [] }
pairs = []
sock_pile.each do |sock|
if sock_book[sock].length > 0
pairs.push([sock, sock_book[sock].pop])
else
sock_book[sock].push(sock)
end
end
pairs
end
p advanced(pile)
#=> [[:red, :red], [:white, :white], [:white, :white], [:black, :black], [:black, :black], [:red, :red]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment