Skip to content
Advertisement

Display the type of triangle

This is the problem:

https://www.hackerrank.com/challenges/what-type-of-triangle/problem

I’ve tried this solution:

SELECT 
    CASE
        WHEN (A+B) <= C OR (B+C) <= A OR (C+A) <= B THEN 'Not A Triangle'
        WHEN A = C OR A = B OR B = C THEN 'Isoceles'
        WHEN A = C AND A = B THEN 'Equilateral'
        ELSE 'Scalene'
    END 
FROM TRIANGLES;

The above solution does not work.

Whereas the solution given below works

SELECT 
    CASE 
        WHEN A >= (B + C) OR B >= (A + C) OR C >= (A + B) THEN 'Not A Triangle'
        WHEN A = B AND A = C THEN 'Equilateral'
        WHEN A = B OR B = C OR A = C THEN 'Isosceles'
        ELSE 'Scalene'
    END
FROM TRIANGLES;

I feel both solutions are the same except swapping the side of the operands while I also appropriately changed the operator so as to not change the meaning. Can someone please explain why solution 1 doesn’t work?

Advertisement

Answer

select
case
when A + B <= C or  C + A <= B or C + B <= A then 'Not A Triangle'
when A = B and B = C then 'Equilateral'
when A <> B and B <> C  and A <> C then 'Scalene'
when (A = B and C <> A) or (A = C and B <> A) or (B = C and A <> C) then 'Isosceles' END
from Triangles;
  • Simple Case for each situation
  • The order here is important for accurate results
  • Important: <= condition is not clearly shown in Hacker rank question for ‘Not a Triangle’, This may be your issue
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement