๋ฌธ์ œ

5๋ช…์˜ ํ•™์ƒ์ด ์ฃผ์–ด์งˆ ๋•Œ ๊ฐ ํ•™์ƒ์ด ๋ฐ›์€ ์ ์ˆ˜์˜ ํ‰๊ท ์„ ๊ตฌํ•˜์—ฌ ๊ธฐ์ค€์— ๋”ฐ๋ผ ํ•™์ ์„ ๋ถ€์—ฌ

์กฐ๊ฑด : ์ž๊ธฐ์ž์‹ ์„ ํ‰๊ฐ€ํ•œ ์ ์ˆ˜๊ฐ€ ์œ ์ผํ•œ ์ตœ๊ณ ์  ๋˜๋Š” ์œ ์ผํ•œ ์ตœ์ €์ ์ด๋ผ๋ฉด, ๊ทธ ์ ์ˆ˜๋Š” ์ œ์™ธํ•˜๊ณ  ํ‰๊ท ์„ ๊ตฌํ•œ๋‹ค. 

 

 

ํ’€์ด

ํ‰๊ท ์„ ๋จผ์ € ๊ตฌํ•˜๊ณ , ํ•™์ ์„ ๊ตฌํ•œ๋‹ค. ๊ฐ ํ–‰์— ํ‰๊ท ์„ ๊ตฌํ•ด์•ผ ํ•  ์ ์ˆ˜๊ฐ€ ๋“ค์–ด์žˆ๋‹ค. 

 

 

ํ†ต๊ณผํ•˜์ง€๋ชปํ•œ ์ฝ”๋“œ

const solution = (scores) => {
  let sum = 0;

  let sumArr = [];
  let scoreArr = [];
  let studentScoreArr = [];
  let len;

  let answer = '';

  // ๋ฐฐ์—ด ๊ฐ€๊ณต
  for (let i = 0; i < scores.length; i++) {
    studentScoreArr = [];
    for (let j = 0; j < scores.length; j++) {
      studentScoreArr.push(scores[j][i]);
    }
    scoreArr.push(studentScoreArr);
  }

  // ์ ์ˆ˜ ๊ฐ€๊ณต
  for (let i = 0; i < scores.length; i++) {
    sum = 0;
    len = scoreArr[i].length;
    for (let j = 0; j < scores.length; j++) {
      let findIdx = scoreArr.indexOf(scoreArr[i]);
      let findCurrentIdx = scoreArr[i].indexOf(scoreArr[i][j]);

      if (findIdx === findCurrentIdx) {

        const myScore = scoreArr[i][j];
        const dubCheck = isDuplicate(scoreArr[i]);

        if (dubCheck) {
          if (Math.max(...scoreArr[i]) === myScore || Math.min(...scoreArr[i]) === myScore) {
            let myIdx = scoreArr[i].indexOf(myScore);
            console.log(myIdx);
            scoreArr[i].splice(myIdx, 1, 0);
            len--;
          } else {
            sum += scoreArr[i][j];
          }
        } else if (Math.max(...scoreArr[i]) === myScore || Math.min(...scoreArr[i]) === myScore) {
          let myIdx = scoreArr[i].indexOf(myScore);
          scoreArr[i].splice(myIdx, 1, 0);
          len--;
        } else {
          sum += scoreArr[i][j]
        }
      } else {
        sum += scoreArr[i][j];
      }
    }
    console.log(len);
    sumArr.push(sum / len);
  }

  // ํ•™์  ๊ณ„์‚ฐ
  for (let i = 0; i < sumArr.length; i++) {
    if (sumArr[i] >= 90) {
      answer += 'A';
    } else if (90 > sumArr[i] && sumArr[i] >= 80) {
      answer += 'B';
    } else if (80 > sumArr[i] && sumArr[i] >= 70) {
      answer += 'C';
    } else if (70 > sumArr[i] && sumArr[i] >= 50) {
      answer += 'D';
    } else {
      answer += 'F';
    }
  }
  return answer;
}

const isDuplicate = (arr) => {
  const isDup = arr.some(function (x) {
    return arr.indexOf(x) !== arr.lastIndexOf(x);
  });
  return isDup;
}

๊ธฐ๋ณธ ํ…Œ์ŠคํŠธ๋Š” ํ†ต๊ณผํ–ˆ๋Š”๋ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค 3๊ฐœ๋ฅผ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ–ˆ๋‹ค. 

 

 

 

 

ํ†ต๊ณผํ•œ ์ฝ”๋“œ

const solution = (scores) => {
  let answer = '';

  for (let i = 0; i < scores.length; i++) {
    let myScore = scores[i][i];
    let count = scores.length;

    let min = 101;
    let max = -1;
    let sum = 0;

    let flag = true;

    for (let j = 0; j < scores.length; j++) {
      let score = scores[j][i];

      if (i !== j && myScore === score) { // ๋‚ด์ ์ˆ˜๊ฐ€ ์•„๋‹ˆ๊ณ  ๋™์ผํ•œ ์ ์ˆ˜๊ฐ€ ์•„๋‹ ๋•Œ 
        flag = false;
      }

      min = Math.min(min, score);
      max = Math.max(max, score);
      sum += score;
    }

    if (flag && (min === myScore || max === myScore)) { // ๋‚ด ์ ์ˆ˜์ผ ๋•Œ
      count--;
      sum -= myScore;
    }

    answer += findGrade(sum / count);
  }
  return answer;
}

const findGrade = (score) => {
  if (score >= 90) {
    return 'A';
  } else if (90 > score && score >= 80) {
    return 'B';
  } else if (80 > score && score >= 70) {
    return 'C';
  } else if (70 > score && score >= 50) {
    return 'D';
  } else {
    return 'F';
  }
}

+ Recent posts