3
\$\begingroup\$

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):
    rounded_grades = []
    for grade in grades:
        if grade < 38:
            rounded_grades.append(grade)
        else:
            if grade % 5 == 0:
                rounded_grades.append(grade)
            else:
                difference = 0
                new_grade = grade
                while grade % 5 != 0:
                    difference += 1
                    new_grade += 1
                    if new_grade % 5 == 0:
                        break
                if difference < 3:
                    rounded_grades.append(new_grade)
                else:
                    rounded_grades.append(grade)
    return rounded_grades
\$\endgroup\$
3
\$\begingroup\$
  • 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):
    rounded_grades = []
    for grade in grades:
        if grade >= 38:
            difference = 5 - grade % 5
            if difference < 3:
                grade += difference
        rounded_grades.append(grade)
    return rounded_grades

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]


def gradingStudents(grades):
    rounded_grades = []
    for grade in grades:
        if grade >= 38:
            grade += ADDITIONS[grade % 5]
        rounded_grades.append(grade)
    return rounded_grades

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

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


def gradingStudents(grades):
    return [
        grade if grade < 38 else grade + ADDITIONS[grade % 5]
        for grade in grades
    ]
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Thank you for your commentary. I very much prefer the first version that you wrote. List comprehensions are great and all but just seeing the first version is good enough for me. \$\endgroup\$ – King Cold Feb 16 at 0:23
  • 1
    \$\begingroup\$ No problem, happy I could help :) \$\endgroup\$ – Peilonrayz Feb 16 at 0:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.