# Grading Students

# Grading Students

cakerusk + 29 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.

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.

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.

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. :)

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 + 6 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; }

doctorj0023 + 2 comments 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)); } }

alexb760 + 2 comments in your solution what happen if grade[i] = 67 them grades[i] % 5 = 7 and them 5-7 = -2 so 67 -2 = 65

keickhoff1 + 0 comments No, it will not pass the second condition of the if statement. Because 67 % 5 = 2 not 7.

harshrajan00 + 1 comment [deleted]harshrajan00 + 0 comments As @keickhoff1 pointed out 67 remains 67.

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. :)

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

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]

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

tazpandey + 1 comment Your answer is nice.But why you put -grades[i]<3 in if condition can you explain it.

harshrajan00 + 0 comments (5 - grades[i] % 5) < 3 if this is the condition you are talking about then the question says that If the difference between the grade and the next multiple of 5 is less than 2.

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

rabbyalone + 2 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; }

aliriomambo + 0 comments [deleted]van07 + 0 comments Would this be better? For reability only. Im not sure. It's really the same solution.

int[] res = new int[grades.Length];

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

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; }

owenized + 1 comment Why use EOF?

deepesh463 + 0 comments it stands for end of file

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;

vijayashreeshet1 + 0 comments wow ..thank u

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();

sidra_mbn + 1 comment Excellent

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

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.........

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.

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.

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.

alternativo_vini + 0 comments thanks

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; }

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.

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 + 1 comment def solve(grades): result = [] for i in grades: if i >= 38: if i % 5 >= 3: i += (5 - i % 5) result.append(i) return result

JehanJoshi + 0 comments You can club both the if conditions using an and here and neatly get it in one line :)

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)

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)

ahmettortumlu25 + 1 comment so good.

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...

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

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

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.

arunesh_sarin + 0 comments [deleted]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 !

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;

sfelde21 + 1 comment haha I hope you're joking

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.

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;

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

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.

rajkumarkanjo + 0 comments nice logic ..

m_eli10 + 0 comments [deleted]harshrajan00 + 0 comments Even my mobile has 4GB ram.

jasonganub + 9 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)

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

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

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

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))

sammy9292 + 0 comments fantastic. Such simple logic!!!

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; }) }

marinskiy + 0 comments Here is

**Python 3**solution from my HackerrankPractice repository:for _ in range(int(input())): grade = int(input()) if grade >= 38 and grade % 5 >= 3: grade = (grade + 5) // 5 * 5 print(grade)

Feel free to ask if you have any questions :)

rakesh_shah19971 + 1 comment ## C Function -

int* solve(int grades_size, int* grades, int *result_size){

`// Complete this function int i, g, r; for (i=0; i<grades_size; i++) { g = grades[i]; if (g < 38) continue; if ((r = g%5) > 2) grades[i] = g + 5 -r; } *result_size = grades_size; return grades;`

}

All comments most welcome, thanks.

gobisvivek + 0 comments 1)int* solve(int grades_size, int* grades, int *result_size) -> In this line, why are we using "result size" variable? 2)*result_size = grades_size; -> What is the meaning of this line?

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

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(" "));

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])); } }

alok66661082 + 0 comments hahahahha lets copy

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;`

jaramillob93 + 1 comment Answer in Java 8

package solution; import java.io.*; import java.lang.annotation.ElementType; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); if (n >= 1 && n <= 60) { for(int a0 = 0; a0 < n; a0++){ int grade = in.nextInt(); // your code goes here if (grade >= 0 && grade <= 100 && grade >= 38) { if (multipleOf5(grade) - grade < 3) { grade = multipleOf5(grade); System.out.println(grade); } else { System.out.println(grade); } } else { System.out.println(grade); } } } } public static int multipleOf5(int G) { int aux = 0; if (G % 5 == 0) { return G; } else { while (G % 5 != 0) { G++; if (G % 5 == 0) { aux = G; } } } return aux; } }

Thanks!

goodman_d_erik + 0 comments I would suggest trying to consolidate those if/else blocks a bit. Using the modulus you can simplify rounding quite a bit. After reading in the grade I used a helper method as follows

public static int rounder(int input){ if (input >= 38 && 5-input%5 < 3){ return input += (5-input%5); } return input; }

It's not the cleanest, but hopefully that's pretty legible

RodneyShag + 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

RodneyShag + 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;

Sort 830 Discussions, By:

Please Login in order to post a comment