Reputation: 171
According to the question, If the difference between the grade and the next multiple of 5 is less than 3, round grade up to the next multiple of 5 . If the value of grade is less than 38, no rounding occurs as the result will still be a failing grade.
Here's my solution for it,
def gradingStudents(grades):
for i in grades:
if (5 * round(1 + i/5) - i) < 3 and i>= 38:
print (5 * round(1 + i/5))
else:
print (i)
grades_count = int(input().strip())
grades = []
for p in range(grades_count):
g = int(input(''))
grades.append(g)
result = gradingStudents(grades)
However, on examining the output I noticed that the if condition is not working as the output generates the same grade as the input.
Upvotes: 2
Views: 77
Reputation: 2584
Using modulo %
5 should help you. Modulo is the remainder got after the division. Example 8 % 5=3
Also the //
operator automatically rounds down for you after the division
grades = [1, 9, 37, 38, 43, 47, 49, 99, 91]
rounded_grades = [grade if grade < 38 or grade % 5 in [0, 1, 2] else 5 * (grade // 5 + 1) for grade in grades]
print(grades)
print(rounded_grades)
#Output
[1, 9, 37, 38, 43, 47, 49, 99, 91]
[1, 9, 37, 40, 45, 47, 50, 100, 91]
Upvotes: 1
Reputation: 5935
Simplify your approach
def finalGrade(grade, multiple_of=5, limit=38):
if not grade < limit:
grade = int(round(grade / multiple_of) * multiple_of)
return grade
grades = [20, 34, 37, 38, 39, 41, 45, 48, 52]
finalGrades = [finalGrade(grade) for grade in grades]
# [20, 34, 37, 40, 40, 40, 45, 50, 50]
Upvotes: 1
Reputation: 1361
I think something like this should work
def gradingStudents(grades):
output =[]
for g in filter(lambda x: x>38, grades):
step = g + (5 - g % 5)
output.append(step < 3 : g + step ? g)
output.extend(filter(lambda x: x<=38, grades)):
return output
# ... Your other code
print(gradingStudents(grades))
Upvotes: 1