[Lv.1]모의고사

Jihyun-Jeon·2022년 3월 22일
0

🔶 내가 푼 방법

  1. 수포자들이 쓴 답을 이중배열로 만든다. (세개의 배열을 다 검색해야 하니까 이중배열로 세팅함.)
  2. 그 이중배열을 돌면서 answer와 맞는 값만 새 이중배열(correct)에 넣음.
  3. correct의 길이를 rank배열로 만든다.
  4. rank배열 중 젤 큰 값을 찾아 maxNum 변수로 만든다.
  5. rank배열에서 maxNum과 같은 값을 찾아, 그 요소의 인덱스+1을 result배열에 넣음
  6. result배열를 리턴.
function solution(answers) {
  const person = [
    [1, 2, 3, 4, 5],
    [2, 1, 2, 3, 2, 4, 2, 5],
    [3, 3, 1, 1, 2, 2, 4, 4, 5, 5],
  ];

  const correct = [[], [], []]; // [[1,2,3,4,5],[],[]];
  for (let j = 0; j < person.length; j += 1) {
    for (let i = 0; i < answers.length; i += 1) {
      if (answers[i] === person[j][i % person[j].length]) {
        correct[j].push(person[j][i % person[j].length]);
      }
    }
  }

  const rank = []; // [5, 0, 0] [2,2,2] [2,3,1]
  for (let i = 0; i < correct.length; i += 1) {
    rank.push(correct[i].length);
  }

  const maxNum = Math.max(...rank);

  const result = [];
  for (let i = 0; i < rank.length; i += 1) {
    if (maxNum === rank[i]) {
      result.push(i + 1);
    }
  }
  return result;
}

🔶 다른 사람 풀이

방법1

function solution(answers) {
    var answer = [];
    var a1 = [1, 2, 3, 4, 5];
    var a2 = [2, 1, 2, 3, 2, 4, 2, 5]
    var a3 = [ 3, 3, 1, 1, 2, 2, 4, 4, 5, 5];

    var a1c = answers.filter((a,i)=> a === a1[i%a1.length]).length;
    var a2c = answers.filter((a,i)=> a === a2[i%a2.length]).length;
    var a3c = answers.filter((a,i)=> a === a3[i%a3.length]).length;
    var max = Math.max(a1c,a2c,a3c);

    if (a1c === max) {answer.push(1)};
    if (a2c === max) {answer.push(2)};
    if (a3c === max) {answer.push(3)};


    return answer;
}

방법2

function solution(answers) {
  const man1 = [1, 2, 3, 4, 5];
  const man2 = [2, 1, 2, 3, 2, 4, 2, 5];
  const man3 = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5];

  const count = [0, 0, 0];
  const result = [];

  for (let i = 0; i < answers.length; i += 1) {
    if (answers[i] === man1[i % man1.length]) count[0] += 1;
    if (answers[i] === man2[i % man2.length]) count[1] += 1;
    if (answers[i] === man3[i % man3.length]) count[2] += 1;
  }

  const max = Math.max(count[0], count[1], count[2]);
  for (let i = 0; i < count.length; i += 1) {
    if (max === count[i]) {
      result.push(i + 1);
    }
  }

  return result;
}

방법3

function solution(answers) {
  const man1 = [1, 2, 3, 4, 5];
  const man2 = [2, 1, 2, 3, 2, 4, 2, 5];
  const man3 = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5];

  const rank = [0, 0, 0];
  const result = [];

  for (let i = 0; i < answers.length; i += 1) {
    if (answers[i] === man1[i % man1.length]) {
      rank[0] += 1;
    }
  }

  for (let i = 0; i < answers.length; i += 1) {
    if (answers[i] === man2[i % man2.length]) {
      rank[1] += 1;
    }
  }

  for (let i = 0; i < answers.length; i += 1) {
    if (answers[i] === man3[i % man3.length]) {
      rank[2] += 1;
    }
  }

  const max = Math.max(...rank);

  for (let i = 0; i < rank.length; i += 1) {
    if (max === rank[i]) {
      result.push(i + 1);
    }
  }

  return result;
}

🔶피드백

  • 배열을 추출해야 할 때는 변수를 새 변수로 만들어 활용하지 않고, 배열을 리턴하는 메소드를 활용하면 유용함
  • %
    a%b : a가 b 보다 작으면, b로 a를 나눌 수 없어서 a가 그대로 나머지로 나옴.
    console.log(1 % 8); // 1 
    console.log(2 % 8); // 2 
    console.log(3 % 8); // 3 
    console.log(4 % 8); // 4
    console.log(5 % 8); // 5 
    console.log(6 % 8); // 6
    console.log(7 % 8); // 7 
    // 7을 8로 못나눠서, 7이 그대로 나머지로 나옴
    console.log(8 % 8); // 0
    console.log(9 % 8); // 1
    console.log(10 % 8); // 2
    console.log(11 % 8); // 3
    console.log(12 % 8); // 4
    console.log(13 % 8); // 5
    console.log(14 % 8); // 6
    console.log(15 % 8); // 7
    console.log(16 % 8); // 0
    console.log(17 % 8); // 1
    console.log(18 % 8); // 2

0개의 댓글