# Grading Students

- VN
cakerusk + 27 comments Java one-liner code :

System.out.println(grade < 38 || grade % 5 < 3 ? grade : grade + (5 - (grade % 5)));

eavestn + 0 comments [deleted]eavestn + 0 comments [deleted]eavestn + 5 comments Nice solution. I am writing in C#, myself. I def. had to start with a more fleshed-out solution and work my way down to this. I wonder if a lengthier solution would be more communicative of the imposed requirements. I pushed mine up as a one-liner, though.

- VN
cakerusk + 2 comments Thanks. A lengthier solution might improve readability but compromise on lines of code (maintainability). Either way it doesn't affect runtime, so it comes down to a matter of personal preference I guess. :)

sungmkim80 + 1 comment I don't agree that a lengthier answer would compromise maintainability. If you deal with a smaller chunk of code, it's easier to maintain as you can focus on a narrow logic.

- VN
cakerusk + 1 comment Probably not in this question but I pointed that out in general from an interview/development perspective. For this particular qs, it's only a matter of preference.

- HS
JavaANDHannah + 1 comment It's a terrible solution. Hello? Have you ever heard of the words, "RE-READ YOUR WORK?" Oh, and plus, it doesn't work. :)

- VN
cakerusk + 2 comments It works perfectly fine.

https://www.hackerrank.com/challenges/grading/submissions/code/39368045

In case of issues, let me know.

uniyal_guru + 5 comments I have another solution for the same.

static int[] solve(int[] grades){ // Complete this function for(int i=0;i<grades.length;i++) { if(grades[i]>=38) { if(grades[i]+(5-grades[i]%5)-grades[i]<3) grades[i]=(grades[i]+(5-grades[i]%5)); } } return grades; }

- JG
doctorj0023 + 1 comment I did this with backward iteration and without your most inner If:

for(int i = grades.Length-1; i >=0; i--) { if(grades[i] >= 38 && grades[i] % 5 >2) { grades[i] = grades[i] + (5 - (grades[i] % 5)); } }

PareshSalunke + 0 comments Your solution is perfectly fine but as a first draft. Whenever possible take out common operations and put it in a variable so next time what if the moderator said that multiple of 6 instead of 5 . you'll have to change at 2 places in your case but it may be a become bigger issue in complex chunk. Just saying. :)

- AS
hard_system + 0 comments very good, by the way it has to be grades.Length

- AG
avi_ganguly94 + 1 comment in the inner if() , why did you do a grades[i] + blahBlahPivot - grades[i]? i mean, grades[i]-grades[i] is zero anyway so why bother?

ssaulakh2 + 0 comments [deleted]

- KS
krishna_kumar_b1 + 0 comments this program have some error check your program

- DW
devin99 + 0 comments Your link doesn't work, like the code provided above

rabbyalone + 0 comments my solution

static int[] solve(int[] grades) { int[] res = new int[grades.Length]; for (int i = 0; i < grades.Length; i++) { if (grades[i] % 5 > 2 && !(grades[i] < 38)) res[i] = grades[i] + (5-grades[i]%5); else res[i] = grades[i]; } return res; }

- PH
piyushhajare + 5 comments A simple and perhaps readable solution.. You can use n instead of temp as its just a dummy..

int main() { int n,temp; cin>>temp; while(cin>>n) { if(n<38) cout<<n<<endl; else if(n%5 >= 3) cout<<n+ (5-n%5)<<endl; else cout<<n<<endl; } return 0; }

- RO
owenized + 0 comments Why use EOF?

- AS
- AB
agnivabasak1 + 1 comment according to me it shouldn't work but if it passed test cases ,then i dont know how .the reason is that consider a number like 73 ,then n%5 should give 3 ,and hence it would redirect to the else statement and print n, whereas it should have printed 75 as we need to consider the next multiple of 5 ,75-73 =2 (<3) .Correct me ,if i am wrong ,anyone. (might be i am wrong too :P ,i developed my solution in my way ,though!)

steven_tang + 0 comments 73 % 5 = 3 and 3 <= 3, so it'll satisfy the else if condition. Then we have cout << 73 + (5- 73%5) << endl; which simplifies to cout << 73 + 2 << endl;

- VS
vijayashreeshet1 + 0 comments wow ..thank u

- HS
JavaANDHannah + 0 comments [deleted] borb_ + 1 comment You can do the same thing in C# ->

return grades.Select(grade => grade < 38 || (grade % 5 < 3) ? grade : grade + (5 - grade % 5 )).ToArray();

return grades.Select(grade => grade < 38 || (grade % 5 < 3) ? grade : grade + (5 - grade % 5 )).ToArray();

- SM
sidra_mbn + 1 comment Excellent

- SM
sidra_mbn + 1 comment Can you please also tell me that ToArray is to be used only when we are returning an array?

- MS
moh_so2001 + 0 comments `foreach(`$grades as $`grade){`

grade < 38 || grade : grade % 5))); } return $g;

