글 작성자: Doublsb

https://programmers.co.kr/learn/courses/30/lessons/83201

 

풀이

- 문제의 나는 학점을 뿌리는 교수이다 그러나 상호평가인

- 학생은 자기 자신에게 점수를 부여할 수도 있다 (...)

- 그런데 자기 평가 점수가 최고점이거나 최저점이면 해당 점수는 제외하고 평균을 냄

- 그리고 점수대별로 학점 A/B/C/D/F를 나눈다 이걸 문자열로 리턴하면 된다

 

- 이번 문제도 썩 쉽다 그러니까 코드를 읽기 쉽게 만드는 것에 중점을 둬보겠다

 

 

C#

using System;
using System.Collections.Generic;
using System.Linq;

public class Solution 
{
    public string solution(int[,] scores) 
    {
        string answer = "";
        
        for(int i=0; i<scores.GetLength(0); i++)
        {
            var row = subArray(ref scores, i, scores.GetLength(0));
            answer += new Evaluation(row, i).Result;
        }
        
        return answer;
    }
    
    public int[] subArray(ref int[,] array, int row, int count)
    {
        var list = new List<int>();
        
        for(int i=0; i<count; i++)
        {
            list.Add(array[i, row]); 
        }
        
        return list.ToArray();
    }
}

public class Evaluation
{
    public char Result;
    
    private List<int> Scores;
    private int SelfIndex;
    
    public Evaluation(int[] Scores, int SelfIndex)
    {
        this.Scores = Scores.ToList();
        this.SelfIndex = SelfIndex;

        if(isSelfScoreMinOrMax())
            this.Scores.RemoveAt(SelfIndex);
        
        Result = get_Grade();
    }
    
    private bool isSelfScoreMinOrMax()
    {
        if(Scores.Count(e => e == Scores[SelfIndex]) >= 2 || Scores.Count(e => e == Scores[SelfIndex]) >= 2)
            return false;
        if(Scores.Max() == Scores[SelfIndex] || Scores.Min() == Scores[SelfIndex])
            return true;
        else
            return false;
    }
    
    private char get_Grade()
    {
        float average = Scores.Sum() / Scores.Count;

        if     (average >= 90) return 'A';
        else if(average >= 80) return 'B';
        else if(average >= 70) return 'C';
        else if(average >= 50) return 'D';
        else                   return 'F';

    }
}

 

결과

100.0 / 100.0

 

피드백

'유일한'을 걸러내야 한다는 케이스를 제대로 안 읽어서 헤멨다. ^w^... 문제를 제대로 읽자

반응형