Juni_Dev_log

(200202_TIL) 'Triangle' 본문

CodingTest/Exercism

(200202_TIL) 'Triangle'

Juni_K 2021. 2. 2. 14:37

▶ Triangle

Triangle

(Problem)

Introduction

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

 

- equilateral triangle은, 세 변의 길이가 모두 같은 삼각형

- isosceles triangle은, 적어도 두 변의 길이가 같은 삼각형 (세 변이 같아도 isosceles triangle 에 속함)

- scalene triangle은, 모든 변의 길이가 각각 다른 삼각형

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.

 

- 모든 변의 길이는 0보다 커야하고 두 변의 길이의 합이 한 변의 길이보다는 크거나 같아야한다.

- 길이가 소수점은 가능하다.

 

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.

 

 

(Solution)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
export class Triangle {
  constructor(num1,num2,num3) {
    this.num_array = [num1,num2,num3];
    // 오름차순 정렬
    this.num_array_sort = this.num_array.sort(function(a,b){
        return a-b;
    })
 
    // 모든 길이가 0보다는 커야함.
    // 두 변의 길이의 합이 한 변의 길이보다 작으면, false 반환.
    if( num1 > 0 && num2 > 0 && num3 > 0  && this.num_array_sort[0+ this.num_array[1<= this.num_array_sort[2]){
        return false;
    }
  }
 
  get isEquilateral() {
    // 모든 변의 길이가 같은 경우
    if(this.num_array_sort[0=== this.num_array_sort[1&& this.num_array_sort[1=== this.num_array_sort[2]){
        return true;
    }
  }
 
  get isIsosceles() {
    // 두 변의 길이가 같은 경우
    if(this.num_array_sort[0=== this.num_array_sort[1|| this.num_array_sort[1=== this.num_array_sort[2]){
        return true;
    }
  }
 
  get isScalene() {
    // 모든 변의 길이가 각각 다른 삼각형
    if(this.num_array_sort[0!== this.num_array_sort[1&& this.num_array_sort[1!== this.num_array_sort[2&& this.num_array_sort[0!== this.num_array_sort[2]){
        return true;
    }
  }
}
 
cs

 

- 우선 받아온 길이 요소들을 sort() 로 오름차순 정리를 배열에 담아서 정리한다.

 

- 정렬한 요소들 중은 0보다 커야하고, 작은 두변의 합이 큰 한 변보다는 커야 삼각형이 기본적으로 성립된다.

이를 if 문을 통해서 조건화시켰다.

 

- 모든 변의 길이가 같은 경우(isEquilateral()), if 문으로 &&를 이용해서 두 번의 연산자를 통해서 세 변이 같은지 조건화한다.

 

- 두 변의 길이가 같은 경우(isIsoceles()), if 문으로 || 를 이용해서 두 변의 길이가 같은지 조건화한다.

 

- 모든 길이가 다른 경우(isScalene()), if문으로 && 를 이용해서 모든 변의 길이가 다른지 조건화한다.

 

📚 참고

developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

 

Array.prototype.sort() - JavaScript | MDN

sort() 메서드는 배열의 요소를 적절한 위치에 정렬한 후 그 배열을 반환합니다. 정렬은 stable sort가 아닐 수 있습니다. 기본 정렬 순서는 문자열의 유니코드 코드 포인트를 따릅니다. 정렬 속도

developer.mozilla.org

 

'CodingTest > Exercism' 카테고리의 다른 글

(200204_TIL) 'Hamming'  (0) 2021.02.04
(200203_TIL) 'ETL'  (0) 2021.02.03
(200201_TIL) 'Collatz Conjecture'  (0) 2021.02.01
(200201_TIL) 'Reverse String'  (0) 2021.02.01
(200131_TIL) 'Leap'  (0) 2021.01.31
Comments