Created
January 25, 2016 11:18
-
-
Save vladfaust/a2e842bf75c4a6b37d32 to your computer and use it in GitHub Desktop.
Was given a playing cards sample array. The task was to sort it and make all the members unique.
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
@canonical_hash = Hash.new | |
def init | |
i = 0 | |
[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 'Jack', 'Queen', 'King', 'Aces' ].map do |value| | |
@canonical_hash[value] = (i += 1) | |
end | |
end | |
def card_sort (array) | |
array.uniq! | |
array.sort_by do |item| | |
@canonical_hash[item] | |
end | |
end | |
init | |
p "Sorted & unique: #{ card_sort([ 1, 'Queen', 'King', 'Jack', 3, 5, 7, 2, 2, 'Jack' ]) }" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment