Last active
July 8, 2019 20:19
-
-
Save srinidhi-lwt/94275cc9448e0eb7672a3ff1dc143815 to your computer and use it in GitHub Desktop.
Set Checker
This file contains hidden or 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 set?(*cards) | |
shapes = cards.map(&:shape).uniq | |
colours = cards.map(&:colour).uniq | |
fills = cards.map(&:fill).uniq | |
count = cards.map(&:count).uniq | |
[shapes, colours, fills, count].all? do |attribute| | |
attribute.length.odd? | |
end | |
end | |
Card = Struct.new(:shape, :colour, :fill, :count) | |
card_1 = Card.new(:diamond, :purple, :lined, :two) | |
card_2 = Card.new(:oval, :purple, :blank, :two) | |
card_3 = Card.new(:squiggle, :purple, :filled, :two) | |
set?(card_1, card_2, card_3) | |
=> true | |
card_4 = Card.new(:diamond, :purple, :lined, :two) | |
card_5 = Card.new(:oval, :purple, :blank, :two) | |
card_6 = Card.new(:squiggle, :purple, :lined, :three) | |
set?(card_4, card_5, card_6) | |
=> false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment