저장을 습관화
프로그래머스 LV.0 평행 본문
프로그래머스 LV.0 평행
https://school.programmers.co.kr/learn/courses/30/lessons/120875
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
1. 문제 명
평행
2. 문제 설명
점 네 개의 좌표를 담은 이차원 배열 dots가 다음과 같이 매개변수로 주어집니다.
[[x1, y1], [x2, y2], [x3, y3], [x4, y4]]
주어진 네 개의 점을 두 개씩 이었을 때, 두 직선이 평행이 되는 경우가 있으면 1을 없으면 0을 return 하도록 solution 함수를 완성해보세요.
3. 제한 사항
dots의 길이 = 4 dots의 원소는 [x, y] 형태이며 x, y는 정수입니다. 0 ≤ x, y ≤ 100 서로 다른 두개 이상의 점이 겹치는 경우는 없습니다. 두 직선이 겹치는 경우(일치하는 경우)에도 1을 return 해주세요. 임의의 두 점을 이은 직선이 x축 또는 y축과 평행한 경우는 주어지지 않습니다.
4. 예시
dots | result |
[[1, 4], [9, 2], [3, 8], [11, 6]] | 1 |
[[3, 5], [4, 1], [2, 4], [5, 10]] | 0 |
5. 기본 제공 코드
function solution(dots) {
var answer = 0;
return answer;
}
6. 제출한 내 답
const solution = (dots) => {
dots.sort((a, b) => a[0] - b[0]);
let [a, b, c, d] = dots;
if ((b[1] - a[1]) / (b[0] - a[0]) === (d[1] - c[1]) / (d[0] - c[0])) {
return 1;
} else if ((c[1] - a[1]) / (c[0] - a[0]) === (d[1] - b[1]) / (d[0] - b[0])) {
return 1;
} else if ((d[1] - a[1]) / (d[0] - a[0]) === (c[1] - b[1]) / (c[0] - b[0])) {
return 1;
} else {
return 0;
}
};
6-2. VSC에 작성한 내용
const solution = (dots) => {
dots.sort((a, b) => a[0] - b[0]);
let [a, b, c, d] = dots;
/** 두 선이 x 축이나 y축과 평행하다는 제한 조건이 있을 때,
* 두 선이 평행하다는 말은 즉 두 선의 기울기가 같다는 말과 같을 수 있다.
*
* 선의 기울기를 구하는 공식
* y의 증가율 / x의 증가율
*
*/
// 1. ab - cd 비교
if ((b[1] - a[1]) / (b[0] - a[0]) === (d[1] - c[1]) / (d[0] - c[0])) {
return 1;
}
// 2. ac - bd 비교
else if ((c[1] - a[1]) / (c[0] - a[0]) === (d[1] - b[1]) / (d[0] - b[0])) {
return 1;
}
// 3. ad - bc 비교
else if ((d[1] - a[1]) / (d[0] - a[0]) === (c[1] - b[1]) / (c[0] - b[0])) {
return 1;
} else {
return 0;
}
};
// test
console.log(
solution([
[1, 4],
[9, 2],
[3, 8],
[11, 6],
]),
); // 1
console.log(
solution([
[3, 5],
[4, 1],
[2, 4],
[5, 10],
]),
); // 0
console.log(
solution([
[1, 2],
[5, 1],
[3, 6],
[6, 3],
]),
); // 1
console.log(
solution([
[1, 2],
[2, 1],
[3, 4],
[4, 5],
]),
); // 0
console.log(
solution([
[3, 5],
[4, 4],
[8, 9],
[6, 11],
]),
); // 1
7. 특이사항
기울기를 구하는 공식
점 (x1, y1), 점 (x2, y2)가 있을때
y의 증가율 / x의 증가율
출처
공식을 알기전 시도했던 방식
const solution = (dots) => {
dots.sort((a, b) => a[0] - b[0]);
let [a, b, c, d] = dots;
if (b[0] - a[0] === d[0] - c[0] && b[1] - a[1] === d[1] - c[1]) {
return 1;
} else if (c[0] - a[0] === d[0] - b[0] && c[1] - a[1] === d[1] - b[1]) {
return 1;
} else if (d[0] - a[0] === c[0] - b[0] && d[1] - a[1] === c[1] - b[1]) {
return 1;
} else {
return 0;
}
};
요소를 오름차순으로 정렬한뒤, 각각을 a, b, c, d에 할당한다.
a, b, c, d는 점의 좌표가 되며
이후 a-b, c-d / a-c, b-d / a-d, b-d와 같이 두 점씩 이어본다.
이후 점 a에서 점 b까지의 변화량과 점 c에서 점 d까지의 변화량이 일치하는지 비교하고
점 a에서 점 c까지의 변화량.....(이하생략)
세 조건 중 하나라도 변화량이 같은 선이 있다면 평행하다라고 했으나
제출하고 보니 82점으로 1번 10번 문제에서 틀렸다
8. 다른 사람이 작성한 답
8-1. 함수화
function solution(dots) {
if (calculateSlope(dots[0], dots[1]) === calculateSlope(dots[2], dots[3]))
return 1;
if (calculateSlope(dots[0], dots[2]) === calculateSlope(dots[1], dots[3]))
return 1;
if (calculateSlope(dots[0], dots[3]) === calculateSlope(dots[1], dots[2]))
return 1;
return 0;
}
function calculateSlope(arr1, arr2) {
return (arr2[1] - arr1[1]) / (arr2[0] - arr1[0]);
}
8-2.
function solution(dots) {
if((dots[0][1] - dots[1][1]) / (dots[0][0] - dots[1][0]) === (dots[2][1] - dots[3][1]) / (dots[2][0] - dots[3][0])) return 1
if((dots[0][1] - dots[2][1]) / (dots[0][0] - dots[2][0]) === (dots[1][1] - dots[3][1]) / (dots[1][0] - dots[3][0])) return 1
if((dots[0][1] - dots[3][1]) / (dots[0][0] - dots[3][0]) === (dots[2][1] - dots[1][1]) / (dots[2][0] - dots[1][0])) return 1
return 0
}
'코딩 테스트 > 프로그래머스 - 자바스크립트' 카테고리의 다른 글
프로그래머스 LV.0 특이한 정렬 (0) | 2024.02.26 |
---|---|
프로그래머스 LV.0 유한소수 판별하기 (0) | 2024.02.25 |
프로그래머스 LV.0 저주의 숫자 3 (0) | 2024.02.23 |
프로그래머스 LV.0 다항식 더하기 (0) | 2024.02.20 |
프로그래머스 LV.0 정수를 나선형으로 배치하기 (0) | 2024.02.19 |