Grading student hackerrank solution with explanation, first lets see what the problem is :
Grading Students Hacker Rank Problem:
HackerLand University has the following grading policy: 1. Every student receives a grade in the inclusive range from 1 to 100. 2. Any grade less than 30 is a failing grade. Sam is a professor at the university and likes to round each student's grade according to these rules: 1.If the difference between the grade and the next multiple of 5 is less than 3 , round up to the next multiple of 5. 2.If the value of grade is less than 38 , no rounding occurs as the result will still be a failing grade. Examples 1. grade = 84 round to85 (85 - 84 is less than 3) 2. grade = 29 do not round (result is less than 40) 3. grade = do not round (60 - 57 is 3 or higher) Given the initial value of grade for each of Sam's n students, write code to automate the rounding process. Function Description Complete the function gradingStudents in the editor below. 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
Grading students Hackerrank solution JavaScript:
function gradingStudents(grades) { grades.forEach((grade, i) => { let count = 5 - grade % 5; if (grade >= 38 && count < 3) {grades[i] += count; } }); return grades; }
Grading students Hackerrank solution Python:
def gradingStudents(grades): for i in range(len(grades)): if grades[i] < 38: continue diff = grades[i] % 5 if 5 - diff < 3: grades[i] += 5 - diff return grades
Grading students Hackerrank solution C#:
public static List GradingStudents(List grades) { for (int i = 0; i < grades.Count; i++) { int diff = 5 - grades[i] % 5; if (grades[i] >= 38 && diff < 3) grades[i] += diff; } return grades; }
Explanation of Code:
- First, we need to find whether grade is less greater than 38 or not.
- Then we need to find if the number on diving by 5 gives a number less than 3 or not using given line 5 – grade[i] % 5 and made it equal to some temp variable.
- If both conditions satisfied then add the temp variable to grades else return the original grades
Leave a Reply