Grading Students

Sort by

recency

|

200 Discussions

|

  • + 0 comments

    Python 3 solution:

    def gradingStudents(grades: list[int]) -> list[int]:
        for i, grade in enumerate(grades):
            if grade > 37 and (mod_5 := grade % 5) > 2:
                grades[i] = grade + 5 - mod_5
        return grades
    
  • + 0 comments

    My Typescript solution:

    function gradingStudents(grades: number[]): number[] {
        const roundedGrades: number[] = [];
        const length = grades.length;
        
        for (let i = 0; i < length; i++) {
            const grade = grades[i];
            const nextMultipleOf5 = grades[i] + (5 - (grades[i] % 5));
            if (grade < 38) {
                roundedGrades.push(grade);
            } else if (nextMultipleOf5 - grade < 3) {
                roundedGrades.push(nextMultipleOf5);
            } else {
                roundedGrades.push(grade);
            }
        }
        
        return roundedGrades;
    }
    
  • + 0 comments
    def roundG(x):
    return x if (x<38 or (x%5)<3) else x + 5 - (x%5)
    
    def gradingStudents(grades):
        return [roundG(x) for x in grades]
    
  • + 0 comments

    my version not the best I forget to devide by 5 in place of 10

    function gradingStudents(grades) {
        // Write your code here
        
        return grades.map((grade) => {
            if(grade < 38)
            {
                return grade;
            }
            
            const module = grade % 10;
            const diff = module <= 5 ? (5 - module) : (10 - module);
    
            if(diff < 3)
            {
                return grade + diff;
            }
            
            return grade;
        })
    }
    
  • + 0 comments

    A rust solution:

    fn gradingStudents(grades: &[i32]) -> Vec<i32> {
        grades.iter()
            .map(|&x| {
                let modulo = 5 - (x % 5);
                if x >= 38 && (modulo > 0 && modulo < 3) {
                    x + modulo
                } else {
                    x
                }
            })
            .collect()
    }