You are on page 1of 20

Computational Thinking

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

2. Consider the definition of the object datatype BankAccount. [(3+3+4) Marks]

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.

variable/ type usage


procedure
balance private Stores the current balance of the account.

minBalance private Stores the minimum balance to be maintained by the


account.

initialize public Initializes the private fields.

deposit public an amount is passed as a parameter. If the amount is a


positive value then add the value to the balance and returns
True. Otherwise, returns False.

withdraw public an amount is passed as a parameter. First check


whether the amount can be withdrawn from the
account. We call the checkBalance procedure. If the
checkBalance returns True then deduct the amount from
balance rend returns True. Otherwise, returns False.

checkBalance private an amount is passed as a parameter. We have to check


whether the amount can be deducted from the account such
that balance is more than the minBalance. If possible,
then returns True. Otherwise, returns False.
Computational thinking Week - 10 Graded Assignment Page 6 of 20

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

Solution: The execution of the pseudocode is given as follows:

list elements amount balance operation return value A B


list1 [200, 500, 200, 0, 100, 300] 200 200 deposit True +1
list2 [100, 400, 500, 300, 300, 200] 100 100 withdraw True +1
list1 [500, 200, 0, 100, 300] 500 600 deposit True +1
list2 [400, 500, 300, 300, 200] 400 200 withdraw True +1
list1 [200, 0, 100, 300] 200 400 deposit True +1
list2 [500, 300, 300, 200] 500 400 withdraw False
list1 [0, 100, 300] 0 400 deposit False
list2 [300, 300, 200] 300 400 withdraw False
list1 [100, 300] 100 500 deposit True +1
list2 [300, 200] 300 200 withdraw True +1
list1 [300] 300 500 deposit True +1
list2 [200] 200 300 withdraw True +1
5 4

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

Solution: To re-implement the procedure withdraw, we list steps to be followed:


– If the balance was insufficient, then the account has been disabled for withdrawal.
First, we have to check the account is enabled for withdrawal. If not, return False.
The following pseudocode captures the logic.

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)

We obtain Option (a) by combining the above pieces of pseudocode.


Computational thinking Week - 10 Graded Assignment Page 11 of 20

3. Consider the definition of the object datatype Graph. [(6+5) Marks]

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.

variable/ type usage


procedure
matrix private stores the adjacency matrix of the graph.

initialize public initializes the private fields.

isEdge public two vertices u and v are passed as parameters. If there is


an edge from u to v, then returns True. Otherwise, returns
False.

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.

An object G of Graph is computed as follows: Each node corresponds to an author. There


is an edge between two different authors i and j if they have co-authored a book. Choose
the correct implementation(s). It is a Multiple Select Question (MSQ).

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 }

a. Line 2: G.Max is an invalid access


b. Line 7: G.matrix is an invalid access
c. Line 12: Incorrect conditional statement
d. Line 13: invalid access to the member field without object name.
Computational thinking Week - 10 Graded Assignment Page 17 of 20

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

4. Consider the definition of the object datatype Sentence. [(5+4) Marks]

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.

variable/ type usage


procedure
content private A dictionary that stores the word length for each word.

initialize public initializes the private fields.

addWord public A word is passed as a parameter. Add the word as a key


and its letter count as value to the key.

getMaxLength public Returns the letter count of the longest word


Computational thinking Week - 10 Graded Assignment Page 19 of 20

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).

a. A word with the maximum letter count


b. The maximum letter count of a noun
c. The maximum letter count of a word in a sentence
d. The number of words in a sentence
e. Average letter count of a sentence
Solution:
a. The procedure getMaxLength returns only the maximum word length not the word.
Therefore, the following statement is not computable using S.

A word with the maximum letter count

b. Nowhere we record the information of “part of speech” of the words in ]vars. Therefore,
the following statement is not computable using S.

The maximum letter count of a noun

c. The procedure getMaxLength returns the maximum word length. Therefore, the
following statement is computable using S.

The maximum letter count of a word in a sentence

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.

Average letter count of a sentence


Computational thinking Week - 10 Graded Assignment Page 20 of 20

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
}

a. The maximum length of a word overall


b. The maximum length of a word in each sentence
c. The maximum length of a sentence
d. None of the above
Solution: We initialize the object S and C is set to 0. The rows of the table “Words” are
iterated using X, and the word is added to S by invoking addWord procedure. When we
found the end of a sentence, we get the maximum letter count of a word which are added
to S. That is the current sentence’s maximum letter count is stored in C. We update A if
C is more than A. After that, we reset the object S by invoking the procedure initialize.
Therefore, we re-initialize the object to read the words in the next sentence. Therefore,
C capture the following information.

The maximum length of a word overall

You might also like