We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
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. :)
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.
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.
staticint[]solve(int[]grades){// Complete this functionfor(inti=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));}}returngrades;}
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. :)
(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.
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;
Grading Students
You are viewing a single comment's thread. Return to all 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. :)
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.
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.
It's a terrible solution. Hello? Have you ever heard of the words, "RE-READ YOUR WORK?" Oh, and plus, it doesn't work. :)
It works perfectly fine.
https://www.hackerrank.com/challenges/grading/submissions/code/39368045
In case of issues, let me know.
I have another solution for the same.
I did this with backward iteration and without your most inner If:
What's the point of the backwards iteration? It practically won't make any difference.
Yes it will! it will make the array go back wards and solution will be saved and appear in LIFO.
It is a stack implementation.
in your solution what happen if grade[i] = 67 them grades[i] % 5 = 7 and them 5-7 = -2 so 67 -2 = 65
No, it will not pass the second condition of the if statement. Because 67 % 5 = 2 not 7.
As @keickhoff1 pointed out 67 remains 67.
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. :)
very good, by the way it has to be grades.Length
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?
this program have some error check your program
Your answer is nice.But why you put -grades[i]<3 in if condition can you explain it.
(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.
Your link doesn't work, like the code provided above
my solution
Would this be better? For reability only. Im not sure. It's really the same solution.
int[] res = new int[grades.Length];
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.