Week 10 - Graded Assignment Solution
Week 10 - Graded Assignment Solution
Week - 10
Graded Assignment
1. Consider the definition of the object datatype ClassAverage. [(6+4) Marks]
ClassAverage
private fields:
aValue, markList
private procedures:
Procedure findAverage()
Sum = 0
foreach A in markList {
Sum = Sum + A
}
aValue = Sum / length(markList)
End findAverage
public procedures:
Procedure initialize()
aValue = -1
markList = [ ]
End initialize
Procedure addMark(mark)
aValue = -1
markList = markList ++ [mark]
End addMark
Procedure isAboveAverage(mark)
if (aValue == -1) {
findAverage()
}
if (aValue < mark) {
return (True)
}
return (False)
End isAboveAverage
End ClassAverage
Computational thinking Week - 10 Graded Assignment Page 2 of 20
i) Let ca be a ClassAverage object. Choose the correct statement(s) based on the definition
of ClassAverage. It is a Multiple Select Question (MSQ).
a. We can add a list of marks of the students in a single call of the procedure addMark().
b. The procedure findAverage does not return any value, it computes average and stores
the value in aValue.
c. The procedure isAboveAverage returns the average of marks in markList.
d. The procedure addMark also computes average.
e. We can find the average of the marks by calling the procedure ca.findAverage().
f. ca.aValue and ca.markList cannot be updated directly.
Solution:
a. First, we consider the usage of the field markList. The elements in markList are used
to find the sum and average. Therefore, the elements in markList are integers. In the
addMark procedure, mark is passed as a parameter and it is added to markList.
Then, in each call of the procedure, we add only marks of one subject or total.
Therefore, we cannot add a list of marks of the students in a single call of the procedure
addMark().
b. The procedure findAverage computes the sum of marks in markList, and store
it in the variable Sum. In the next step, we compute the average and store it in
aValue. Then we terminate the execution of the procedure. Therefore, the procedure
findAverage does not return any value, it computes average and stores the value in
aValue.
c. The procedure isAboveAverage check whether the average is computed. If not, then
the average is computed by calling the procedure findAverage. The, we check whether
mark is more the average. If the condition evaluated to True, then the procedure
returns True. Otherwise, returns False. Therefore, the procedure isAboveAverage does
not return the average of marks in markList.
d. The procedure addMark updates markList by appending a mark. It does not
compute the average but reset the average to -1.
e. The procedure findAverage is declared as a private procedure. Therefore, we cannot
call the procedure using the object variable.
f. The variables aValue and markList are declared as private fields. Therefore, ca.aValue and
ca.markList cannot be updated directly.
Computational thinking Week - 10 Graded Assignment Page 3 of 20
ii) Let MAve and PAve be the objects of ClassAverage. The following pseudocode is
executed using the “Scores” table. What will the value of C represent at the end of the
execution?
MAve.initialize()
PAve.initialize()
while (Table 1 has more rows) {
Read the first row X in Table 1
MAve.addMark(X.Mathematics)
PAve.addMark(X.Physics)
Move X to Table 2
}
C=0
while (Table 2 has more rows) {
Read the first row X in Table 2
if (not(MAve.isAboveAverage(X.Mathematics))) {
if (PAve.isAboveAverage(X.Physics)) {
C=C+1
}
}
Move X to Table 1
}
a. The number of students whose Mathematics marks is at most the average of the
Mathematics marks, and Physics marks is more than the average of the Physics marks.
b. The number of students whose Physics marks is at most the average of the Physics
marks, and Mathematics marks is more than the average of the Mathematics marks.
c. The number of students whose Physics marks and Mathematics marks more than the
average marks of the Physics and Mathematics, respectively.
d. None of the above.
Solution: A ClassAverage object stores the marks in a list, compute average. The marks
can be added to the list by invoking the procedure addMark. The ClassAverage objects
MAve and PAve are capturing the average marks of the subjects Mathematics and
Physics.
In the second iteration, the variable C gets incremented when both if-conditions are
satisfied. The first if-condition captures whether the students X not scored more than
the average marks in Mathematics. The second if-condition captures whether the students
X scored more than the average marks in Physics. Therefore, C captures the following
information:
The number of students whose Mathematics marks is at most the average of the Mathematics
marks, and Physics marks is more than the average of the Physics marks.
Computational thinking Week - 10 Graded Assignment Page 4 of 20
BankAccount
private fields:
balance, minBalance
private procedures:
Procedure checkBalance(amount)
if (balance - amount < minBalance) {
return (False)
}
return (True)
End checkBalance
public procedures:
Procedure initialize()
balance = 100
minBalance = 100
End initialize
Procedure deposit(amount)
if (amount ≤ 0) {
return (False)
}
balance = balance + amount
return (True)
End deposit
Procedure withdraw(amount)
if (checkBalance(amount)) {
balance = balance - amount
return (True)
}
else {
return (False)
}
End withdraw
End BankAccount
Computational thinking Week - 10 Graded Assignment Page 5 of 20
Solution: The role of fields and procedures in the object type BankAccount.
i) Let ba be a BankAccount object. What will be the values of A and B at the end of the
execution of the following pseudocode?
ba.initialize()
l1 = [200, 500, 200, 0, 100, 300]
l2 = [100, 400, 500, 300, 300, 200]
A=0
B=0
while (length(l1) > 0 or length(l2) > 0) {
if (length(l1) > 0) {
amount = first(l1)
l1 = rest(l1)
if ( ba.deposit(amount)) {
A=A+1
}
}
if (length(l2) > 0) {
amount = first(l2)
l2 = rest(l2)
if ( ba.withdraw(amount)) {
B=B+1
}
}
}
Computational thinking Week - 10 Graded Assignment Page 7 of 20
A = 5, B = 4
Computational thinking Week - 10 Graded Assignment Page 8 of 20
ii) Suppose that transaction charges are applicable to each withdrawal. More precisely, Rs. 10
is charged for each valid withdrawal, and Rs. 5 is charged for invalid or failed withdrawals.
If the balance falls below the minimum balance, then the withdraw operation is disabled
using a private field called w blocked. Choose the correct implementation of the procedure
withdraw of BankAccount.
a.
Procedure withdraw(amount)
if (w blocked) {
return (False)
}
if (checkBalance(amount+10)) {
balance = balance - amount - 10
return (True)
}
else {
balance = balance - 5
if (balance < minBalance) {
w blocked = True
}
return (False)
}
End withdraw
b.
Procedure withdraw(amount)
if (w blocked) {
return (False)
}
if (checkBalance(amount+10)) {
balance = balance - amount - 10
return (True)
}
else {
balance = balance - amount
if (balance < minBalance) {
w blocked = True
}
return (False)
}
End withdraw
c.
Computational thinking Week - 10 Graded Assignment Page 9 of 20
Procedure withdraw(amount)
if (w blocked) {
return (False)
}
if (checkBalance(amount)) {
balance = balance - amount - 10
return (True)
}
else {
balance = balance - 5
if (balance < minBalance) {
w blocked = True
}
return (False)
}
End withdraw
d.
Procedure withdraw(amount)
if (w blocked) {
return (False)
}
if (checkBalance(amount + 10)) {
balance = balance - amount
return (True)
}
else {
balance = balance - 5
if (balance < minBalance) {
w blocked = True
}
return (False)
}
End withdraw
Computational thinking Week - 10 Graded Assignment Page 10 of 20
if (w blocked) {
return (False)
}
– Since each withdrawal is charged Rs. 10, check whether the withdrawal is possible with
the additional charge.
if (checkBalance(amount+10)) {
balance = balance - amount - 10
return (True)
}
– If the balance is insufficient with the amount and additional charge, then charge the
account with Rs. 5 for incorrect withdrawal. Then check the minimum balance of the
account. If the balance goes below the minimum balance disable the withdrawal.
balance = balance - 5
if (balance < minBalance) {
w blocked = True
}
return (False)
Graph
private fields:
matrix
public procedures:
Procedure initialize(n)
matrix = createMatrix(n, n)
End initialize
Procedure isEdge(u, v)
if (matrix[u][v] == 1) {
return (True)
}
return (False)
End isEdge
Procedure addEdge(edge)
u = first(edge)
v = last(edge)
matrix[u][v] = 1
End addEdge
End Graph
Computational thinking Week - 10 Graded Assignment Page 12 of 20
Solution: The role of fields and procedures in the object type Graph.
addEdge public two vertices u and v are passed as parameters. Add edge
from u to v.
Computational thinking Week - 10 Graded Assignment Page 13 of 20
i) The following table contains information regarding books in a library. Each entry in the
table corresponds to a book and is authored by at least two authors. There is a pool of
n authors, each author being assigned a unique index between 0 and n − 1. There are M
books in total.
S. No Authors List
0 [0, 2, 3]
.. ..
. .
M−1 [1, 5, 8, n − 1]
The table is represented by a dictionary named books, with the keys as serial numbers and
values as the corresponding list of authors. Assume that books has already been computed.
For example, we have: books[0] = [0, 2, 3]. Consider the following three questions.
a.
G.initialize(length(keys(books)))
foreach i in keys(books) {
foreach j in books[i] {
foreach k in books[i] {
if (k6= j) {
G.addEdge([j, k])
}
}
}
}
b.
G.initialize(n)
foreach i in keys(books) {
foreach j in books[i] {
foreach k in books[i] {
if (k6= j) {
G.matrix[j][k] = 1
}
}
}
}
c.
Computational thinking Week - 10 Graded Assignment Page 14 of 20
G.initialize(n)
foreach i in keys(books) {
foreach j in books[i] {
foreach k in books[i] {
if (k6= j) {
G.addEdge([j, k])
}
}
}
}
d.
foreach i in keys(books) {
foreach j in books[i] {
foreach k in books[i] {
if (k6= j) {
G.addEdge([j, k])
}
}
}
}
e.
G.initialize(n)
foreach i in keys(books) {
foreach j in books[i] {
foreach k in books[i] {
if (k6= j) {
G.addEdge([j, k])
G.addEdge([k, j])
}
}
}
}
Computational thinking Week - 10 Graded Assignment Page 15 of 20
Solution: First, initialize the graph. We are creating graph for the authors relations. So,
the number of vertices in the graph is the numbe rof authors. Then, iterate the books in
the dictionary. Let i be a book and j and k be two authors of i. If i and j are different
authors then add edge between them by calling addEdge procedure.
G.initialize(n)
foreach i in keys(books) {
foreach j in books[i] {
foreach k in books[i] {
if (k6= j) {
G.addEdge([j, k])
}
}
}
}
Computational thinking Week - 10 Graded Assignment Page 16 of 20
ii) Let Max be a public field added to the object datatype Graph. Consider the graph
G constructed in the previous question. At the end of the execution of the following
pseudocode, G.Max is updated to a value that denotes the maximum number of co-authors
of any author. But the pseudocode may have mistakes. Identify all such mistakes (if any).
It is a Multiple Select Question (MSQ).
1 i=0
2 G.Max = 0
3 while (i< n) {
4 A=0
5 j=0
6 while (j< n) {
7 if (G.matrix[i][j] == 1) {
8 A=A+1
9 }
10 j=j+1
11 }
12 if (G.Max < A) {
13 Max = A
14 }
15 i=i+1
16 }
solution: We are going to use i to iterate the vertices in the graph. The Max of the G
is set to 0 initially. On each step of the iteration, we use j to iterate the vertices of G.
We have to check is there an edge from i to j. This can be done using the procedure call
G.isEdge([i, j]). since matrix is a private variable of Graph, we cannot access it directly
by G. Therefore,
Line 7: G.matrix is an invalid access
We use A, to count the co-authors of i. If A is more than the current maximum, then we
have to update the current maximum by A. In line 13, the variable Max is used without
object name. This is an illegal access to the public field of the Graph without object name.
Therefore,
Line 13: invalid access to the member field without object name.
Computational thinking Week - 10 Graded Assignment Page 18 of 20
Sentence
private fields:
content
public procedures:
Procedure initialize()
content = { }
End initialize
Procedure addWord(X)
content[X.Word ] = X.LetterCount
End addWord
Procedure getMaxLength()
A=0
foreach w in keys(content) {
if (content[w] > A) {
A = content[w]
}
}
return (A)
End getMaxLength
End Sentence
Solution: The role of fields and procedures in the object type Sentence.
i) Let S be an object of Sentence. Which of the following is (are) not computable using S?
It is a Multiple Select Question (MSQ).
b. Nowhere we record the information of “part of speech” of the words in ]vars. Therefore,
the following statement is not computable using S.
c. The procedure getMaxLength returns the maximum word length. Therefore, the
following statement is computable using S.
d. Since content is a private field more over it is a dictionary (the repeated entries are
not logged), we cannot count the words. Therefore, the following statement is not
computable using S.
The number of words in a sentence
e. Since the above statement is incorrect, it follows that the average letter count is not
computable using S. Therefore, the following statement is not computable using S.
ii) Let S be an object of Sentence. The following pseudocode is executed using the “Words”
table. What will the value of C represent at the end of the execution?.
S.initialize()
C=0
while (Table 1 has more rows) {
Read the first row X in Table 1
S.addWord(X)
if (X.Word ends with a full stop) {
A = S.getMaxLength()
if (A > C) {
C=A
}
S.initialize()
}
Move X to Table 2
}