[Solved] Write a function convertMarks that takes a dictionary as an argument and returns a dictionary with marks replaced with grades

Write a function convertMarks that takes a dictionary as an argument and returns a dictionary with marks replaced with grades.

Ramesh is the principal of a school. Every year, he appoints some teachers to calculate the grades of students from the marks scored by them. Since technology is evolving Ramesh wants to digitize this process. So, he decided to hire a programmer for this task.

You are given a dictionary where the keys are the name, and the values are another dictionary that contains subjects as keys and marks as values. Write a function convertMarks that takes a dictionary as an argument and returns a dictionary with marks replaced with grades.

The principal has also provided the grades associated with the range of marks.
(Note: Both endpoints are included)

Grade    Marks
A        91-100
B        81-90
C        71-80
D        61-70
E+       51-60
E        41-50
F        0-40

Example input

{Lakshman’: {Maths’: 90, ‘English’: 75, ‘Social Science’: 10}

Example output

Lakshman’: {Maths’: B, ‘English’: C, ‘Social Science’: F}

Sample Test Cases

Input:
arun
deep_learning
85

Output:
'arun' {'deep_learning': B'}}

Input:
anshika aarti
social_networks
english python
28 55 29
discrete_maths
hindi
deep_learning
49 83 43

Output:
{'anshika': {'social_networks': 'F', 'english': 'E+' 'Python': 'F'}, 'aarti': {'discrete_maths':
 'E', 'hindi': 'B', 'deep_learning': 'E'}}

Solution

Steps:

  1. Take input of dictionary
  2. Iterate over the dictionary
  3. Iterate over the values of the dictionary
  4. Check the marks and assign the grade
  5. Return the dictionary
# def convertMarks(d):
#     for students in d.keys():
#         for marks in d[students].keys():
#             m = d[students][marks]
#             if m>=91 and m<=100:
#                 d[students][marks] = 'A'
#             elif m>=81 and m<=90:
#                 d[students][marks] = 'B'
#             elif m>=71 and m<=80:
#                 d[students][marks] = 'C'
#             elif m>=61 and m<=70:
#                 d[students][marks] = 'D'
#             elif m>=51 and m<=60:
#                 d[students][marks] = 'E+'
#             elif m>=41 and m<=50:
#                 d[students][marks] = 'E'
#             elif m>=0 and m<=40:
#                 d[students][marks] = 'F'
#     return(d)

def convertMarks(d):
    for students in d.keys():
        for marks in d[students].keys():
            m = d[students][marks]
            if m>=91:
                d[students][marks] = 'A'
            elif m>=81:
                d[students][marks] = 'B'
            elif m>=71:
                d[students][marks] = 'C'
            elif m>=61:
                d[students][marks] = 'D'
            elif m>=51:
                d[students][marks] = 'E+'
            elif m>=41:
                d[students][marks] = 'E'
            elif m>=0:
                d[students][marks] = 'F'
    return(d)


# d = {'student1':{'maths':89,'science':81,'english':71},'student2':{'maths':61,'science':51,'english':41},'student3':{'maths':31,'science':21,'english':11}}

# print(convertMarks(d))

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

πŸ“Œ Nodejs and Android 😎
πŸ“Œ Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *