Python/백준 알고리즘
[백준][Python3] #25206. 너의 평점은
2nan
2024. 3. 3. 12:00
728x90
코드 입력
# 학점을 반환할 딕셔너리 생성
dict_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
}
total_score = 0 # 학점 * 등급 (총 평점)
total_point = 0 # 총 이수학점
for _ in range(20):
# 과목, 학점, 등급을 string으로 받음
subject, point, grade = map(str, input().split())
point = float(point) # 학점은 따로 float 처리
# Pass는 학점 계산 제외
if grade == 'P':
continue
else:
total_score += dict_grade[grade] * point
total_point += point
# 총 평점 / 총 이수학점
print(total_score/total_point)
처음에 while 문과 학점을 반환하는 함수를 따로 만들어서 시도했었는데,
계속 실패하길래 몇몇 과목을 스킵하는 이슈가 발생했다.
아무리 디버깅해도 원인을 알 수가 없어서 새로운 마음으로 다시 처음부터 작성해서 결국은 해냈다..
원래 시도했던 코드는 이렇다.
def grade_(input):
score = 0
if input == 'P' or input == 'F':
return 0.0
elif input[0] == 'A':
score = 4.0
elif input[0] == 'B':
score = 3.0
elif input[0] == 'C':
score = 2.0
else:
score = 1.0
if input[-1] == '+':
return score + 0.5
else:
return score
total_score = 0
total_point = 0
while True:
if len(input()) > 0:
list_score = list(map(str, input().strip().split()))
point = float(list_score[1])
if list_score[-1] == 'P':
continue
else:
score = grade_(list_score[-1])
total_score += score * point
total_point += point
else:
break
print(total_score / total_point)
출처 : 백준 Online Judge