기억은 짧고 기록은 길다
[프로그래머스/Programmers] 2주차_상호 평가 - Python 본문
Link
코딩테스트 연습 - 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
Solution
필자는 다른 풀이에 비해 투박한 방법으로 행렬을 변환했지만 아래 다른 풀이들의 변환 방법을 익혀두기 바란다.
def solution(scores):
answer = []
s = []
for i in range(len(scores)):
t = []
for j in range(len(scores)):
t.append(scores[j][i])
s.append(t)
for i in range(len(s)):
if s[i][i] == max(s[i]) or s[i][i] == min(s[i]):
if s[i].count(s[i][i]) == 1:
s[i].remove(s[i][i])
for i in range(len(s)):
t = sum(s[i]) / len(s[i])
if t >= 90:
t = 'A'
elif t >= 80:
t = 'B'
elif t >= 70:
t = 'C'
elif t >= 50:
t = 'D'
else:
t = 'F'
answer.append(t)
return ''.join(answer)
📌 Tip: list(map(list, zip(*scores)))
Other Solution
from collections import Counter
def solution(scores):
answer = ''
for idx, score in enumerate(list(map(list, zip(*scores)))):
length = len(score)
if Counter(score)[score[idx]] == 1 and (max(score) == score[idx] or min(score) == score[idx]):
del score[idx]
length -= 1
grade = sum(score) / length
if grade >= 90:
answer += 'A'
elif grade >= 80:
answer += 'B'
elif grade >= 70:
answer += 'C'
elif grade >= 50:
answer += 'D'
else:
answer += 'F'
return answer
def solution(scores):
answer = ''
for i, score in enumerate(zip(*scores)):
avg = (sum(score) - score[i]) / (len(score) - 1) if score[i] in (min(score), max(score)) and score.count(score[i]) == 1 else sum(score) / len(score)
answer += "%s" % (
"A" if 90 <= avg else
"B" if 80 <= avg else
"C" if 70 <= avg else
"D" if 50 <= avg else
"F"
)
return answer
'CodingTest > programmers' 카테고리의 다른 글
[프로그래머스/Programmers] 3진법 뒤집기 - Python (0) | 2021.09.04 |
---|---|
[프로그래머스/Programmers] 4주차_직업군 추천하기 - Python (0) | 2021.09.04 |
[프로그래머스/Programmers] 1주차_부족한 금액 계산하기 - Python (0) | 2021.09.03 |
[프로그래머스/Programmers] 내적 - Python (0) | 2021.09.03 |
[프로그래머스/Programmers] 숫자 문자열과 영단어 - Python (0) | 2021.09.02 |
Comments