php code

eavestn + 0 comments Don't know why I got downvoted for a simple comment?

rajatdhoot + 0 comments Great Solution.........

- A
alexandre_ragal1 + 3 comments Got some fun with Java 8:

Arrays.stream(new int[n]).map(i-> in.nextInt()).map(g -> (g >= 38 && g % 5 >=3) ? g+5-g%5 : g).forEach(System.out::println);

jude_niroshan11 + 0 comments nice shortcut for nearest 5th divisible value.

g+5 - g%5

ashishsinghchau1 + 1 comment Does not working out.It did some basics mistakes on the Editor.

- A
alexandre_ragal1 + 0 comments It is working since I passed all tests.

public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); Arrays.stream(new int[n]).map(i-> in.nextInt()).map(g -> g >= 38 && g % 5 >=3 ? g+5-g%5 : g).forEach(System.out::println); }

a_gutsal + 0 comments Arrays.stream(grades).map(grade -> (grade < 38) || (grade % 5 < 3)?grade:((int)(grade / 5) + 1) * 5).toArray()

Cuteboy06 + 1 comment Can anyone explain me how it is printing grade for the values like 67.

- VN
cakerusk + 3 comments The rules for rounding off is stated as follows :

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.

Since the value 67 is greater than 38 and the difference between value and next multiple of 5 (i.e. 70) is 3 (70 - 67) which is not less than 3, it will not be rounded. Hence, 67 i.e. 'grade' value is printed.

However, for the value 68, the difference is 2 (70 - 68) < 3, so the grade will be rounded to the next multiple by the calculation :

*grade + (5 - (grade % 5)) = 68 + (5 - (68 % 5))**= 68 + (5 - 3)**= 68 + 2**= 70 (next multiple of 5)*Hope this helps! :)

Cuteboy06 + 0 comments Thanks for your time :)

TejanSD17 + 0 comments Thank you.

- VM
alternativo_vini + 0 comments thanks

- PH
piyushhajare + 1 comment A simple and perhaps readable solution.. You can use n instead of temp as it's dummy..

int main() { int n,temp; cin>>temp; while(cin>>n) { if(n<38) cout<<n<<endl; else if(n%5 >= 3) cout<<n+ (5-n%5)<<endl; else cout<<n<<endl; } return 0; }

- BG
bg2407111 + 1 comment Just a tip I've learned from my professors, you should rarely ever have to rewrite the same lines of code, see how your initial if and your default else are doing the same thing? You could condense this by doing this:

`if(n>38 && n%5 > 3) { cout << n+(5-n%5) << endl; } else { cout << n << endl; }`

Makes it still readable, but in less lines of code. Also, in Industry code it's generally good practice to use curly braces on the body of if statements to prevent any confusion.

ssaulakh2 + 0 comments Will I get kicked out if I do this from nothing.

- MG
Ardillo + 5 comments Python3 one-liner:

grades = [int(input()) for __ in range(int(input()))]

[print(g+5 - g%5 if g%5 > 2 and g>37 else g) for g in grades]

h201551072 + 2 comments i am new in pyhton can u help me with the mistake?

#!/bin/python import sys def solve(grades): r=[] y=0 d=0 # Complete this function for i in range(0,len(grades)-1): if(grades[i]>=38): y=grades[i]/5 d=5*(y+1)-grades[i] if(d<3): r[i]=5*(y+1) else: r[i]=grades[i] else: r[i]=grades[i] return r n = int(raw_input().strip()) grades = [] grades_i = 0 for grades_i in xrange(n): grades_t = int(raw_input().strip()) grades.append(grades_t) result = solve(grades) print "\n".join(map(str, result))

