SQL 코딩테스트 - Type of Triangle (★★★/X/1)

기운찬곰·2021년 4월 7일
0

SQL

목록 보기
2/2

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


문제

Write a query identifying the type of each record in the TRIANGLES table using its three side lengths. Output one of the following statements for each record in the table:

  • Equilateral: It's a triangle with 3 sides of equal length.
  • Isosceles: It's a triangle with 2 sides of equal length.
  • Scalene: It's a triangle with 3 sides of differing lengths.
  • Not A Triangle: The given values of A, B, and C don't form a triangle.

Input Format

The TRIANGLES table is described as follows:

Each row in the table denotes the lengths of each of a triangle's three sides.

Sample Input

Sample Output

Isosceles
Equilateral
Scalene
Not A Triangle
  • Values in the tuple (20, 20, 23) form an Isosceles triangle, because A = B
  • Values in the tuple (20, 20, 20) form an Equilateral triangle, because A = B = C
  • Values in the tuple (20, 21, 22) form a Scalene triangle, because A != B != C
  • Values in the tuple (13, 14, 30) cannot form a triangle because the combined value of sides A and B is not larger than that of side C.

해결방법

문제 이해하기

일단 문제를 보면 A, B, C에 따라 타입이 4가지가 될 수 있다. 따라서 이 문제는 CASE문 사용해야 한다.

근데 잘 생각해보면 우선 순위가 있다. 가장 먼저 무엇을 고려해야할까? 삼각형이 될 수 있는 경우와 없는 경우를 나누는게 제일 먼저 해야 할 일이다. 나는 이 부분에서 문제를 틀렸다.


소스코드

정답 코드

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

✍️ 상당히 재미있는 문제였다. 만약 내가 코딩테스트 SQL 문제를 출제하는 사람이라면 이런식으로 문제를 내볼거 같다.

profile
velog ckstn0777 부계정 블로그 입니다. 프론트 개발 이외의 공부 내용을 기록합니다. 취업준비 공부 내용 정리도 합니다.

0개의 댓글