# Grading Students

# Grading Students

cakerusk + 36 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 + 4 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;`

aaronederby + 0 comments You are right. There is a pretty big epedimic of poor readability in coding these days. Some of these projects are pretty good proof of that.

munna1991vikram + 0 comments Get the complete information of windows 10 operating system by visiting here on this website. windows 10 free download Because this website provides us complete information of windows 10 operating system in your personal computer.

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 + 1 comment 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;

zeashaikh3306 + 0 comments Why are we checking <=3 and not some other number?

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 + 4 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()

ismailkuet + 1 comment Get some fun with Python 3::

return [n if n < 38 or n % 5 < 3 else (n + 5 - (n % 5)) for n in grades]

nvinson234 + 0 comments A lot of modulo use when it's not really needed. An alternate version:

return [max(n, (n + 2) // 5 * 5) if n >= 38 else n for n in grades]

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 + 3 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 + 2 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

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

vishalnirne98 + 1 comment how doesthe second if statement work

utk1234 + 0 comments oh, that is an equivalent of Ternery Operator ?: in other languages. Do_this

*if*this_is_true*else*do_thissay for example,

>>> x = 10 >>> y = 21 if x > 12 else 5

y will be equal to 21 if x is greater than 12 else y will be 5.

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 + 3 comments #!/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.

mtausif_0003 + 0 comments its so complicated logic

ismailkuet + 0 comments One line solution::

return [n if n < 38 or n % 5 < 3 else (n + 5 - (n % 5)) for n in grades]

ismailkuet + 0 comments @h201551072 You can do it in one line with list comprehensions

return [n if n < 38 or n % 5 < 3 else (n + 5 - (n % 5)) for n in grades]

archit_imsec10 + 2 comments 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

massimiliano_de1 + 0 comments nice one liner, but write something like that in my project and I'll fire you.

you must spare maintainer/programmer time, not whitespaces in the file.

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.

coder_aky + 1 comment # Python 3 , 3-liner

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

meghrajpardesi + 0 comments # One liner for abouve code

def gradingStudents(grades): return [g if g < 38 or g % 5 < 3 else g + (5 -(g % 5)) for g in grades]

soheil_jahangir1 + 0 comments awesome.

CyberZii + 0 comments Amazing. Bravo!

gabrielbb0306 + 0 comments Your solution is bad for the problem as it is today. You are printing the elements, but the problem, at least today, says you should return the array. If you do the assigning to the array in one line then it is not possible to check when to assign, so you will always assign, which will be much slower. For this reason i will have to give a downvote to your answer. Still pretty cool for a problem that ask to print them instead of returning, tho

vangalasuneel + 0 comments this soluation will not pass all test cases

ismailkuet + 0 comments Get some fun with Python 3::

return [n if n < 38 or n % 5 < 3 else (n + 5 - (n % 5)) for n in grades]

jasonganub + 10 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 [deleted]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!!!

max_aka21 + 0 comments You can just group 1st and 2nd if condition into single if condition

imerljak + 3 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; }) }

hroger19 + 0 comments Wow that's short!

svl_taylaran + 0 comments Short and sweet unlike mine lol

function gradingStudents(grades) { // Write your code here let finalGrades = new Array(grades.length); for (let x = 0; x <= grades.length - 1; x++) { if (grades[x] >= 38 && (grades[x] + 2) % 5 === 0) { finalGrades[x] = grades[x] + 2; } else if (grades[x] === 99 || (grades[x] >= 38 && (grades[x] + 1) % 5 === 0)) { finalGrades[x] = grades[x] + 1; } else { finalGrades[x] = grades[x]; } } return finalGrades; }

Totally forgot about the map method too.

sidinsomniac + 0 comments My JS solution:

function gradingStudents(grades) { let finalMarks = []; grades.forEach(grade => { grade < 38 || grade % 5 < 3 ? finalMarks.push(grade) : finalMarks.push(Math.ceil(grade/5)*5) }) return finalMarks; }

accounts47 + 1 comment The output asks us to print each grade on a new line. The function should actually return a list of values.

jmhrovat + 0 comments Thanks, this question should be re-written :/

samarthsegu1999 + 0 comments `public static List<Integer> gradingStudents(List<Integer> grades) { int n= grades.size(); List<Integer> result = new ArrayList<>(); for(int i=0;i<n;i++) { int ele=grades.get(i); if((ele%5)>=3 && ele>=38) ele=ele+(5-(ele%5)); else ele=ele; result.add(ele); } return result; }`

16FE1A0592CSE + 0 comments in java

`public static List<Integer> gradingStudents(List<Integer> grades) { for(int i=0;i<grades.size();i++){ if(grades.get(i)>=38){ int r=grades.get(i)%5; int diff=Math.abs(r-5); if(diff<3) grades.set(i,grades.get(i)+diff); } } return grades; }`

kevinlai31 + 0 comments C version is broken btw. main function calls "gradingStudents" with undeclared first argument. change the first argument to "n"

marinskiy + 1 comment 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 :)

Rekt4 + 0 comments Why do you add 5 to grade?

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 + 1 comment 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?

dhatrijani14 + 0 comments Which length of output you want to display ? which size of array would display? for all thoswe que solution is )*result_size = grades_size;

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]

Sort 1208 Discussions, By:

Please Login in order to post a comment