Photo by Jeswin Thomas on Unsplash
Grading Students Algorithm
This algorithm is a part of HackerRank's algorithm implementation challenges.
In this article, I'll be describing how I solved the
Grading Students
Challenge on HackerRank using Python 3.
This challenge is part of the Implementation
challenges in the Algorithm
section on HackerRank.
Problem 🎲
HackerLand University has the following grading policy:
- Every student receives a
grade
in the inclusive range from0
to100
. - Any
grade
less than40
is a failing grade.
Sam is a professor at the university and likes to round each student's grade
according to these rules:
- If the difference between the
grade
and the next multiple of5
is less than3
, roundgrade
up to the next multiple of5
. - If the value of
grade
is less than38
, no rounding occurs as the result will still be a failing grade.
Example
grade
=84
round to85
(85 - 84 is less than 3)grade
=29
do not round (result is less than 40)grade
=57
do not round (60 - 57 is 3 or higher)
Given the initial value of
grade
for each of Sam'sn
students, write code to automate the rounding process.
Function Description
Complete the function gradingStudents
.
gradingStudents
has the following parameter(s):
int grades[n]
: the grades before rounding
Returns
int[n]
: the grades after rounding as appropriate
Input Format
The first line contains a single integer, n
, the number of students.
Each line i
of the n
subsequent lines contains a single integer, grades[i]
.
Constraints
- 1 ≤
n
≤ 60 - 0 ≤
grades[i]
≤ 100
Samle Input:
73
67
38
33
Samle Ouput:
75
67
40
33
Explanation:
- Student
1
received a73
, and the next multiple of5
from73
is76
. Since75-73 < 3
, the student's grade is rounded to75
. - Student
2
received a67
, and the next multiple of5
from67
is70
. Since70 - 67 = 3
, the grade will not be modified and the student's final grade is67
. - Student
3
received a38
, and the next multiple of5
from38
is40
. Since40 - 38 < 3
, the student's grade will be rounded to40
. - Student
4
received a grade below33
, so the grade will not be modified and the student's final grade is33
.
Solution 🔎
We'll code up logic for all conditions in the problem.
Let's assume
grades = [73, 67, 38, 33]
to test our solution.
def gradingStudents(grades):
# Write your code here
Step 1️⃣ : Loop through the array of grades
We'll need to apply or grading logic to every grade
in the array by looping and storing the results in a new empty array called `result.
def gradingStudents(grades):
result = []
for grade in grades:
Step 2️⃣: Developing the grading logic using conditional statements. ⚙️
For us to track how each grade
relates to a multiple of 5, we use:
tmp = grade % 5
- If the
grade
is less than 38, no rounding occurs, and add the grade to theresults
array:
if grade < 38:
result.append(grade)
- If the difference between the
grade
and the next multiple of5
is less than3
, roundgrade
up to the next multiple of5
. - If the difference between the
grade
and the next multiple of5
is greater than or equal to3
, the grade will not be modified and the student's final grade will be returned. - Add the modified grades to the
results
array.
elif (5 - tmp) < 3:
result.append(grade + 5 - tmp)
elif (5 - tmp) == 3:
result.append(grade)
- else return all other grades outside this condition and add them to the
results
array.
else:
result.append(grade)
- Finally, return our completed
results
array.
return result
Full code:
def gradingStudents(grades):
result = []
# Write your code here
for grade in grades:
tmp = grade % 5
if grade < 38:
result.append(grade)
elif (5 - tmp) < 3:
result.append(grade + 5 - tmp)
elif (5 - tmp) == 3:
result.append(grade)
else:
result.append(grade)
return result
gradingStudents([73, 67, 38, 33])
>> [75, 67, 38, 33]
Hope this helps with understanding this challenge. Happy Coding!👨🏽💻