문제
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
메모
- 기울기 = y값의 변화량 / x값의 변화량
- 가능한 경우의 수를 미리 판단하고 코드를 짜야 하는 문제이다.
- 각각의 배열은 하나의 점에 해당하므로 이미 사용한 점은 다시 사용할 수 없다.
- 기울기를 구하는 함수를 따로 만드는 방식을 기억해두자.
- 복잡한 계산이 필요한 경우, 먼저 해당 식을 함수로 구현해두면 훨씬 편리하다.
정답
/* 모범 답안 */
function calculateSlope(arr1, arr2) {
  return (arr2[1] - arr1[1]) / (arr2[0] - arr1[0]);
}
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 solution(dots) {
  // 가능한 기울기의 종류
  let slope1 = (dots[0][1] - dots[1][1]) / (dots[0][0] - dots[1][0]); // a - b
  let slope2 = (dots[0][1] - dots[2][1]) / (dots[0][0] - dots[2][0]); // a - c
  let slope3 = (dots[0][1] - dots[3][1]) / (dots[0][0] - dots[3][0]); // a - d
  let slope4 = (dots[1][1] - dots[2][1]) / (dots[1][0] - dots[2][0]); // b - c
  let slope5 = (dots[1][1] - dots[3][1]) / (dots[1][0] - dots[3][0]); // b - d
  let slope6 = (dots[2][1] - dots[3][1]) / (dots[2][0] - dots[3][0]); // c - d
  // 가능한 기울기의 쌍
  let arr1 = [slope1, slope6]; // [a - b], [c - d]
  let arr2 = [slope2, slope5]; // [a - c], [b - d]
  let arr3 = [slope3, slope4]; // [a - d], [b - c]
  let arr = [arr1, arr2, arr3];
  if (arr.filter(x => x[0] === x[1]).length > 0) {
    return 1;
  } else {
    return 0;
  }
}
문제
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
메모
- 기울기 = y값의 변화량 / x값의 변화량
- 가능한 경우의 수를 미리 판단하고 코드를 짜야 하는 문제이다.
- 각각의 배열은 하나의 점에 해당하므로 이미 사용한 점은 다시 사용할 수 없다.
- 기울기를 구하는 함수를 따로 만드는 방식을 기억해두자.
- 복잡한 계산이 필요한 경우, 먼저 해당 식을 함수로 구현해두면 훨씬 편리하다.
정답
/* 모범 답안 */
function calculateSlope(arr1, arr2) {
  return (arr2[1] - arr1[1]) / (arr2[0] - arr1[0]);
}
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 solution(dots) {
  // 가능한 기울기의 종류
  let slope1 = (dots[0][1] - dots[1][1]) / (dots[0][0] - dots[1][0]); // a - b
  let slope2 = (dots[0][1] - dots[2][1]) / (dots[0][0] - dots[2][0]); // a - c
  let slope3 = (dots[0][1] - dots[3][1]) / (dots[0][0] - dots[3][0]); // a - d
  let slope4 = (dots[1][1] - dots[2][1]) / (dots[1][0] - dots[2][0]); // b - c
  let slope5 = (dots[1][1] - dots[3][1]) / (dots[1][0] - dots[3][0]); // b - d
  let slope6 = (dots[2][1] - dots[3][1]) / (dots[2][0] - dots[3][0]); // c - d
  // 가능한 기울기의 쌍
  let arr1 = [slope1, slope6]; // [a - b], [c - d]
  let arr2 = [slope2, slope5]; // [a - c], [b - d]
  let arr3 = [slope3, slope4]; // [a - d], [b - c]
  let arr = [arr1, arr2, arr3];
  if (arr.filter(x => x[0] === x[1]).length > 0) {
    return 1;
  } else {
    return 0;
  }
}
