04. Python(너의 평점은?)
04. Python(너의 평점은?)
[toc]
평점 계산기
문제
https://www.acmicpc.net/problem/25206
오답
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
dic = {'A+': 4.5, 'A0': 4.0, 'B+': 3.5, 'B0': 3.0, 'C+': 2.5, 'C0': 2.0, 'D+': 1.5, 'D0': 1.0, 'F': 0.0}
result_list = [0 for i in range(20)]
for i in range(20):
subject_score_rank = list(map(str, input().split()))
result_list[i] = subject_score_rank
score_list = []
for j in range(20):
if result_list[j][2] in dic:
score_list.append(dic[result_list[j][2]])
total_score1 = 0
total_score2 = 0
for l in range(20):
if result_list[l][2] != 'p':
for k in range(len(score_list)):
total_score1 += float(result_list[k][1]) * float(score_list[k])
total_score2 += float(result_list[k][1])
print(f'{total_score1/total_score2:.6f}')
문제점 1
1
2
3
4
score_list = []
for j in range(20):
if result_list[j][2] in dic:
score_list.append(dic[result_list[j][2]])
- 불필요한 리스트 작성
문제점 2
1
2
3
4
5
for l in range(20):
if result_list[l][2] != 'p':
for k in range(len(score_list)):
total_score1 += float(result_list[k][1]) * float(score_list[k])
total_score2 += float(result_list[k][1])
- 가장 잘못된 부분으로 연산과정 자체가 틀림
- 불필요한 중첩 반복문 사용
- 연산과정이 꼬이고, 잘못된 연산 결과 출력
- 불필요한 리스트 작성으로 인해 리스트 연산이 꼬임
해결1(My Answer)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
dic = {'A+': 4.5, 'A0': 4.0, 'B+': 3.5, 'B0': 3.0, 'C+': 2.5, 'C0': 2.0, 'D+': 1.5, 'D0': 1.0, 'F': 0.0}
result_list = [0 for i in range(20)]
for i in range(20):
subject_score_rank = list(map(str, input().split()))
result_list[i] = subject_score_rank
total_score1 = 0
total_score2 = 0
for j in range(20):
if result_list[j][2] in dic and result_list[j][2] != 'p':
total_score1 += float(result_list[j][1]) * dic[result_list[j][2]]
total_score2 += float(result_list[j][1])
print(f'{total_score1/total_score2:.6f}')
- 가독성이 많이 떨어짐
다듬기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
grade_dic = {'A+': 4.5, 'A0': 4.0, 'B+': 3.5, 'B0': 3.0, 'C+': 2.5, 'C0': 2.0, 'D+': 1.5, 'D0': 1.0, 'F': 0.0}
result_list = [0 for i in range(20)]
for i in range(20):
subject_score_rank = list(map(str, input().split()))
result_list[i] = subject_score_rank
credit_grade = 0
credit_ = 0
for j in range(20):
if result_list[j][2] in grade_dic and result_list[j][2] != 'p':
grade = grade_dic[result_list[j][2]]
credit = float(result_list[j][1])
credit_grade += credit * grade
credit_ += credit
print(f'{credit_grade/credit_:.6f}')
해결2(Reference)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import sys
input = sys.stdin.readline
grade = {'A+': 4.5, 'A0': 4.0, 'B+': 3.5, 'B0': 3.0, 'C+': 2.5, 'C0': 2.0, 'D+': 1.5, 'D0': 1.0, 'F': 0.0}
total_credit = 0
total_score = 0
for _ in range(20):
subject, credit, score = input().split()
credit = float(credit)
if score != 'P':
total_credit += credit
total_score += credit * grade[score]
print(f'{total_score / total_credit:.6f}')
- 줄일 수 있는 반복문을 줄이고, 연산도 간단하게 변경