Last active
January 8, 2017 03:34
-
-
Save DawnPaladin/3817249702b20acc420d9eed08e8dc9f to your computer and use it in GitHub Desktop.
Advanced, working sock sorting for https://medium.com/@DawnPaladin/sorting-socks-e31e254bfa6f
This file contains 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
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