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.
Loading...
  • Practice
  • Compete
  • Jobs
  • Leaderboard
  • Hiring developers?
  1. Practice
  2. Algorithms
  3. Implementation
  4. Grading Students
  5. Discussions

Grading Students

  • Problem
  • Submissions
  • Leaderboard
  • Discussions
  • Editorial

Sort 1366 Discussions, By:

votes
  • recency
  • votes

Please Login in order to post a comment

  • cakerusk 3 years ago+ 38 comments

    Java one-liner code :

        System.out.println(grade < 38 || grade % 5 < 3 ? grade : grade + (5 - (grade % 5)));
    
    176|
    Permalink
    • eavestn 3 years ago+ 0 comments
      [deleted]
      0|
      ParentPermalink
    • eavestn 3 years ago+ 0 comments
      [deleted]
      -1|
      ParentPermalink
    • eavestn 3 years ago+ 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.

      -5|
      ParentPermalink
      • cakerusk 3 years ago+ 7 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. :)

        0|
        ParentPermalink
        • sungmkim80 3 years ago+ 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.

          18|
          ParentPermalink
          • cakerusk 3 years ago+ 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.

            1|
            ParentPermalink
            • JavaANDHannah 3 years ago+ 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. :)

              -24|
              ParentPermalink
              • cakerusk 3 years ago+ 2 comments

                It works perfectly fine.

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

                In case of issues, let me know.

                1|
                ParentPermalink
                • uniyal_guru 2 years ago+ 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;
                      }
                  
                  7|
                  ParentPermalink
                  • doctorj0023 2 years ago+ 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));
                                    }
                                }
                    
                    0|
                    ParentPermalink
                    • simonhaan 2 years ago+ 1 comment

                      What's the point of the backwards iteration? It practically won't make any difference.

                      9|
                      ParentPermalink
                      • ssaulakh2 2 years ago+ 0 comments

                        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.

                        -9|
                        ParentPermalink
                    • alexb760 1 year ago+ 3 comments

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

                      -8|
                      ParentPermalink
                      • keickhoff1 1 year ago+ 0 comments

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

                        0|
                        ParentPermalink
                      • harshrajan00 1 year ago+ 1 comment
                        [deleted]
                        0|
                        ParentPermalink
                        • harshrajan00 1 year ago+ 0 comments

                          As @keickhoff1 pointed out 67 remains 67.

                          0|
                          ParentPermalink
                      • anshulbadyal11 2 months ago+ 0 comments

                        please correct yourself and please do learn how to use modulus operator

                        0|
                        ParentPermalink
                  • PareshSalunke 2 years ago+ 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. :)

                    4|
                    ParentPermalink
                  • hard_system 2 years ago+ 0 comments

                    very good, by the way it has to be grades.Length

                    0|
                    ParentPermalink
                  • avi_ganguly94 2 years ago+ 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?

                    1|
                    ParentPermalink
                    • ssaulakh2 2 years ago+ 0 comments
                      [deleted]
                      0|
                      ParentPermalink
                  • krishna_kumar_b1 2 years ago+ 0 comments

                    this program have some error check your program

                    0|
                    ParentPermalink
                  • tazpandey 1 year ago+ 1 comment

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

                    0|
                    ParentPermalink
                    • harshrajan00 1 year ago+ 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.

                      -2|
                      ParentPermalink
                • devin99 2 years ago+ 0 comments

                  Your link doesn't work, like the code provided above

                  0|
                  ParentPermalink
        • rabbyalone 2 years ago+ 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;
                  }
          
          6|
          ParentPermalink
          • aliriomambo 1 year ago+ 0 comments
            [deleted]
            1|
            ParentPermalink
          • van07 1 year ago+ 1 comment

            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;
            
            2|
            ParentPermalink
            • skoffice47 1 month ago+ 0 comments

              Yes i am totally agreed with this article and i just want say that this article is very nice and very informative article.I will make sure to be reading your blog more. You made a good point but I can't help but wonder, what about the other side? !!!!!!Thanks voyance gratuite immediate par telephone

              0|
              ParentPermalink
        • aaronederby 10 months ago+ 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.

          1|
          ParentPermalink
        • munna1991vikram 5 months ago+ 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.

          -1|
          ParentPermalink
        • skoffice47 1 month ago+ 0 comments

          In the world of www, there are countless blogs. But believe me, this blog has all the perfection that makes it unique in all. I will be back again and again. Fortnite V bucks generator

          0|
          ParentPermalink
        • skoffice47 1 month ago+ 0 comments

          Thanks for a very interesting blog. What else may I get that kind of info written in such a perfect approach? I’ve a undertaking that I am simply now operating on, and I have been at the look out for such info. viking appliance repair near me

          0|
          ParentPermalink
        • skoffice47 1 month ago+ 0 comments

          Thanks for a very interesting blog. What else may I get that kind of info written in such a perfect approach? I’ve a undertaking that I am simply now operating on, and I have been at the look out for such info. Demir Leather

          0|
          ParentPermalink
      • piyushhajare 3 years ago+ 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;
        }
        
        13|
        ParentPermalink
        • owenized 3 years ago+ 1 comment

          Why use EOF?

          0|
          ParentPermalink
          • deepesh463 1 year ago+ 0 comments

            it stands for end of file

            -4|
            ParentPermalink
        • aalukony 3 years ago+ 1 comment

          what is the use of temp?

          0|
          ParentPermalink
          • aggregate 2 years ago+ 0 comments

            to satisfy the input condition.

            1|
            ParentPermalink
        • agnivabasak1 2 years ago+ 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!)

          0|
          ParentPermalink
          • steven_tang 2 years ago+ 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;

            1|
            ParentPermalink
            • zeashaikh3306 9 months ago+ 0 comments

              Why are we checking <=3 and not some other number?

              0|
              ParentPermalink
        • vijayashreeshet1 2 years ago+ 0 comments

          wow ..thank u

          -1|
          ParentPermalink
        • iamuday 2 years ago+ 1 comment

          EOF?

          0|
          ParentPermalink
          • ssaulakh2 2 years ago+ 0 comments

            It is a achronym of null(0) in C language in stdio.h

            0|
            ParentPermalink
      • JavaANDHannah 3 years ago+ 0 comments
        [deleted]
        -1|
        ParentPermalink
      • borb_ 2 years ago+ 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();

        9|
        ParentPermalink
        • sidra_mbn 2 years ago+ 1 comment

          Excellent

          0|
          ParentPermalink
          • sidra_mbn 2 years ago+ 1 comment

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

            0|
            ParentPermalink
            • moh_so2001 2 years ago+ 0 comments
              foreach(`$grades as $`grade){ 
              

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

              php code

              0|
              ParentPermalink
      • eavestn 2 years ago+ 0 comments

        Don't know why I got downvoted for a simple comment?

        -6|
        ParentPermalink
    • rajatdhoot 3 years ago+ 0 comments

      Great Solution.........

      1|
      ParentPermalink
    • alexandre_ragal1 3 years ago+ 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);
      
      3|
      ParentPermalink
      • jude_niroshan11 3 years ago+ 0 comments

        nice shortcut for nearest 5th divisible value.

        g+5 - g%5
        
        9|
        ParentPermalink
      • ashishsinghchau1 2 years ago+ 1 comment

        Does not working out.It did some basics mistakes on the Editor.

        0|
        ParentPermalink
        • alexandre_ragal1 2 years ago+ 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);
              }
          
          1|
          ParentPermalink
      • a_gutsal 2 years ago+ 0 comments
        Arrays.stream(grades).map(grade -> (grade < 38) || (grade % 5 < 3)?grade:((int)(grade / 5) + 1) * 5).toArray()
        
        1|
        ParentPermalink
      • ismailkuet 5 months ago+ 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]

        3|
        ParentPermalink
        • nvinson234 4 months ago+ 2 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]
          
          0|
          ParentPermalink
          • auratice 3 months ago+ 0 comments

            it would be really helpful if you could explain this. thank you!

            1|
            ParentPermalink
          • j0096 2 months ago+ 0 comments

            Nice. I think it is little bit more readaable if you use the same approach but use a single modulus:

            return [max(n, 5 - (n % 5) + n) if n > 37 else n for n in grades]

            (Sorry format as code button is missing from editor)

            0|
            ParentPermalink
    • Cuteboy06 3 years ago+ 1 comment

      Can anyone explain me how it is printing grade for the values like 67.

      1|
      ParentPermalink
      • cakerusk 3 years ago+ 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! :)

        9|
        ParentPermalink
        • Cuteboy06 3 years ago+ 0 comments

          Thanks for your time :)

          1|
          ParentPermalink
        • TejanSD17 2 years ago+ 0 comments

          Thank you.

          1|
          ParentPermalink
        • alternativo_vini 2 years ago+ 0 comments

          thanks

          0|
          ParentPermalink
    • piyushhajare 3 years ago+ 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;
      }
      
      0|
      ParentPermalink
      • bg2407111 2 years ago+ 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.

        1|
        ParentPermalink
        • ssaulakh2 2 years ago+ 0 comments

          Will I get kicked out if I do this from nothing.

          image

          0|
          ParentPermalink
    • Ardillo 3 years ago+ 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]

      11|
      ParentPermalink
      • h201551072 3 years ago+ 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))
        
        3|
        ParentPermalink
        • immanoj16 3 years ago+ 3 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
          
          13|
          ParentPermalink
          • JehanJoshi 1 year ago+ 0 comments

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

            0|
            ParentPermalink
          • vishalnirne98 5 months ago+ 1 comment

            how doesthe second if statement work

            1|
            ParentPermalink
            • utkarshGG 5 months ago+ 0 comments

              oh, that is an equivalent of Ternery Operator ?: in other languages. Do_this if this_is_true else do_this

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

              0|
              ParentPermalink
          • mahmut_bilgen 3 months ago+ 0 comments

            Good solution! Congratulation !

            def gradingStudents(grades): # Write your code here result = [] for grade in grades: rounding_num= ((grade // 5)+1)*5 if grade<38 : result.append(grade) elif rounding_num-grade<3: result.append(rounding_num) else: result.append(grade) return result

            0|
            ParentPermalink
        • Mandar_Inamdar 2 years ago+ 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)

          0|
          ParentPermalink
          • ditonikola 2 years ago+ 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

            0|
            ParentPermalink
            • _dugy 2 years ago+ 0 comments

              You have to return array of ints from "solve" function.

              0|
              ParentPermalink
            • mtausif_0003 9 months ago+ 0 comments

              its so complicated logic

              0|
              ParentPermalink
            • ismailkuet 5 months ago+ 0 comments

              One line solution::

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

              0|
              ParentPermalink
        • ismailkuet 5 months ago+ 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]

          0|
          ParentPermalink
      • archit_imsec10 2 years ago+ 2 comments

        And how about this?

        map(lambda x: 5*(1 + x//5) if (x > 37 and ((x%5) > 2)) else x, grades)
        
        17|
        ParentPermalink
        • ahmettortumlu25 2 years ago+ 1 comment

          so good.

          0|
          ParentPermalink
          • sidra_mbn 2 years ago+ 0 comments

            Thank you

            0|
            ParentPermalink
        • massimiliano_de1 9 months ago+ 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.

          1|
          ParentPermalink
      • Spiznak 2 years ago+ 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...

        1|
        ParentPermalink
      • Dimple151997 2 years ago+ 1 comment

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

        0|
        ParentPermalink
        • MilosMM 2 years ago+ 0 comments

          Hi, because we don't care about the value of the iterator.

          0|
          ParentPermalink
      • rudralittle 2 years ago+ 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]

        0|
        ParentPermalink
    • chris_dziewa 3 years ago+ 0 comments

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

      -1|
      ParentPermalink
    • ashishsinghchau1 2 years ago+ 0 comments

      Your code does not working out in the editor.

      0|
      ParentPermalink
    • arunesh_sarin 2 years ago+ 0 comments
      [deleted]
      0|
      ParentPermalink
    • arunesh_sarin 2 years ago+ 1 comment

      but this code should round off 67 as 67%5 is 2 which is less 3 . Help me undertand this please.

      0|
      ParentPermalink
      • borb_ 2 years ago+ 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.

        0|
        ParentPermalink
    • ummahusla 2 years ago+ 0 comments

      Wow! Clearly beatiful and cleanest way I've seen so far

      0|
      ParentPermalink
    • SupreethMC 2 years ago+ 0 comments

      Neat !

      0|
      ParentPermalink
    • vijayashreeshet1 2 years ago+ 0 comments

      i dont understan java .. bt i understood the logic .. thank you:)

      0|
      ParentPermalink
    • UWUTM8 2 years ago+ 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;
      
      -2|
      ParentPermalink
      • sfelde21 2 years ago+ 1 comment

        haha I hope you're joking

        0|
        ParentPermalink
        • ditonikola 2 years ago+ 0 comments

          No im not joking

          0|
          ParentPermalink
    • WatchandLearn 2 years ago+ 0 comments

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

      0|
      ParentPermalink
    • dipznjoy 2 years ago+ 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.

      0|
      ParentPermalink
    • Tunvir07 2 years ago+ 0 comments

      Great.

      0|
      ParentPermalink
    • nzwisisa 2 years ago+ 1 comment

      Python - 2 liner

      grades = [int(input()) for x in range(int(input()))]
      print('\n'.join(map(str,[(i-(i%5)+5) if (i>=38 and i%5>2) else i for i in grades])))
      

      image

      5|
      ParentPermalink
      • crynut84 2 years ago+ 0 comments

        awsome! your code!

        0|
        ParentPermalink
    • santhoshkumar_n 2 years ago+ 0 comments

      It works

      0|
      ParentPermalink
    • rishabh0_9 2 years ago+ 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;
      
      0|
      ParentPermalink
    • ramamoorthypand1 2 years ago+ 0 comments
      [deleted]
      0|
      ParentPermalink
    • saiful007 2 years ago+ 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;

      0|
      ParentPermalink
    • vikramhpatel12 2 years ago+ 1 comment

      won't work for grade =73 since 73%5 is 3

      0|
      ParentPermalink
      • saiful007 2 years ago+ 0 comments

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

        0|
        ParentPermalink
    • tejasree438 1 year ago+ 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.

      0|
      ParentPermalink
    • rajkumarkanjo 1 year ago+ 0 comments

      nice logic ..

      0|
      ParentPermalink
    • m_eli10 1 year ago+ 0 comments
      [deleted]
      0|
      ParentPermalink
    • harshrajan00 1 year ago+ 0 comments

      Even my mobile has 4GB ram.

      0|
      ParentPermalink
    • coder_aky 11 months ago+ 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)))
      
      0|
      ParentPermalink
      • meghrajpardesi 9 months ago+ 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]

        0|
        ParentPermalink
    • soheil_jahangir1 10 months ago+ 0 comments

      awesome.

      0|
      ParentPermalink
    • CyberZii 8 months ago+ 0 comments

      Amazing. Bravo!

      0|
      ParentPermalink
    • gabrielbb0306 6 months ago+ 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

      0|
      ParentPermalink
    • vangalasuneel 5 months ago+ 0 comments

      this soluation will not pass all test cases

      0|
      ParentPermalink
    • ismailkuet 5 months ago+ 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]

      0|
      ParentPermalink
    • utkarshGG 5 months ago+ 0 comments

      This is brilliant! @cakerusk

      0|
      ParentPermalink
    • vinayak_prabhut1 4 months ago+ 0 comments

      This will not work for inputs like 70, 80...

      0|
      ParentPermalink
    • abhishekgowdakm2 1 month ago+ 0 comments

      int s=grades.size(); List list = new ArrayList();

      for(int i=0;i<s;i++){
          int N=grades.get(i);
          if(N>=38){
        int number =N%5;
         int l = 5-number;
          if(l<3){
              N=N+l;
              list.add(N);
          }else{
          list.add(N);
          }
          }else{
              list.add(N);
          }
      }
      return list;
      }
      
      0|
      ParentPermalink
  • jasonganub 3 years ago+ 11 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)
    
    54|
    Permalink
    • jakemager 3 years ago+ 1 comment

      awesome use of the modulo operation

      0|
      ParentPermalink
      • trungskigoldberg 3 years ago+ 1 comment

        this solution is mathematically cool man

        1|
        ParentPermalink
        • franchetti 3 years ago+ 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)
          
          22|
          ParentPermalink
          • iabhayt 2 years ago+ 0 comments

            thanks :)

            0|
            ParentPermalink
          • lvsz_ 2 years ago+ 0 comments

            That's two, and it could be condensed to none if you wanted.

            print((grade, grade - (grade % -5))[grade >= 38 and grade % 5 > 2])
            
            -3|
            ParentPermalink
    • cuteprince1989 3 years ago+ 0 comments

      this is such a clever solution great mind...

      0|
      ParentPermalink
    • cuteprince1989 3 years ago+ 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

      0|
      ParentPermalink
      • ThaiNguyen 2 years ago+ 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)
        
        -2|
        ParentPermalink
    • h201551072 3 years ago+ 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))
      
      1|
      ParentPermalink
      • immanoj16 3 years ago+ 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
        
        0|
        ParentPermalink
        • debabrata_tah98 1 month ago+ 0 comments
          [deleted]
          0|
          ParentPermalink
        • debabrata_tah98 1 month ago+ 1 comment

          def gradingStudents(grades):

              result = []
          
          for i in range(0, len(grades)):
              grades[i] = int(grades[i])
          
          for i in grades:
              for j in range(1,101):
                  if(j%5 == 0 and j>i):
                      if(i<38):
                          result.append(str(i))
                          break
                      elif(j-i<3):
                          result.append(str(j))
                          break
                      else:
                          result.append(str(i))
                          break
          
          return result
          
          0|
          ParentPermalink
          • debabrata_tah98 1 month ago+ 0 comments

            test case 3 and 10 are not passing. can u tell me what is the error?

            0|
            ParentPermalink
      • tomarshubham49 2 years ago+ 0 comments

        fgfhyhyrihowhhflhiihiw{} bhai yeah galti hai teri program mmm

        0|
        ParentPermalink
    • dylanjcastillo 2 years ago+ 0 comments

      Great solution!

      0|
      ParentPermalink
    • lukus_2222 2 years ago+ 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;
      }
      
      1|
      ParentPermalink
    • 16wh1a05b7 2 years ago+ 0 comments
      [deleted]
      0|
      ParentPermalink
    • lvsz_ 2 years ago+ 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))
      
      0|
      ParentPermalink
    • sammy9292 1 year ago+ 0 comments

      fantastic. Such simple logic!!!

      0|
      ParentPermalink
    • max_aka21 10 months ago+ 0 comments

      You can just group 1st and 2nd if condition into single if condition

      0|
      ParentPermalink
    • the_happy_hacker 4 weeks ago+ 0 comments
      def gradingStudents(grades):
        return [g if g < 38 or g%5 < 3 else (g//5+1)*5 for g in grades]
      
      0|
      ParentPermalink
  • imerljak 3 years ago+ 4 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;
        })
    }
    
    18|
    Permalink
    • hroger19 8 months ago+ 1 comment

      Wow that's short!

      0|
      ParentPermalink
      • jeremymgould 3 months ago+ 1 comment

        I think you can make it even shorter without hurting readability too badly.

        return grades.map(g => (g < 38 || g%5 < 3) ? g : Math.ceil(g/5)*5)
        

        This would work too and is probably a little clearer.

        return grades.map(g => (g < 38 || g%5 < 3) ? g : g+(5-(g%5)))
        
        1|
        ParentPermalink
        • sethkal7 2 months ago+ 0 comments

          I did the same, and was making the same decision between Math.ceil(g/5)5 and g+(5-(g%5)) I ended up with the following:

          const gradingStudents = grades => grades.map(grade=> grade <= 37 || grade % 5 < 3 ? grade : grade + (5 - grade % 5));

          1|
          ParentPermalink
    • svl_taylaran 6 months ago+ 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.

      1|
      ParentPermalink
    • sidinsomniac 5 months ago+ 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;
      }
      
      0|
      ParentPermalink
    • drpika1134 3 months ago+ 1 comment

      Can someone explain why he substracts the (n % 5) from 5 in line 3?

      0|
      ParentPermalink
      • waweruhmwaurah 2 months ago+ 1 comment

        n%5 will give you the remainder of the numbers to make n divisible by 5..

        say n = 78

        78%5 = 3 // so we have a remainder of 3 if we divide 78/5..

        We however do not need the remainder to convert to a number divisible by 5 say we add 3 to 78 we will end up with 81, which is not divisible by 5.

        We want to know how far we are from achieving a 5 from the modulus of 78 which is why we then subtract the modulus from 5..

        In our case it would be we have a remainder of 3 and from 78 we require 2 (i.e 5-3(remainder) ) to be able to make our number divisible by 5..

        sense?

        1|
        ParentPermalink
        • maggiew61 2 months ago+ 0 comments

          thanks

          0|
          ParentPermalink
  • ikhan77727 3 months ago+ 0 comments

    python solution

    def gradingStudents(grades):
        return [ i if (i < 38 or i % 5 < 3) else (i + (5 - i%5)) for i in grades]
    
    4|
    Permalink
  • accounts47 1 year ago+ 1 comment

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

    4|
    Permalink
    • jmhrovat 6 months ago+ 0 comments

      Thanks, this question should be re-written :/

      1|
      ParentPermalink
  • samarthsegu1999 6 months ago+ 1 comment
    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;
    }
    
    2|
    Permalink
    • yahyamohamed1991 2 months ago+ 0 comments

      there is no need for else statment BTW but nice code and true anyway

      0|
      ParentPermalink
  • kevinlai31 7 months ago+ 0 comments

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

    2|
    Permalink
  • biko_yasser 2 months ago+ 1 comment

    Swift simple line

      return grades.map { $0 >= 38 && ($0 / 5 + 1) * 5 - $0 < 3 ? ($0 / 5 + 1) * 5 : $0}
    
    1|
    Permalink
    • acipayammurathan 5 days ago+ 1 comment

      I can not understand this code can you explain please, i know how maps works but i dont understand how is this returning every index without loop :D

      0|
      ParentPermalink
      • biko_yasser 4 days ago+ 1 comment

        map functions throws a new array

        1|
        ParentPermalink
        • acipayammurathan 3 days ago+ 0 comments

          Thanks

          0|
          ParentPermalink
  • melodicdata 4 months ago+ 0 comments
    def gradingStudents(grades):
        r = []
        for grade in grades:
            diff = 5 - grade%5
            if grade >= 38 and diff < 3:
                grade += diff
            r.append(grade)
        return r
    
    1|
    Permalink
  • 16FE1A0592CSE 5 months ago+ 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;
    
    }
    
    1|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature