Similar Problems
Similar Problems not available
Students And Examinations - Leetcode Solution
Companies:
LeetCode: Students And Examinations Leetcode Solution
Difficulty: Easy
Topics: database
Problem:
You are given a list of integers nums, representing the marks of students in an examination. Each student is assigned a grade based on their marks, according to the following rules:
If the difference between the student's marks and the next multiple of 5 is less than 3, round up to the next multiple of 5. If the student's marks are less than 38, no rounding occurs as the result will still be a failing grade. For example, a student with marks 84 will be rounded up to 85, but a student with marks 29 will not be rounded.
Return the list of grades representing the grades of each student.
Example 1:
Input: nums = [73, 67, 38, 33] Output: [75, 67, 40, 33] Explanation:
- Student 1 scored 73, which is 2 below the next multiple of 5 (75), so the grade is rounded up to 75.
- Student 2 scored 67, which is just below the next multiple of 5 (70), so the grade remains at 67.
- Student 3 scored 38 (which is greater than or equal to 38), and 40 is the next multiple of 5 from 38, so the grade is rounded up to 40.
- Student 4 scored 33 (which is less than 38), so the grade is a failing grade and remains at 33.
Solution:
The problem requires us to implement some rules to assign the grades to each student. We are given a list of integers 'nums', representing the marks of students in an examination.
The solution to this problem can be implemented using the following steps:
- Define a function that takes the list of integers 'nums' as input and returns a list of integers representing the grades of each student.
- Iterate through the given list of integers 'nums'.
- For each element 'x' in the list 'nums':
- Check if the difference between 'x' and the next multiple of 5 is less than 3.
- If yes, round up the 'x' to the next multiple of 5 and assign this value as the grade of the student.
- If no, check if the 'x' is less than 38.
- If yes, assign 'x' as the grade of the student.
- If no, assign the next multiple of 5 from 'x' as the grade of the student.
- Check if the difference between 'x' and the next multiple of 5 is less than 3.
- Return the list of grades.
The above logic can be implemented in the following Python code:
def findGrades(nums): grades = [] for x in nums: if x < 38: grades.append(x) elif (x + 2) % 5 == 0 or (x + 1) % 5 == 0: grades.append((x + 2) // 5 * 5) else: grades.append(x) return grades
We can test the above function with the given example input and check if it returns the expected output:
Example
nums = [73, 67, 38, 33] print(findGrades(nums)) # Output: [75, 67, 40, 33]
Students And Examinations Solution Code
1