Last active
February 27, 2017 12:37
-
-
Save rkotov93/f1ac183ac22198e2df9ee93a9e107772 to your computer and use it in GitHub Desktop.
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
def full_suit?(suit) | |
suit.each do |k, v| | |
return false if v < 1 | |
end | |
true | |
end | |
def solution(a) | |
ranks = (2..9).map(&:to_s) + %w(T J Q K A) | |
suits = %w(H D C S) | |
deck = {} | |
sets = {} | |
ranks.each do |r| | |
deck[r] = {} | |
sets[r] = 0 | |
suits.each do |s| | |
deck[r][s] = 0 | |
end | |
end | |
a.each do |c| | |
r = c[0] | |
s = c[1] | |
deck[r][s] += 1 | |
if full_suit?(deck[r]) | |
sets[r] += 1 | |
deck[r].keys.each do |k| | |
deck[r][k] -= 1 | |
end | |
end | |
end | |
return sets.values.min | |
end | |
# --------------------------------------------------- | |
# Should return 0 | |
cards = ["9C", "KS", "AC", "AH", "8D", "4C", "KD", "JC", "7D", "9D", "2H", "7C", "3C", "7S", "5C", "6H", "TH"] | |
puts solution(cards) | |
# --------------------------------------------------- | |
cards = [] | |
ranks = (2..9).map(&:to_s) + %w(T J Q K A) | |
suits = %w(H D C S) | |
ranks.each do |r| | |
suits.each do |s| | |
cards << "#{r}#{s}" | |
end | |
end | |
# Should return 1 | |
puts solution(cards) | |
# --------------------------------------------------- | |
cards = [] | |
2.times do | |
ranks.each do |r| | |
suits.each do |s| | |
cards << "#{r}#{s}" | |
end | |
end | |
end | |
# Should return 2 | |
puts solution(cards) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment