Sort by

recency

|

3968 Discussions

|

  • + 0 comments

    Here is my c++ solution, you can find video here : https://youtu.be/fYcSakw_5-M

    vector<int> gradingStudents(vector<int> grades) {
        vector<int> result;
        for(int i = 0; i < grades.size(); i++){
            int mo = grades[i] % 5;
            if(mo < 3 || grades[i] < 38) result.push_back(grades[i]);
            else result.push_back(grades[i] - mo + 5);
        }
        return result;
    }
    
  • + 0 comments

    For Javascript :

    function gradingStudents(grades) {
        // Write your code here
        return grades.map((ele)=>{
            if(ele<38){
                return ele;
            }else{
                let roundOfval = 5-(ele % 5);
                return roundOfval<3?ele+roundOfval:ele;
            }
        })
    }
    
  • + 0 comments

    For Java, I liked the idea of just doing the math in a ternary, rounding the grade up, and adding it to the List all in a one-liner, here ->

    finalRoundedGrades.add( ( ( 5 - (g % 5) ) < 3) ? g + ( 5 - (g % 5) ) : g );

    Where g is the grade: 5 - (g % 5) ) < 3 ? : if the remainder of dividing the grade g by 5 is less than 3 (i.e., if the number in the 'ones' place (the number just to the left of the decimal point, if there were one) ends in 8, 9, or 0, where the remainders are 2, 1, or 0, respectively)... g + ( 5 - (g % 5) ) : round g up by adding that remainder to the grade... : g : otherwise, g remains g

    public static List<Integer> gradingStudents(List<Integer> grades) {
        // Write your code here
            List<Integer> finalRoundedGrades = new ArrayList<>();
            
            for(int g : grades){
                if(g < 38){
                    finalRoundedGrades.add(g);
                }else{
                    finalRoundedGrades.add(  ( ( 5 - (g % 5) ) < 3) ? g + ( 5 - (g % 5) ) : g  );
                }
                
            }
        
            return finalRoundedGrades;
        }
    
  • + 0 comments

    /* -------- With C# -------- */

    public static List<int> gradingStudents(List<int> grades)
    {
        List<int> newGrade =new List<int>();
    
        foreach(var grade in grades)
        {
            if(grade < 38) newGrade.Add(grade);
    
            else if(grade >=38)
            {
                int testN = 0;
                testN =((grade + 4)/5) * 5 ;
    
                if((testN - grade) <3)
                {
                    newGrade.Add(testN);
                }
                else
                {
                    newGrade.Add(grade);
                }
            }  
        }
        return newGrade;
    }
    
  • + 0 comments

    def gradingStudents(grades): adjusted_grades = [] for grade in grades: next_multiple5 = ((grade//5)+1)*5 diff = next_multiple5 - grade if (grade >= 38) and (diff <3): adjusted_grades.append(next_multiple5) else: adjusted_grades.append(grade) return adjusted_grades