immanoj16 + 0 comments def solve(grades): result = [] for i in grades: if i >= 38: if i % 5 >= 3: i += (5 - i % 5) result.append(i) return result

- MI
Mandar_Inamdar + 1 comment you are doing y=grades[i]/5 which will give you decimal numbers. round division using math functions or simply like: y=int(grades[i]/5)

- DN
ditonikola + 1 comment #!/bin/python3 import sys def solve(grades): for k in range(0,n): if grades[k]<38: print(grades[k]) else: l=grades[k]/10 m=round(l) if 0.25<abs(l-m)<0.5 or 0.5<abs(l-m)<0.75: a=round(grades[k]/5) b=a*5 c=b-grades[k] if c<0: if c<-3: print(b) else: print(grades[k]) else: if c<3: print(b) else: print(grades[k]) else: f=m*10 d=f-grades[k] if d<0: if d<-3: print(f) else: print(grades[k]) else: if d<3: print(f) else: print(grades[k]) n = int(input().strip()) grades = [] grades_i = 0 for grades_i in range(n): grades_t = int(input().strip()) grades.append(grades_t) result = solve(grades) print ("\n".join(map(str, result)))

whats wrong with this pls tell me, it gives me runtime error, but passes 5 times

- _
_dugy + 0 comments You have to return array of ints from "solve" function.

archit_imsec10 + 1 comment And how about this?

map(lambda x: 5*(1 + x//5) if (x > 37 and ((x%5) > 2)) else x, grades)

- AT
ahmettortumlu25 + 1 comment so good.

- SM
sidra_mbn + 0 comments Thank you

Spiznak + 0 comments 1 iteration, almost 1-liner. O(n) is slightly better than O(2n):

for _ in range(int(input())): x = int(input()) print(x + (5 - x % 5)) if x >= 38 and 5 - x % 5 < 3 else print(x)

I wouldn't be surprised to learn that there is a better way to do it, though...

- DG
Dimple151997 + 1 comment Can you explain the code.. why do we write __ in for loop and 2nd line in [] brackets

- MM
MilosMM + 0 comments Hi, because we don't care about the value of the iterator.

- RN
rudralittle + 0 comments i ran it on my jupyer notebook. i cant understand the output. input>>>>>>>>>>>>> grades = [int(input()) for __ in range(int(input()))]

[print(g+5 - g%5 if g%5 > 2 and g>37 else g) for g in grades]

output>>>>>> 5 45 66 23 56 98 45 66 23 56 100 [None, None, None, None, None]

chris_dziewa + 0 comments I knew I would find something much shorter than my code here but wow! Nice work!

ashishsinghchau1 + 0 comments Your code does not working out in the editor.

- A
arunesh_sarin + 0 comments [deleted] - A
arunesh_sarin + 1 comment but this code should round off 67 as 67%5 is 2 which is less 3 . Help me undertand this please.

borb_ + 0 comments The problem states that the rounding should occur if the DIFFERENCE is LESS than 3. The difference here is 3 so it is not rounded.

ummahusla + 0 comments Wow! Clearly beatiful and cleanest way I've seen so far

SupreethMC + 0 comments Neat !

- VS
vijayashreeshet1 + 0 comments i dont understan java .. bt i understood the logic .. thank you:)

UWUTM8 + 1 comment I've got a pretty sick one-liner too for C++ can your boy get some upvotes =D

for (int i = 0; i < grades.size(); i++){int nearest5Multiple = 5*((grades[i]/5)+1);if (grades[i] >= 38 &&(nearest5Multiple-grades[i]) < 3 ){grades[i] = nearest5Multiple;}}return grades;

- SF
sfelde21 + 1 comment haha I hope you're joking

- DN
ditonikola + 0 comments No im not joking

WatchandLearn + 0 comments it looks good,but keeping result of (grade%5) in a variable could save you from calculating it two times :)

dipznjoy + 0 comments actually not , as it will work for 41,42 . as 41 % 5 =1 , but we want to have the for 43,44. that goes for all the cases.

Tunvir07 + 0 comments Great.

- NC
- SN
santhoshkumar_n + 0 comments It works

rishabh0_9 + 0 comments This is my solution

