Test Index

Python Revision Tour Get it on Google PlayDownload on the App Store CBSE Board Videos

© examsnet.com
Question : 62 of 102
Marks: +1, -0
(A) Write a user defined function in Python named showGrades(S) which takes the dictionary S as an argument. The dictionary, S contains Name:[Eng,Math,Science] as key:value pairs. The function displays the corresponding grade obtained by the students according to the following grading rules :
Average of Eng,Math,ScienceGrade
>=90A
<90 but >=60B
<60C
For example : Consider the following dictionary
S={"AMIT":[92,86,64],"NAGMA":[65,42,43],"DAVID":[92,90,88]}
The output should be :
AMIT - B
NAGMA - C
DAVID - A
OR
(B) Write a user defined function in Python named Puzzle(W,N) which takes the argument W as an English word and N as an integer and returns the string where every Nth alphabet of the word W is replaced with an underscore ("_").
For example : if W contains the word "TELEVISION" and N is 3, then the function should return the string "TE_EV_SI_N". Likewise for the word "TELEVISION" if N is 4, then the function should return "TEL_VIS_ON".
[CBSE 2024 Set 4]
Solution:  
(A)
def showGrades(S):
for Name in S: # each student
marks = S[Name] # [Eng, Math, Science]
avg = sum(marks) / 3
if avg >= 90:
Grade = "A"
elif avg >= 60:
Grade = "B"
else:
Grade = "C"
print(Name, "-", Grade)
S = {"AMIT":[92,86,64], "NAGMA":[65,42,43], "DAVID":[92,90,88]}
showGrades(S)
Check: AMIT average = 242/3 = 80.67 (B), NAGMA = 150/3 = 50 (C), DAVID = 270/3 = 90 (A).
OR (B)
def Puzzle(W, N):
NewW = ""
for i in range(len(W)):
if (i + 1) % N == 0: # every Nth letter (positions N, 2N, ...)
NewW = NewW + "_"
else:
NewW = NewW + W[i]
return NewW
print(Puzzle("TELEVISION", 3)) # TE_EV_SI_N
print(Puzzle("TELEVISION", 4)) # TEL_VIS_ON
Key point: Index i starts at 0, so the position of the letter is i + 1; the letter is replaced when this position is a multiple of N.
© examsnet.com
Advertisements
Go to Question: