ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 위클리 챌린지 2주차 상호평가
    코딩 테스트/Level 1 2021. 8. 14. 16:05
    반응형

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

     

    코딩테스트 연습 - 2주차

    [[100,90,98,88,65],[50,45,99,85,77],[47,88,95,80,67],[61,57,100,80,65],[24,90,94,75,65]] "FBABD" [[70,49,90],[68,50,38],[73,31,100]] "CFD"

    programmers.co.kr

    파이썬

    def solution(scores):
        answer = ""
        for i, score in enumerate(map(list, zip(*scores))):
            if score.count(score[i]) == 1 and (score[i] == min(score) or score[i] == max(score)):
                del score[i]
            if (average := sum(score) // len(score)) >= 90:
                answer += 'A'
            elif average >= 80:
                answer += 'B'
            elif average >= 70:
                answer += 'C'
            elif average >= 50:
                answer += 'D'
            else:
                answer += 'F'
        return answer
    def solution(scores):
        answer = ""
        grade = "FFFFFDDCBAA"
        for i, score in enumerate(map(list, zip(*scores))):
            if score.count(score[i]) == 1 and (score[i] == min(score) or score[i] == max(score)):
                del score[i]
            answer += grade[sum(score) // len(score) // 10]
        return answer

    컴프리헨션을 사용해서...
    del 을 쓰지 않기 때문에 리스트보다 빠른 튜플을 쓸 수 있다. 

    def solution(scores):
        return ''.join(map(lambda x: 'FFFFFDDCBAA'[x], (sum(score) // len(scores) // 10
                if not (score.count(score[i]) == 1 and (score[i] == min(score) or score[i] == max(score))) else
                (sum(score) - score[i]) // (len(scores) - 1) // 10
                for i, score in enumerate(map(tuple, zip(*scores))))))

    PEP 위반이니까 재미로만... 

    solution = lambda scores: ''.join(map(lambda x: 'FFFFFDDCBAA'[x], (sum(score) // len(scores) // 10
                if not (score.count(score[i]) == 1 and (score[i] == min(score) or score[i] == max(score))) else
                (sum(score) - score[i]) // (len(scores) - 1) // 10
                for i, score in enumerate(map(tuple, zip(*scores))))))

    golang

    func solution(scores [][]int) (result string) {
    	grade := "FFFFFDDCBAA"
    	for j := 0; j < len(scores); j++ {
    		sum := 0
    		length := len(scores)
    		maxIndex, maxScore := 0, scores[0][j]
    		minIndex, minScore := 0, scores[0][j]
    		only := true
    		for i := 0; i < length; i++ {
    			if i != j && scores[j][j] == scores[i][j] {
    				only = false
    			}
    			if maxScore < scores[i][j] {
    				maxScore = scores[i][j]
    				maxIndex = i
    			}
    			if minScore > scores[i][j] {
    				minScore = scores[i][j]
    				minIndex = i
    			}
    			sum += scores[i][j]
    		}
    		if only == true && maxIndex == j {
    			sum -= maxScore
    			length -= 1
    		} else if only == true && minIndex == j {
    			sum -= minScore
    			length -= 1
    		}
    		result += string(grade[sum/length/10])
    	}
    	return
    }

    문자열의 길이를 알 수 있는 경우, 문자열보다는 []rune이나 []byte가 더 좋긴 하다. 

    func solution(scores [][]int) string {
    	grade := "FFFFFDDCBAA"
    	result := make([]byte, len(scores))
    	for j := 0; j < len(scores); j++ {
    		sum := 0
    		length := len(scores)
    		maxIndex, maxScore := 0, scores[0][j]
    		minIndex, minScore := 0, scores[0][j]
    		only := true
    		for i := 0; i < length; i++ {
    			if i != j && scores[j][j] == scores[i][j] {
    				only = false
    			}
    			if maxScore < scores[i][j] {
    				maxScore = scores[i][j]
    				maxIndex = i
    			}
    			if minScore > scores[i][j] {
    				minScore = scores[i][j]
    				minIndex = i
    			}
    			sum += scores[i][j]
    		}
    		if only == true && maxIndex == j {
    			sum -= maxScore
    			length -= 1
    		} else if only == true && minIndex == j {
    			sum -= minScore
    			length -= 1
    		}
    		result[j] = grade[sum/length/10]
    	}
    	return string(result)
    }

    한 줄이라도 더 줄이기 위해... 이럴 수도 있겠지만... 무슨 의미가...

    func solution(scores [][]int) string {
    	result := make([]byte, len(scores))
    	for j := 0; j < len(scores); j++ {
    		sum := 0
    		length := len(scores)
    		maxIndex, maxScore := 0, scores[0][j]
    		minIndex, minScore := 0, scores[0][j]
    		only := true
    		for i := 0; i < length; i++ {
    			if i != j && scores[j][j] == scores[i][j] {
    				only = false
    			}
    			if maxScore < scores[i][j] {
    				maxScore = scores[i][j]
    				maxIndex = i
    			}
    			if minScore > scores[i][j] {
    				minScore = scores[i][j]
    				minIndex = i
    			}
    			sum += scores[i][j]
    		}
    		if only == true && maxIndex == j {
    			sum -= maxScore
    			length -= 1
    		} else if only == true && minIndex == j {
    			sum -= minScore
    			length -= 1
    		}
    		result[j] = "FFFFFDDCBAA"[sum/length/10]
    	}
    	return string(result)
    }

     

    줄여보았다.. 

    func solution(scores [][]int) (result string) {
    	for i := 0; i < len(scores); i++ {
    		sum, length := 0, len(scores[0])
    		max_val, min_val := 0, 100
    		for j := 0; j < len(scores); j++ {
    			if i == j {
    				continue
    			}
    			if scores[j][i] > max_val {
    				max_val = scores[j][i]
    			}
    			if scores[j][i] < min_val {
    				min_val = scores[j][i]
    			}
    			sum += scores[j][i]
    		}
    		if max_val < scores[i][i] || min_val > scores[i][i] {
    			length--
    		} else {
    			sum += scores[i][i]
    		}
    		result += string("FFFFFDDCBAA"[sum/length/10])
    	}
    	return
    }
    반응형
Designed by Tistory.