static int[] solve(int[] grades){ // Complete this function for(int i=0; i<grades.length; i++){ if(grades[i]>=38 && grades[i]%5>2) grades[i] = grades[i] + (5 - (grades[i]%5)); } return grades;

ramamoorthypand1 + 0 comments [deleted]saiful007 + 0 comments if (grade > 37){ int newGrade = grade; for (int i=1; i<=2; i++){ newGrade++; if(newGrade%10 == 5 || newGrade%10 == 0){ grade = newGrade; return grade; } } } return grade;

- VP
vikramhpatel12 + 1 comment won't work for grade =73 since 73%5 is 3

saiful007 + 0 comments i dont think so it pass all the test cases ...but thanks for your kindness

- TS
tejasree438 + 0 comments I achived useing my own implementaion upto 20 lines to achieve this code.But your solution is just awe.I will adopt for this technique.Thank you.

- RK
rajkumarkanjo + 0 comments nice logic ..

jasonganub + 8 comments A simple and clean solution with only one print statement and minimal conditionals.

if grade >= 38: if grade % 5 == 3: grade += 2 elif grade % 5 == 4: grade += 1 print(grade)

jakemager + 1 comment awesome use of the modulo operation

trungskigoldberg + 1 comment this solution is mathematically cool man

franchetti + 2 comments Could be condensed to a single condition if you wanted.

if grade >= 38: if grade % 5 > 2: grade += 5 - (grade % 5) print(grade)

- SM
cuteprince1989 + 0 comments this is such a clever solution great mind...

- SM
cuteprince1989 + 1 comment it is not working it is giving me error unsupported operand type for %: list and int. can any help me? there compiler is not working correctly, i have run this code in python 2 in my system it is working fine but not in here

ThaiNguyen + 0 comments You are using grade, which is a list. You have to access the indices in the list using loop like this

for i in grade: if i >= 38: if i % 5 == 3: i += 2 elif i % 5 == 4: i += 1 print (i)

h201551072 + 2 comments new in python can u help me with this code

#!/bin/python import sys def solve(grades): r=[] y=0 d=0 # Complete this function for i in range(0,len(grades)-1): if(grades[i]>=38): y=grades[i]/5 d=5*(y+1)-grades[i] if(d<3): r[i]=5*(y+1) else: r[i]=grades[i] else: r[i]=grades[i] return r n = int(raw_input().strip()) grades = [] grades_i = 0 for grades_i in xrange(n): grades_t = int(raw_input().strip()) grades.append(grades_t) result = solve(grades) print "\n".join(map(str, result))

immanoj16 + 0 comments def solve(grades): result = [] for i in grades: if i >= 38: if i % 5 >= 3: i += (5 - i % 5) result.append(i) return result

- ST
tomarshubham49 + 0 comments fgfhyhyrihowhhflhiihiw{} bhai yeah galti hai teri program mmm

- DC
dylanjcastillo + 0 comments Great solution!

lukus_2222 + 0 comments neat

heres the same solution in PHP

function solve($grades){ for ($i=0;$i <= sizeof($grades); $i++){ if ($grades[$i] >= 38){ if ($grades[$i] % 5 == 3){ $grades[$i] += 2; } else if ($grades[$i] % 5 == 4){ $grades[$i] += 1; } } } return $grades; }

16wh1a05b7 + 0 comments wow!! nice

lvsz_ + 0 comments A difficult and intricate solution with only one line and

*no*conditionals (nor variables).print . ((uncurry (+) .) . ((. ((*) . fromEnum)) . flip second) <*> (uncurry ((. (>) 3) . (&&) . (<) 37))) . ((,) <*> (flip mod 5 . negate))

- IM
imerljak + 0 comments Javascript with ES6 :)

function solve(grades){ return grades.map((n) => { let diff = 5 - (n % 5); if(diff < 3 && n >= 38) { n += diff; } return n; }) }

enilaydagdemir + 3 comments Here is my solution with C#:

static int solve(int grade){ int result = grade; if (grade >= 38) { if ((5 - (grade % 5) + grade) - grade < 3) result = 5 - (grade % 5) + grade; } return result; } static void Main(String[] args) { int n = Convert.ToInt32(Console.ReadLine()); int[] grades = new int[n]; for(int grades_i = 0; grades_i < n; grades_i++){ grades[grades_i] = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(solve(grades[grades_i])); } }

