src/main/scala/triangle.scala

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
object TriangleType extends Enumeration {
  val Equilateral, Isosceles, Scalene, Illogical = Value
}

case class Triangle(a: Int, b: Int, c: Int) {
  private val sides = List(a, b, c)

  private def uniqueSides = sides.toSet
  private def satisfiesTriangleInequality = sides.sum > sides.max * 2
  private def valid = sides.forall(_ > 0) && satisfiesTriangleInequality
  private def equilateral = uniqueSides.size == 1
  private def isosceles = uniqueSides.size == 2

  def triangleType =
    if (!valid)           TriangleType.Illogical
    else if (equilateral) TriangleType.Equilateral
    else if (isosceles)   TriangleType.Isosceles
    else                  TriangleType.Scalene
}

@abo64 thinks this looks great

Comments


You're not logged in right now. Please login via GitHub to comment