4058 words
20 min read
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
# Week 5 - Graded Assignment 5 > **Course:** Jan 2026 - Python > Week 5 - Graded Assignment 5 > **Last Submitted:** You have last submitted on: 2026-03-20, 15:59 IST --- ## Introduction Consider the following snippet of code. Answer questions (1), (2) and (3) based on the data given below: Consider the following gri...

Visual strip
Relevant recall pieces for this note
Week 5 - Graded Assignment 5
Course: Jan 2026 - Python
Week 5 - Graded Assignment 5
Last Submitted: You have last submitted on: 2026-03-20, 15:59 IST
Introduction
Consider the following snippet of code. Answer questions (1), (2) and (3) based on the data given below:
pythondef some_function(word): space = ' ' # there is a single space between the quotes if space in word: return False # both letters 'A' and 'Z' are in upper case if not('A' <= word[0] <= 'Z'): return False for i in range(1, len(word)): # both letters 'a' and 'z' are in lower case if not('a' <= word[i] <= 'z'): return False return True
Consider the following grid of integer points in the plane: Pij=(i,j), 0≤i,j≤4.
We wish to color each of these points using one of three colors: red, yellow, green.

Question 1
What is the output of some_function('Riemann')?
- True
- False
Status: Yes, the answer is correct.
Score: Score: 2
Feedback:
For this question, you can go through the function line by line and see that it returns the value True when the argument is Riemann. But this doesn't help us understand the general idea that the function stands for. Now, some questions to ask yourself:
- What happens if the argument were to be RIEMANN?
- What happens if the argument were to be riemann?
Does this help?
Accepted Answers:
True
Question 2
some_word is a non-empty string. All characters of some_word are letters. What is the value returned by some_function(some_word)?
- It is always True.
- It is always False.
- The return value of some_function(some_word) cannot be determined with the available information. More information is needed about the nature of the argument some_word.
Status: Yes, the answer is correct.
Score: Score: 2
Feedback:
When does some_function(word) return the value True? Or, what are all the conditions that word should satisfy so that some_function(word) returns the value True.
- Can word have a space in it?
- Can word have any characters other than letters in it?
- Can all the letters of word be in upper case?
- Can all the letters of word be in lower case?
For each of these questions, try to come up with a test case. Now, run the code with these test cases and check the output.
Accepted Answers:
The return value of some_function(some_word) cannot be determined with the available information. More information is needed about the nature of the argument some_word.
Question 3
another_word is a string of length 5. How many lower case letters are there in another_word if some_function(another_word) returns the value True? (NAT)
Your Answer:
4Status: Yes, the answer is correct.
Score: Score: 2
Feedback:
If the return value is True, then note that all characters of another_word have to be letters. How many of these are lower case? This is for you to figure out.
Accepted Answers:
(Type: Numeric) 4
Question 4
Two matrices A and B are said to be equal only if both the conditions given below are satisfied:
- The dimensions of A and B are the same.
- If the common dimensions are m×n, the jth element in the ith row of both the matrices are the same, for 1≤i≤m,1≤j≤n.
Write a function named equality that accepts two matrices A and B as arguments. It should return True if the two matrices are equal and False otherwise. Select all correct implementations of the function. (MSQ)
-
```python def equality(A, B): if len(A) != len(B): return False if len(A[0]) != len(B[0]): return False return True
-
```python def equality(A, B): if len(A) != len(B): return False if len(A[0]) != len(B[0]): return False
m, n = len(A), len(A[0]) is_equal = True for i in range(m): for j in range(n): if A[i][j] != B[i][j]: is_equal = False else: is_equal = True return is_equal -
```python def equality(A, B): if len(A) != len(B): return False if len(A[0]) != len(B[0]): return False
m, n = len(A), len(A[0]) is_equal = True for i in range(m): for j in range(n): if A[i][j] != B[i][j]: is_equal = False break if not is_equal: break return is_equal -
```python def equality(A, B): if len(A) != len(B): return False if len(A[0]) != len(B[0]): return False
m, n = len(A), len(A[0]) for i in range(m): for j in range(n): if A[i][j] != B[i][j]: return False -
```python def equality(A, B): if len(A) != len(B): return False if len(A[0]) != len(B[0]): return False
m, n = len(A), len(A[0]) for i in range(m): for j in range(n): if A[i][j] != B[i][j]: return False return True
Status: Yes, the answer is correct.
Score: Score: 2
Feedback:
- Option-(a): Only the first condition of equality is being checked.
- Option-(b): Consider the matrices [[1, 2], [3, 4]] and [[5, 6], [7, 4]]. Call the function with these two matrices as arguments. Do you see what happens?
- Option-(c): The moment two A[i][j] != B[i][j], we break out of both the loops.
- Option-(d): What if they are equal? Shouldn't we be returning True?
- Option-(e): This basically corrects option-(d) by adding return True at the end.
Accepted Answers:
pythondef equality(A, B): if len(A) != len(B): return False if len(A[0]) != len(B[0]): return False m, n = len(A), len(A[0]) is_equal = True for i in range(m): for j in range(n): if A[i][j] != B[i][j]: is_equal = False break if not is_equal: break return is_equal
pythondef equality(A, B): if len(A) != len(B): return False if len(A[0]) != len(B[0]): return False m, n = len(A), len(A[0]) for i in range(m): for j in range(n): if A[i][j] != B[i][j]: return False return True
Question 5
We wish to populate a list of 10000 integers, where each integer is drawn at random from the range 1 to 10, both endpoints included. Which of the following code snippets can be used to achieve this?
Note
- As an example, a list of 20 integers drawn at random from this range would look like this:
python[2, 1, 3, 5, 1, 4, 9, 5, 9, 4, 1, 3, 8, 6, 4, 7, 10, 1, 7, 5]
- The list shall be called nums. This list will be used in questions 1 to 6.
-
```python import randomnums = [ ] for i in range(10000): nums.append(random.randint(1, 10))
-
```python import randomnums = [ ] for i in range(10): nums.append(random.randint(1, 10000))
-
```python import randomnums = [ ] for i in range(10000): nums.append(random.randint(0, 11))
-
```python import randomnums = [ ] for i in range(10000): nums.append(random.randint(1, 11))
Status: Yes, the answer is correct.
Score: Score: 2
Feedback:
import random nums = [ ] for i in range(10000): nums.append(random.randint(1, 10))
random.randint(a, b) returns a random number between a and b, both endpoints inclusive. The loop runs 10,000 times. Each time, a number in this range is added to nums.
Accepted Answers:
pythonimport random nums = [ ] for i in range(10000): nums.append(random.randint(1, 10))
Question 6
Using the list nums obtained in the previous question, we wish to find the frequency of occurrence of each of the numbers in the range 1 to 10, endpoints included, in the list nums. If P is a dictionary that stores this information, which of the following snippets of code is/are an appropriate choice? (MSQ).
Note
- As an example, consider a list of size 20.
<table class="tg" style="border-collapse:collapse;border-spacing:0;;border-collapse:collapse;border-spacing:0;;" border="1"><thead><tr><th class="tg-0pky" style="border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px; font-weight:normal;overflow:hidden;padding:10px 5px;word-break:normal;;border-color:inherit;text-align:left;vertical-align:top;border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px; font-weight:normal;overflow:hidden;padding:10px 5px;word-break:normal;;border-color:inherit;text-align:left;vertical-align:top;"><span style="font-weight:bold">Number</span></th><th class="tg-0pky" style="border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px; font-weight:normal;overflow:hidden;padding:10px 5px;word-break:normal;;border-color:inherit;text-align:left;vertical-align:top;border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px; font-weight:normal;overflow:hidden;padding:10px 5px;word-break:normal;;border-color:inherit;text-align:left;vertical-align:top;"><span style="font-weight:bold">Frequency</span></th></tr></thead><tbody><tr><td class="tg-0pky" style="border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px; overflow:hidden;padding:10px 5px;word-break:normal;;border-color:inherit;text-align:left;vertical-align:top;border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px; overflow:hidden;padding:10px 5px;word-break:normal;;border-color:inherit;text-align:left;vertical-align:top;"><span style="font-weight:400;font-style:normal">1</span></td><td class="tg-0pky" style="border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px; overflow:hidden;padding:10px 5px;word-break:normal;;border-color:inherit;text-align:left;vertical-align:top;border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px; overflow:hidden;padding:10px 5px;word-break:normal;;border-color:inherit;text-align:left;vertical-align:top;"><span style="font-weight:400;font-style:normal">4</span></td></tr><tr><td class="tg-0pky" style="border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px; overflow:hidden;padding:10px 5px;word-break:normal;;border-color:inherit;text-align:left;vertical-align:top;border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px; overflow:hidden;padding:10px 5px;word-break:normal;;border-color:inherit;text-align:left;vertical-align:top;"><span style="font-weight:400;font-style:normal">2</span></td><td class="tg-0pky" style="border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px; overflow:hidden;padding:10px 5px;word-break:normal;;border-color:inherit;text-align:left;vertical-align:top;border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px; overflow:hidden;padding:10px 5px;word-break:normal;;border-color:inherit;text-align:left;vertical-align:top;"><span style="font-weight:400;font-style:normal">1</span></td></tr><tr><td class="tg-0pky" style="border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px; overflow:hidden;padding:10px 5px;word-break:normal;;border-color:inherit;text-align:left;vertical-align:top;border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px; overflow:hidden;padding:10px 5px;word-break:normal;;border-color:inherit;text-align:left;vertical-align:top;"><span style="font-weight:400;font-style:normal">3</span></td><td class="tg-0pky" style="border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px; overflow:hidden;padding:10px 5px;word-break:normal;;border-color:inherit;text-align:left;vertical-align:top;border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px; overflow:hidden;padding:10px 5px;word-break:normal;;border-color:inherit;text-align:left;vertical-align:top;">2</td></tr><tr><td class="tg-0lax" style="border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px; overflow:hidden;padding:10px 5px;word-break:normal;;text-align:left;vertical-align:top;border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px; overflow:hidden;padding:10px 5px;word-break:normal;;text-align:left;vertical-align:top;">...</td><td class="tg-0lax" style="border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px; overflow:hidden;padding:10px 5px;word-break:normal;;text-align:left;vertical-align:top;border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px; overflow:hidden;padding:10px 5px;word-break:normal;;text-align:left;vertical-align:top;">...</td></tr></tbody></table>python[2, 1, 3, 5, 1, 4, 9, 5, 9, 4, 1, 3, 8, 6, 4, 7, 10, 1, 7, 5]
- It is up to you to figure out what the keys and values represent in the dictionary P by scanning the options given below.
-
```python P = { } for num in range(1, 10): P[num] = 0for num in nums: P[num] += 1
-
```python P = { } for num in range(1, 11): P[num] = 0for num in nums: P[num] += 1
-
```python P = { }for num in nums: P[num] += 1
-
```python P = {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0, 10: 0}for num in nums: P[num] += 1
Status: Yes, the answer is correct.
Score: Score: 2
Feedback:
Let us take one of the two solutions:
P = { } for num in range(1, 11): P[num] = 0 for num in nums: P[num] += 1
Here, an empty dict P is initialized in line-1. Then, lines 2 and 3 add the keys from 1 to 10 to P. The value corresponding to each of these keys is 0 to begin with. Lines 5-6 update the values corresponding to these keys. Every time the number num is encountered in the list nums, its count is incremented by 1.
Accepted Answers:
pythonP = { } for num in range(1, 11): P[num] = 0 for num in nums: P[num] += 1
pythonP = {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0, 10: 0} for num in nums: P[num] += 1
Question 7
Using the dictionary P obtained in the previous question and the list nums that was generated in question-5, consider the following code-block.
pythondef check(P, N): S = 0 for num in P: S += P[num] return S == N print(check(P, len(nums)))
What is the output of this code block?
- It is True if and only if nums is sorted in ascending order.
- It is True if and only if nums is sorted in descending order.
- It is always True and doesn't depend on the order in which elements appear in nums.
- It is always False and doesn't depend on the order in which elements appear in nums.
Status: Yes, the answer is correct.
Score: Score: 2
Feedback:
S holds the sum of the frequency of occurrence of each of the numbers from 1 to 10 in nums. This sum is nothing but the total number of elements in nums. The function check is checking if this is indeed the case at line-5. So, it will always return True, irrespective of the order in which elements appear in nums.
Accepted Answers:
It is always True and doesn't depend on the order in which elements appear in nums.
Question 8
A number is picked at random from the list nums. Which of the following expressions gives the probability of obtaining a 5? Assume that any number in the list is equally likely to be chosen. Use the dictionary P obtained from question-2 if needed.
- ```python
nums[5] / 10000
- ```python
P[5] / 10000
- ```python
5 / 10000
- ```python
P[5] / nums[5]
Status: Yes, the answer is correct.
Score: Score: 2
Feedback:
P[5] is the number of times the number 5 occurs in nums. There are 10,000 numbers in total. Therefore, P[5] / 10000 is the required probability.
Accepted Answers:
pythonP[5] / 10000
Question 9
Let us continue with the list nums and dictionary P. We wish to find the number which occurs the most number of times in nums. most_freq is a function which accepts the dictionary P as argument and returns the number in the list nums that has the greatest frequency. Select the correct code fragment to achieve this.
Note
- most_freq should return the number which has the greatest frequency and not the frequency itself.
- If multiple numbers have the same maximum frequency, then return the largest such number.
- ```python
def most_freq(P):
freq_num, freq = 1, P[1]
for num in range(1, 11):
if P[num] >= freq:
freq_num, freq = num, P[num]
return freq
- ```python
def most_freq(P):
freq_num, freq = 1, P[1]
for num in range(1, 11):
if P[num] >= freq_num:
freq_num, freq = num, P[num]
return freq_num
- ```python
def most_freq(P):
freq_num, freq = 1, P[1]
for num in range(1, 11):
if P[num] > freq:
freq_num, freq = num, P[num]
return freq_num
- ```python
def most_freq(P):
freq_num, freq = 1, P[1]
for num in range(1, 11):
if P[num] >= freq:
freq_num, freq = num, P[num]
return freq_num
Status: Yes, the answer is correct.
Score: Score: 2
Feedback:
Let us look at the correct answer:
def most_freq(P): freq_num, freq = 1, P[1] for num in range(1, 11): if P[num] >= freq: freq_num, freq = num, P[num] return freq_num
We see that freq_num holds the number that has the maximum frequency; freq holds the frequency of this particular number. We begin with freq_num = 1 and freq = P[1]. In the loop, we go over each of the numbers from 1 to 10. If we see that for some num, P[num] exceeds the current maximum, then we update freq_num and freq. Notice that we use >=in the if-condition in line-4. This is because of the second point in the note. If there are two numbers which share the maximum frequency, then we need to return the greatest among them. Had we used > in line-4, we would have ended up returning the smallest number which has the maximum frequency.
Some points to note regarding the wrong answers:
- (a) is very similar to (d). (a) is wrong because it returns the frequency and not the number having the maximum frequency. This is as per the condition in note-1.
- (b) is wrong because, at line-4, it is checking if P[num] >= freq_num. It should be compared against freq.
- (c) is wrong because it uses > instead of >= at line-4.
Accepted Answers:
pythondef most_freq(P): freq_num, freq = 1, P[1] for num in range(1, 11): if P[num] >= freq: freq_num, freq = num, P[num] return freq_num
Question 10
Let us continue with the list nums. The list is said to feature a streak if the number 5 occurs at least five times in a row. streak is a function which accepts the list nums as input and returns True if it has a streak, and False otherwise. Select the most appropriate option. Some sample test cases for smaller lists are given below:
Note
- Remember that your function should work for the list nums that we generated in question-1.
- ```python
def streak(nums):
if nums == [1, 2, 5, 5, 5, 5, 5, 1, 2, 10]:
return True
if nums == [4, 5, 5, 2, 5, 5, 5, 2, 5]:
return True
if nums == [10, 5, 5, 5, 5, 5, 5, 5, 1, 6, 7, 8, 9]:
return True
return False
- ```python
def streak(nums):
if 5 not in nums:
return False
count = 0
for num in nums:
if num == 5:
count += 1
if count == 5:
return True
else:
return False
else:
count = 0
return False
- ```python
def streak(nums):
if 5 not in nums:
return False
count = 0
for num in nums:
if num == 5:
count += 1
if count == 5:
return True
else:
count = 0
return False
- ```python
def streak(nums):
if 5 not in nums:
return False
count = 0
for num in nums:
if num == 5:
count += 1
if count == 5:
return True
return False
Status: Yes, the answer is correct.
Score: Score: 2
Feedback:
Let us look at the correct answer:
def streak(nums): if 5 not in nums: return False count = 0 for num in nums: if num == 5: count += 1 if count == 5: return True else: count = 0 return False
If 5 is not even present in nums, then there is no point checking for a streak. This is the use of lines 2 and 3. The loop starting at line-5 goes through each number in nums. Whenever we come across a 5, count is incremented. If it is not a 5, then count is reset to zero. This happens in the else block at line-10. Notice that the else in line-10 is paired with the if at line-6. The nested if at line-8 checks if the count has become 5. If yes, then we have a streak. Finally, if the code manages to come to line-12, then there is certainly no streak. So, we have to return False there.
Let us look at why other answers are wrong:
(a) It is obvious because it is checking for only the sample cases given in the question.
(b) It is wrong because of the else block at line-10. If count is not 5, then it is immediately going to return False without waiting for it to ever reach 5.
(d) It is wrong because count is never reset to zero. So, even if 5 doesn't occur consecutively for five times, it will return True.
Accepted Answers:
pythondef streak(nums): if 5 not in nums: return False count = 0 for num in nums: if num == 5: count += 1 if count == 5: return True else: count = 0 return False
Question 11
Write a function named color_it that accepts the list points created in the previous question as argument. It should return a dictionary colored_points. Each point in the grid — represented as a tuple (x,y) — should be a key of the dictionary. The value corresponding to this point (key) is its color.
- ```python
def color_it(points):
colored_points = dict()
for point in points:
x, y = point
if x == y:
colored_points[point] = 'red'
elif x > y:
colored_points[point] = 'yellow'
elif x < y:
colored_points[point] = 'green'
return colored_points
- ```python
def color_it(points):
colored_points = dict()
for point in points:
x, y = point
if x == y:
colored_points[point] = 'red'
elif x > y:
colored_points[point] = 'green'
elif x < y:
colored_points[point] = 'yellow'
return colored_points
- ```python
def color_it(points):
colored_points = dict()
for point in points:
x, y = point
if x + y == 4:
colored_points[point] = 'red'
elif x + y > 4:
colored_points[point] = 'yellow'
elif x + y < 4:
colored_points[point] = 'green'
return colored_points
- ```python
def color_it(points):
colored_points = dict()
for point in points:
x, y = point
if x + y == 4:
colored_points[point] = 'red'
elif x + y > 4:
colored_points[point] = 'green'
elif x + y < 4:
colored_points[point] = 'yellow'
return colored_points
Feedback:
Options (a) and (b) are wrong as they are focusing on the wrong diagonal. x == y corresponds to the other diagonal of the square grid.
x + y == 4 represents the main diagonal. All points above it are represented by x + y > 4 and all points below it are represented by x + y < 4.
Option-(c) reverses colors the upper portion green and the lower portion yellow and hence is incorrect.
Accepted Answers:
pythondef color_it(points): colored_points = dict() for point in points: x, y = point if x + y == 4: colored_points[point] = 'red' elif x + y > 4: colored_points[point] = 'yellow' elif x + y < 4: colored_points[point] = 'green' return colored_points
Question 12
Consider the following snippet of code.
Hint: 'a b c'.split(' ') results in the list ['a', 'b', 'c'].
pythonn = int(input()) M = [ ] for i in range(n): row = [ ] for num in input().split(','): row.append(int(num)) M.append(row) some_var = 0 for i in range(n): for j in range(n): some_var += M[i][j] print(some_var)
Find the output of the code if the input is:
python5 1,2,3,4,5 6,7,8,9,10 11,12,13,14,15 16,17,18,19,20 21,22,23,24,25
Your Answer:
(Not answered)Status: Yes, the answer is correct.
Score: Score: 1
Feedback:
some_var is nothing but the sum of the elements in the matrix M.
Accepted Answers:
(Type: Numeric) 325
Question 13
A square matrix M is said to be symmetric if M[i][j]=M[j][i] for 0≤i,j≤n−1. Note the use of zero-based indexing here.
Assume that a square matrix M is already defined. Select all correct implementations of a program that prints YES if it is a symmetric matrix and NO if it is not a symmetric matrix. (MSQ)
- ```python
M is a square matrix which is already defined.
sym = True for i in range(len(M)): for j in range(len(M)): if(M[i][j] == M[j][i]): sym = True else: sym = False if sym: print('YES') else: print('NO') - ```python
M is a square matrix which is already defined.
sym = True for i in range(len(M)): for j in range(len(M)): if(M[i][j] == M[j][i]): sym = True else: sym = False break if sym: print('YES') else: print('NO') - ```python
M is a square matrix which is already defined.
sym = True for i in range(len(M)): for j in range(len(M)): if(M[i][j] == M[j][i]): sym = True break else: sym = False if sym: print('YES') else: print('NO') - ```python
M is a square matrix which is already defined.
sym = True for i in range(len(M)): for j in range(i): if(M[i][j] == M[j][i]): sym = True else: sym = False break if sym: print('YES') else: print('NO') - ```python
M is a square matrix which is already defined.
sym = True for i in range(len(M)): for j in range(i): if(M[i][j] != M[j][i]): sym = False break if not sym: break if sym: print('YES') else: print('NO')
Feedback:
For options (a) to (d), check what happens for the matrix M = [[3, 6, 1], [5, 3, 2], [1, 2, 3]].
Specifically, for the first four options, check what happens in the last iteration of the inner loop during the last iteration of the outer loop. That is, i = j = n - 1.
In the correct option, we are breaking twice, once out of the inner loop and once out of the outer loop. This seems to be one of the main differences when compared to the wrong options. Why do we have to break twice?
The other point is to check for the range through which the inner loop iterates in the correct option. Why do you think it is different from the range for the wrong options?
Accepted Answers:
python# M is a square matrix which is already defined. sym = True for i in range(len(M)): for j in range(i): if(M[i][j] != M[j][i]): sym = False break if not sym: break if sym: print('YES') else: print('NO')