- AS
alok66661082 + 0 comments hahahahha lets copy

- KQ
kevinq93 + 1 comment static int[] solve(int[] grades, int n){ // Complete this function for(int i = 0; i < n;i++) { if(grades[i] >= 38 && grades[i] % 5 > 2) { grades[i] = grades[i]/5*5+5; } } return(grades); } static void Main(String[] args) { int n = Convert.ToInt32(Console.ReadLine()); int[] grades = new int[n]; for(int grades_i = 0; grades_i < n; grades_i++){ grades[grades_i] = Convert.ToInt32(Console.ReadLine()); } int[] result = solve(grades,n); Console.WriteLine(String.Join("\n", result));

Similar answer, slightly different logic.

abdullah_mamun11 + 0 comments Superb

prodip2416 + 0 comments may be this also Goooddddddd

int[] result = grades;

`for (int i = 0; i < grades.Length; i++) { if (grades[i] >= 38) { if (5 - (grades[i]%5) < 3) { result[i] = (5-grades[i]%5) + grades[i]; } } } return result;`

rshaghoulian + 1 comment ### Java solution - passes 100% of test cases

From my HackerRank solutions.

import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); while (n-- > 0) { int grade = scan.nextInt(); System.out.println(roundGrade(grade)); } } private static int roundGrade(int grade) { if (grade >= 38) { int gradeMod5 = grade % 5; if (gradeMod5 > 2) { grade += 5 - gradeMod5; } } return grade; } }

Let me know if you have any questions.

Sukhnoor + 1 comment if (gradeMod5 > 2) { grade += 5 - gradeMod5; } explain please

rshaghoulian + 0 comments That's the code to do: "If the difference between the grade and the next multiple of 58 is less than 3, round grade up to the next multiple of 5."

The "If the difference between the grade and the next multiple of 58 is less than 3" corresponds to

if (gradeMod5 > 2)

The "round grade up to the next multiple of 5" corrsponds to

grade += 5 - gradeMod5;

ElegantAlgorithm + 1 comment for a0 in range(int(input())): print((lambda g: (g//5+1)*5 if g > 37 and g % 5 > 2 else g)(int(input())))

gersona + 1 comment it indeeds works on all test cases but so hard to read and understand for a python newbie like me (like most onliners actually) thanks anyway

ElegantAlgorithm + 0 comments I think this code is more elegant than my previous solution. I was just playing with lambda.

for g in [int(input()) for x in range(int(input()))]: print((g//5+1)*5 if g > 37 and g % 5 > 2 else g)

- MS
moh_so2001 + 0 comments By php

foreach($grades as $grade){ $g[]= ($grade < 38 || $grade % 5 < 3 ? $grade : $grade + (5 - ($grade % 5))); } return $g;

- KP
kodandapani_v + 2 comments import java.util.Scanner; public class gradingProcess {

`public static void main(String args[]) { Scanner sc = new Scanner(System.in); int size = sc.nextInt(); int[] grades = new int[size]; for(int i=0;i<size;i++) { grades[i]=sc.nextInt(); grades[i] = (grades[i]>=38)?((grades[i]%5>=3)?(grades[i]+(5-(grades[i]%5))):grades[i]):grades[i]; } for(int i=0;i<size;i++) System.out.println(grades[i]); }`

}

bijeli_papir + 0 comments nice

- AB
arunboopathi2 + 0 comments [deleted]

feling87 + 0 comments Javascript solution:

function solve(grades){ var index = 0; while(index < grades.length) { if (grades[index]%5 >=3 && grades[index] >37) { grades[index] = grades[index] - grades[index]%5 + 5; } ++index; } return grades; } function main() { var n = parseInt(readLine()); var grades = []; for(var grades_i = 0; grades_i < n; grades_i++){ grades[grades_i] = parseInt(readLine()); } var result = solve(grades); result.forEach(e => console.log(e)) // console.log(result.join(" "));

- KS
ksanner + 0 comments The C version of this problem has a bug in the main function that calls the function to be implemented.

This line:

int* result = gradingStudents(grades_count, grades, &result_count);

should be replaced with:

int* result = gradingStudents(n, grades, &result_count);

Sort 705 Discussions, By:

Please Login in order to post a comment