🐍Python/Python_알고리즘

[알고리즘] 11. Grading Students

728x90
반응형

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 math
import os
import random
import re
import sys

#
# Complete the 'gradingStudents' function below.
#
# The function is expected to return an INTEGER_ARRAY.
# The function accepts INTEGER_ARRAY grades as parameter.
#

def gradingStudents(grades):
newgrade = list()
for gn in grades:
if gn > 37:
last_digit = str(gn)[-1]
if 2 < int(last_digit) < 5:
gn = round(gn,-1) + 5
newgrade.append(gn)
elif 7 < int(last_digit) <= 9:
gn = round(gn,-1)
newgrade.append(gn)
else:
newgrade.append(gn)
else:
newgrade.append(gn)
return newgrade

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

grades_count = int(input().strip())

grades = []

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

result = gradingStudents(grades)

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

fptr.close()



점수를 제일 먼저 구간별로 나눠서 임계에 다다르지 못하는 점수는 그대로 내뱉도록 했다. 그리고 끝자리를 두 가지 경우로 나눠서 반올림처리 해주거나 그대로 두었다. 


중간에 헷갈려서 생각보다 시간을 많이 쏟았다... 항상 처음부터 머릿속에 그려놓고 코딩하는 습관을 가져야겠다.

728x90
반응형