🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of katrinleinweber

katrinleinweber's solution

to Triangle in the R Track

Published at Jul 13 2018 · 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.

Hint

For this exercise you will need to implement a simple S3 class. If you are not familiar with S3, a great starting point is the chapter on S3 in Hadley Wickham's book Advanced R.

Installation

See this guide for instructions on how to setup your local R environment.

How to implement your solution

In each problem folder, there is a file named <exercise_name>.R containing a function that returns a NULL value. Place your implementation inside the body of the function.

How to run tests

Inside of RStudio, simply execute the test_<exercise_name>.R script. This can be conveniently done with testthat's auto_test function. Because exercism code and tests are in the same folder, use this same path for both code_path and test_path parameters. On the command-line, you can also run Rscript test_<exercise_name>.R.

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.

test_triangle.R

source("./triangle.R")
library(testthat)

context("triangle")

test_that("true if all sides are equal", {
  expect_is(triangle(2, 2, 2), "equilateral")
})

test_that("false if any side is unequal", {
  expect_false(any("equilateral" %in% class(triangle(2, 3, 2))))
})

test_that("false if no sides are equal", {
  expect_false(any("equilateral" %in% class(triangle(5, 4, 6))))
})

test_that("not equilateral if sides are zero", {
  expect_error(triangle(0, 0, 0))
})

test_that("sides may be floats", {
  expect_is(triangle(0.5, 0.5, 0.5), "equilateral")
})

test_that("isosceles if last two sides are equal", {
  expect_is(triangle(3, 4, 4), "isosceles")
})

test_that("isosceles if first two sides are equal", {
  expect_is(triangle(4, 4, 3), "isosceles")
})

test_that("isosceles if first and last sides are equal", {
  expect_is(triangle(4, 3, 4), "isosceles")
})

test_that("equilateral triangles are also isosceles", {
  expect_is(triangle(4, 4, 4), "isosceles")
})

test_that("not isosceles if no sides are equal", {
  expect_false(any("isosceles" %in% class(triangle(2, 3, 4))))
})

test_that("not isosceles if triangle inequality is violated", {
  expect_error(triangle(1, 1, 3))
})

test_that("sides may be floats", {
  expect_is(triangle(0.5, 0.4, 0.5), "isosceles")
})

test_that("scalene if no sides are equal", {
  expect_is(triangle(5, 4, 6), "scalene")
})

test_that("not scalene if all sides are equal", {
  expect_false(any("scalene" %in% class(triangle(4, 4, 4))))
})

test_that("not scalene if two sides are equal", {
  expect_false(any("scalene" %in% class(triangle(4, 4, 3))))
})

test_that("not scalene if triangle inequality is violated", {
  expect_error(triangle(7, 3, 2))
})

test_that("sides may be floats", {
  expect_is(triangle(0.5, 0.4, 0.6), "scalene")
})

message("All tests passed for exercise: triangle")
library(magrittr)

triangle <- function(x, y, z) {
  
  # exclude invalid cases
  if (x == 0
      | y == 0
      | z == 0
      | x + y < z
      | z + x < y
      | y + z < x
  ) stop("That's not really a triangle!")
  
  # construct S3 class
  c(x, y, z) %>%
    structure(., class = switch(length(unique(.)), 
                                c("equilateral", "isosceles"), 
                                "isosceles", "scalene"))
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?