🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉 # SergiiVlasiuk's solution

## to Triangle in the Scala Track

Published at Aug 18 2019 · 0 comments
Instructions
Test suite
Solution

Determine if a triangle is equilateral, isosceles, or scalene.

An equilateral triangle has all three sides the same length.

An isosceles triangle has at least two sides the same length. (It is sometimes specified as having exactly two sides the same length, but for the purposes of this exercise we'll say at least two.)

A scalene triangle has all sides of different lengths.

## Note

For a shape to be a triangle at all, all sides have to be of length > 0, and the sum of the lengths of any two sides must be greater than or equal to the length of the third side. See Triangle Inequality.

## Dig Deeper

The case where the sum of the lengths of two sides equals that of the third is known as a degenerate triangle - it has zero area and looks like a single line. Feel free to add your own code/tests to check for degenerate triangles.

The Scala exercises assume an SBT project scheme. The exercise solution source should be placed within the exercise directory/src/main/scala. The exercise unit tests can be found within the exercise directory/src/test/scala.

To run the tests simply run the command `sbt test` in the exercise directory.

For more detailed info about the Scala track see the help page.

## Source

The Ruby Koans triangle project, parts 1 & 2 http://rubykoans.com

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### TriangleTest.scala

``````import org.scalatest.{Matchers, FunSuite}

/** @version 1.1.0 */
class TriangleTest extends FunSuite with Matchers {

test("equilateral - true if all sides are equal") {
Triangle(2, 2, 2).equilateral should be (true)
}

test("equilateral - false if any side is unequal") {
pending
Triangle(2, 3, 2).equilateral should be (false)
}

test("equilateral - false if no sides are equal") {
pending
Triangle(5, 4, 6).equilateral should be (false)
}

test("equilateral - All zero sides are illegal, so the triangle is not equilateral") {
pending
Triangle(0, 0, 0).equilateral should be (false)
}

test("equilateral - sides may be floats") {
pending
Triangle(0.5, 0.5, 0.5).equilateral should be (true)
}

test("isosceles - true if last two sides are equal") {
pending
Triangle(3, 4, 4).isosceles should be (true)
}

test("isosceles - true if first two sides are equal") {
pending
Triangle(4, 4, 3).isosceles should be (true)
}

test("isosceles - true if first and last sides are equal") {
pending
Triangle(4, 3, 4).isosceles should be (true)
}

test("isosceles - equilateral triangles are also isosceles") {
pending
Triangle(4, 4, 4).isosceles should be (true)
}

test("isosceles - false if no sides are equal") {
pending
Triangle(2, 3, 4).isosceles should be (false)
}

test("isosceles - Sides that violate triangle inequality are not isosceles, even if two are equal") {
pending
Triangle(1, 1, 3).isosceles should be (false)
}

test("isosceles - sides may be floats") {
pending
Triangle(0.5, 0.4, 0.5).isosceles should be (true)
}

test("scalene - true if no sides are equal") {
pending
Triangle(5, 4, 6).scalene should be (true)
}

test("scalene - false if all sides are equal") {
pending
Triangle(4, 4, 4).scalene should be (false)
}

test("scalene - false if two sides are equal") {
pending
Triangle(4, 4, 3).scalene should be (false)
}

test("scalene - Sides that violate triangle inequality are not scalene, even if they are all different") {
pending
Triangle(7, 3, 2).scalene should be (false)
}

test("scalene - sides may be floats") {
pending
Triangle(0.5, 0.4, 0.6).scalene should be (true)
}
}``````
``````case class Triangle(a: Double, b: Double, c: Double) {
val sides = List(a, b, c).sorted reverse

def equilateral: Boolean = validSides && sides.distinct.size == 1

def isosceles: Boolean = validSides && sides.distinct.size <= 2

def scalene: Boolean = validSides && sides.distinct.size == 3

private def validSides = {
lazy val allSidesPositive = sides.count(i => i <= 0).equals(0)
lazy val validLengths = sides.tail.sum > sides.head
allSidesPositive && validLengths
}
}``````