Here is my solution to Grading Students courtesy of HackerRank. My code passes all tests in the testing suite but I feel like there could be a better implementation of it. But nonetheless, what are some pros/cons of my current implementation? Thank you for answering.

def gradingStudents(grades):
else:
if grade % 5 == 0:
else:
difference = 0
while grade % 5 != 0:
difference += 1
if new_grade % 5 == 0:
break
if difference < 3:
else:


• Your code is pretty good. Nice job.
• I'd personally change the first else to an elif so you don't have as much indentation.
• Rather than using that while loop you can just use one % per number.

For each number if you get the modulo then you can find out how much to add. 7 % 5 is 2. Where 5 - 2 is not less than 3.

And so you can easily change the while to just difference = 5 - grade % 5

• You can merge grade % 5 == 0 into the else.
• You can change the order and content of the ifs. This is as we don't need to call list.append three times, just mutate grade.
def gradingStudents(grades):
difference = 5 - grade % 5
if difference < 3:


Since we know the range that anything modulo 5 is 0-4, we can just make a small lists that holds how much to round.

ADDITIONS = [0, 0, 0, 2, 1]



You can also change the code to a list comprehension if you're that way inclined.

ADDITIONS = [0, 0, 0, 2, 1]