Created
August 25, 2018 11:51
-
-
Save dpolivaev/f990c951891395a5a94cd267a612e6c3 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
/* | |
81 cards all unique | |
shuffle deck | |
draw 12 cards | |
maybe<set> findsSET(cqrds) | |
1 card : 4 features | |
feature : values | |
features: shape, shading, colorm nuöber | |
shape: circle, triangle, square | |
shading: filled, hashed, outlined | |
nuöber. one, two, three | |
color, R G B | |
a set is always 3 cards in which | |
for everz feature | |
the said feature | |
on 3 cards | |
are all different or All equal | |
*/ | |
import org.scalatest.Matchers | |
case class Card private(val features: List[Feature]) | |
object Card { | |
def apply(shape: Shape, shading: Shading, number: Number, color: Color): Card = new Card(List(shape, shading, number, color)) | |
private def apply(features: List[Feature]) {} | |
} | |
trait Feature | |
trait Shape extends Feature | |
case object circle extends Shape | |
case object triangle extends Shape | |
case object square extends Shape | |
trait Shading extends Feature | |
case object filled extends Shading | |
case object hashed extends Shading | |
case object outlined extends Shading | |
trait Number extends Feature | |
case object one extends Number | |
case object two extends Number | |
case object three extends Number | |
trait Color extends Feature | |
case object R extends Color | |
case object G extends Color | |
case object B extends Color | |
class SomeTest extends org.scalatest.FunSpec with Matchers { | |
type Set = (Card, Card, Card) | |
def findSet(cards: List[Card]): Option[Set] | |
= { | |
for { | |
firstIndex <- 0 until (cards.size - 2) | |
middleIndex <- firstIndex + 1 until (cards.size - 1) | |
lastIndex <- middleIndex + 1 until cards.size | |
} { | |
val selectedCards = List(cards(firstIndex), cards(middleIndex), cards(lastIndex)) | |
val selectedCardFeatures = selectedCards.map(_.features).transpose | |
if (selectedCardFeatures.forall(isValidSet(_))) return Some(setOf(selectedCards)) | |
} | |
None | |
} | |
private def isValidSet(features: List[Feature]): Boolean = allSame(features) || allDifferent(features) | |
private def allSame(features: List[Feature]) = { | |
features.tail.forall(_ == features.head) | |
} | |
private def allDifferent(features: List[Feature]) = { | |
features.toSet.size == features.size | |
} | |
private def setOf(cards: List[Card]) = | |
(cards(0), cards(1), cards(2)) | |
describe("set finder") { | |
it("valid set of circles") { | |
(findSet(List(Card(circle, filled, one, R), Card(circle, filled, one, R), Card(circle, filled, one, R))) | |
should be(Some((Card(circle, filled, one, R), Card(circle, filled, one, R), Card(circle, filled, one, R))))) | |
} | |
it("valid set of triangles") { | |
(findSet(List(Card(triangle, filled, one, R), Card(triangle, filled, one, R), Card(triangle, filled, one, R))) | |
should be(Some((Card(triangle, filled, one, R), Card(triangle, filled, one, R), Card(triangle, filled, one, R))))) | |
} | |
it("valid set of different shapes") { | |
(findSet(List(Card(circle, filled, one, R), Card(square, filled, one, R), Card(triangle, filled, one, R))) | |
should be(Some((Card(circle, filled, one, R), Card(square, filled, one, R), Card(triangle, filled, one, R))))) | |
} | |
it("invalid set of shapes") { | |
(findSet(List(Card(circle, filled, one, R), Card(circle, filled, one, R), Card(triangle, filled, one, R))) | |
should be(None)) | |
} | |
it("valid set of circles from cards 1, 2 and 4") { | |
(findSet(List(Card(circle, filled, one, R), Card(circle, filled, one, R), Card(triangle, filled, one, R), Card(circle, filled, one, R))) | |
should be(Some((Card(circle, filled, one, R), Card(circle, filled, one, R), Card(circle, filled, one, R))))) | |
} | |
it("valid set of circles from cards 1, 3 and 4") { | |
(findSet(List(Card(circle, filled, one, R), Card(circle, filled, one, R), Card(triangle, filled, one, R), Card(circle, filled, one, R))) | |
should be(Some((Card(circle, filled, one, R), Card(circle, filled, one, R), Card(circle, filled, one, R))))) | |
} | |
it("valid set of circles from cards 2, 3 and 4") { | |
(findSet(List(Card(triangle, filled, one, R), Card(circle, filled, one, R), Card(circle, filled, one, R), Card(circle, filled, one, R))) | |
should be(Some((Card(circle, filled, one, R), Card(circle, filled, one, R), Card(circle, filled, one, R))))) | |
} | |
it("invalid set of shadings") { | |
(findSet(List(Card(circle, filled, one, R), Card(circle, filled, one, R), Card(circle, hashed, one, R))) | |
should be(None)) | |
} | |
it("invalid set of numbers") { | |
(findSet(List(Card(circle, filled, two, R), Card(circle, filled, one, R), Card(circle, filled, one, R))) | |
should be(None)) | |
} | |
it("invalid set of colors") { | |
(findSet(List(Card(circle, filled, one, B), Card(circle, filled, one, R), Card(circle, filled, one, R))) | |
should be(None)) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment