규도자 개발 블로그

[해커랭크(Hackerrank)/Problem Solving/파이썬3(python3)] Grading Students 본문

알고리즘/풀이

[해커랭크(Hackerrank)/Problem Solving/파이썬3(python3)] Grading Students

규도자 (gyudoza) 2019. 3. 19. 22:17

HackerLand University has the following grading policy:

  • Every student receives a  in the inclusive range from  to .
  • Any  less than  is a failing grade.

Sam is a professor at the university and likes to round each student's  according to these rules:

  • If the difference between the  and the next multiple of  is less than , round  up to the next multiple of .
  • If the value of  is less than , no rounding occurs as the result will still be a failing grade.

For example,  will be rounded to  but  will not be rounded because the rounding would result in a number that is less than .

Given the initial value of  for each of Sam's  students, write code to automate the rounding process.

Function Description

Complete the function gradingStudents in the editor below. It should return an integer array consisting of rounded grades.

gradingStudents has the following parameter(s):

  • grades: an array of integers representing grades before rounding

Input Format

The first line contains a single integer, , the number of students. 
Each line  of the  subsequent lines contains a single integer, , denoting student 's grade.

Constraints

Output Format

For each , print the rounded grade on a new line.

Sample Input 0

4
73
67
38
33

Sample Output 0

75
67
40
33

Explanation 0

image

  1. Student  received a , and the next multiple of  from  is . Since , the student's grade is rounded to .
  2. Student  received a , and the next multiple of  from  is . Since , the grade will not be modified and the student's final grade is .
  3. Student  received a , and the next multiple of  from  is . Since , the student's grade will be rounded to .
  4. Student  received a grade below , so the grade will not be modified and the student's final grade is .

풀이

#!/bin/python3

import os
import sys

#
# Complete the gradingStudents function below.
#
def gradingStudents(grades):
    result_list = []
    for score in grades:
        if score < 38 or score % 5 < 3:
            score = score
        else:
            tail_score = score % 5
            to_add_score = 5 - tail_score
            score = score + to_add_score
        result_list.append(score)

    return result_list

if __name__ == '__main__':
    f = open(os.environ['OUTPUT_PATH'], 'w')

    n = int(input())

    grades = []

    for _ in range(n):
        grades_item = int(input())
        grades.append(grades_item)

    result = gradingStudents(grades)

    f.write('\n'.join(map(str, result)))
    f.write('\n')

    f.close()

설명

주어진 점수들을 Sam교수 입맛대로 바꿔서 다시 반환해주는 문제이다. 조건에도 써있다시피 5로 나눴을 때 3이상이 남으면 5단위로 반올림해주는 대신에 점수가 낙제점인 38점 미만이면 그대로 둔다. 사실상 40점 미만이 낙제점이지만 38점까지는 반올림의 대상이기 때문에 38점 미만만 진짜 낙제점이 되는 것이다. 예제의 3번 학생과 4번 학생의 경우를 보면 그 차이를 알 수 있다. 단순 반올림이 아닌 조건문 + 티끌만큼의 수학적 계산이 필요하다..

Comments