JavaScript - 프로그래머스 레벨 : 0(18) - ORDER BY '정답률'

먹보·2023년 1월 5일
0

1. 겹치는 선분의 길이

문제 : 선분 3개가 평행하게 놓여 있습니다. 세 선분의 시작과 끝 좌표가 [[start, end], [start, end], [start, end]] 형태로 들어있는 2차원 배열 lines가 매개변수로 주어질 때, 두 개 이상의 선분이 겹치는 부분의 길이를 return 하도록 solution 함수를 완성해보세요.

function solution(lines) {
    const expan = new Array
    
    lines.forEach(a => {
        const expanding = new Array;
        for(let i = a[0] ; i <= a[1] ; i++){
            expanding.push(i)
        }
        expan.push(expanding)
    })
  
  const fLapped = findOverLapped(expan[0],expan[1])
  const sLapped = findOverLapped(expan[1],expan[2])
  const tLapped = findOverLapped(expan[0],expan[2])
  const sum = fLapped.concat(sLapped).concat(tLapped)
  
  if(repeated(sum) > 1){
    const finalLapped = [...new Set(sum)]
    const length = finalLapped.length-1
    
    return length >= 0 ? length : 0
  } else {
    const fs = fLapped.length > 0 ? fLapped.length-1 : 0
    const ss = sLapped.length > 0 ? sLapped.length-1 : 0
    const ts = tLapped.length > 0 ? tLapped.length-1 : 0
    return fs + ss + ts
  }
  
}

function findOverLapped (arr1,arr2){
  const overLapped = new Array;
  for(let j = 0 ; j < arr1.length ; j++){
    for (let k = 0 ; k < arr2.length ; k++){
      if(arr1[j] == arr2[k]){
        overLapped.push(arr1[j])
      }
    }
  }
  return overLapped.length > 1 ? overLapped : []
}

function repeated(arr) {
  const elementCounts = {};
  let repeatedElements = 0;

  for (const element of arr) {
    if (elementCounts[element]) {
      elementCounts[element]++;
    } else {
      elementCounts[element] = 1;
    }
  }

  for (const element in elementCounts) {
    if (elementCounts[element] > 1) {
      repeatedElements += elementCounts[element];
    }
  }

  return repeatedElements;
}

🗒️코멘트 : 하하하...나 만큼 코드 길게 짜는 사람은 없을 것이다...좌절...리팩토링해서 돌아온다..아윌비백!

2. 평행

문제 : 점 네 개의 좌표를 담은 이차원 배열 dots가 다음과 같이 매개변수로 주어집니다.

[[x1, y1], [x2, y2], [x3, y3], [x4, y4]]

주어진 네 개의 점을 두 개씩 이었을 때, 두 직선이 평행이 되는 경우가 있으면 1을 없으면 0을 return 하도록 solution 함수를 완성해보세요.

function solution(dots) {
    if (line(dots[0],dots[1]) == line(dots[2],dots[3])){
        return 1
    } else if (line(dots[0],dots[2]) == line(dots[1],dots[3])){
        return 1
    } else if (line(dots[0],dots[3]) == line(dots[1],dots[2])){
        return 1
    } else {
        return 0
    }
}

function line(arr1, arr2) {
  const [x1, y1] = arr1;
  const [x2, y2] = arr2;

  const slope = (y2 - y1) / (x2 - x1);

  return slope;
}

🗒️코멘트 : 수학적 풀이 적용

  • 함수에서 두 점 사이를 이은 선의 기울기가 주어진 다른 선의 기울기와 같으면 평행
  • 기울기 공식 점이 두 개 주어졌을 때, 기울기는 (y2-y1)/(x2-y1) 이다

3. 옹알이(1)

문제 : 머쓱이는 태어난 지 6개월 된 조카를 돌보고 있습니다. 조카는 아직 "aya", "ye", "woo", "ma" 네 가지 발음을 최대 한 번씩 사용해 조합한(이어 붙인) 발음밖에 하지 못합니다. 문자열 배열 babbling이 매개변수로 주어질 때, 머쓱이의 조카가 발음할 수 있는 단어의 개수를 return하도록 solution 함수를 완성해주세요.

function solution(babbling) {
    const reg = new RegExp(/aya|ye|woo|ma/g)
    const replaced = new Array
    let answer = 0;
    for(let i of babbling){
        replaced.push(i.replace(reg,''))
        if(i.replace(reg,'').length == 0){
            answer++
        }
    }
    return answer
}

🗒️코멘트 : um.....정답률이 제일 낮은거 치고..너무 쉬웠다??

profile
🍖먹은 만큼 성장하는 개발자👩‍💻

0개의 댓글