 # JaeHyoLee's solution

## to Triangle in the Lua Track

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

## Running the tests

To run the tests, run the command `busted` from within the exercise directory.

## Further information

For more detailed information about the Lua track, including how to get help if you're having trouble, please visit the exercism.io Lua language 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.

### triangle_spec.lua

``````local triangle = require('triangle')

describe('triangle', function()
it('equilateral triangles have equal sides', function()
assert.are.equals('equilateral', triangle.kind(2, 2, 2))
end)

it('larger equilateral triangles also have equal sides', function()
assert.are.equals('equilateral', triangle.kind(10, 10, 10))
end)

it('isosceles triangles have last two sides equal', function()
assert.are.equals('isosceles', triangle.kind(3, 4, 4))
end)

it('isosceles trianges have first and last sides equal', function()
assert.are.equals('isosceles', triangle.kind(4, 3, 4))
end)

it('isosceles triangles have two first sides equal', function()
assert.are.equals('isosceles', triangle.kind(4, 4, 3))
end)

it('isosceles triangles have in fact exactly two sides equal', function()
assert.are.equals('isosceles', triangle.kind(10, 10, 2))
end)

it('scalene triangles have no equal sides', function()
assert.are.equals('scalene', triangle.kind(3, 4, 5))
end)

it('scalene triangles have no equal sides at a larger scale too', function()
assert.are.equals('scalene', triangle.kind(10, 11, 12))
end)

it('scalene triangles have no equal sides in descending order either', function()
assert.are.equals('scalene', triangle.kind(5, 4, 2))
end)

it('very small triangles are legal', function()
assert.are.equals('scalene', triangle.kind(0.4, 0.6, 0.3))
end)

it('test triangles with no size are illegal', function()
assert.has_error(function() triangle.kind(0, 0, 0) end, 'Input Error')
end)

it('triangles with negative sides are illegal', function()
assert.has_error(function() triangle.kind(3, 4, -5) end, 'Input Error')
end)

it('triangles violating triangle inequality are illegal', function()
assert.has_error(function() triangle.kind(1, 1, 3) end, 'Input Error')
end)

it('triangles violating triangle inequality are illegal 2', function()
assert.has_error(function() triangle.kind(7, 3, 2) end, 'Input Error')
end)
end)``````
``````local function Islegal(a, b, c)
return a + b >= c and b + c >= a and c + a >= b and a > 0 and b >0 and c > 0
end

return {
kind = function(a, b, c)
if Islegal(a,b,c) then
if a == b and b == c then return 'equilateral' end
if a == b or b == c or c == a then return 'isosceles' end
return 'scalene'
end
error("Input Error")
end
}`````` Looks good, but IsLegal is a little bit ugly... Solution Author

yeah I agree. Just too lazy to think of any other good solution?. have 'sort' a,b,c will be make this little prettier. Well, if you change your >= and and to < and or and rename function to isIllegal then you dont need to compare with zero, because:

if only a < 0 then a + b < c or a + c < b

if only a, b < 0 then a + b < c

if a,b,c < 0 then one of the inequalities will be true also :)

### 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?