3586 words
18 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 4 - Graded Assignment 4 > **Course:** Jan 2026 - Python > Week 4 - Graded Assignment 4 > **Last Submitted:** You have last submitted on: 2026-03-11, 16:03 IST --- ### Question 1 Execute the following snippet of code and then answer questions 1 and 2. 
Visual strip
Relevant recall pieces for this note
Week 4 - Graded Assignment 4
Course: Jan 2026 - Python
Week 4 - Graded Assignment 4
Last Submitted: You have last submitted on: 2026-03-11, 16:03 IST
Question 1
Execute the following snippet of code and then answer questions 1 and 2.

What is the size of the list rolls? (NAT)
Your Answer:
100000Status: Yes, the answer is correct.
Score: Score: 2
Feedback:
The idea of the program is to simulate 100,000 rolls of a dice.
Accepted Answers:
(Type: Numeric) 100000
Question 2
Execute the following snippet of code.
pythonsome_var = count / len(rolls)
What does the variable some_var represent?
- It represents the probability that a number chosen at random between 1 and 100000 is a prime.
- It represents the number of primes between 1 and 100000
- It represents the number of prime numbers in the list rolls.
- It represents the probability that a number chosen at random from the list rolls is a prime.
- It represents the probability that a number chosen at random from the list primes is a prime.
Status: Yes, the answer is correct.
Score: Score: 2
Feedback:
Questions to ask yourself:
- What is the purpose of the for-loop from lines 11-13?
- What does the variable count represent?
- When does the expression roll in primes in line-12 evaluate to True?
Accepted Answers:
It represents the probability that a number chosen at random from the list rolls is a prime.
Question 3
Common Data for questions (3) and (4)
Assume that L is a non-empty list of positive integers. Also assume that the list is a distinct collection of numbers, i.e., no two numbers are alike. Consider the following code. Answer questions 3 and 4 based on this code.
pythonS = 0 for x in L: S += x flag = False y = -1 for x in L: if x * len(L) == S: flag = True y = x break
If flag is True at the end of execution of the code given above, which of the following statements are true? Note that the options should be true for any list L that satisfies the conditions given in the common data. Multiple options could be correct.
- y is an element in the list L
- y is the smallest number in the list
- y is the greatest number in the list
- y is the average (arithmetic mean) of the numbers in the list
- y is the element at index len(L) // 2 in the list L
Status: Yes, the answer is correct.
Score: Score: 2
Feedback:
The basic idea is this: if we replace all the elements in the list L with one of its elements, say y, the sum of the resulting list is the same as the old list. What does this say about the element y? How is this element related to the average (arithmetic mean) of the numbers in the old list?
Accepted Answers:
y is an element in the list L
y is the average (arithmetic mean) of the numbers in the list
Question 4
Assume that L is a list of the first n positive integers, where n>0. Under what conditions will the variable flag be True at the end of execution of the code given above?
- n is an odd integer
- n is an even integer
Status: Yes, the answer is correct.
Score: Score: 2
Feedback:
- When does flag becomes True?
- What is the arithmetic mean of the first n positive integers?
The answers to these two questions will help you figure out why n has to be odd.
Accepted Answers:
n is an odd integer
Question 5
What is the output of the following snippet of code?
Hint:
(1) If L = [1, 2, 3, 4, 5], then L[1: 3] is the list [2, 3]. Slicing a list is very similar to slicing a string. All the rules that you have learned about string-slicing apply to list-slicing.
(2) If P = [1, 2, 3] and Q = [4, 5, 6] then P + Q is the list [1, 2, 3, 4, 5, 6]. Again, list concatenation is very similar to string concatenation.
pythonL = [90, 47, 8, 18, 10, 7] S = [L[0]] # list containing just one element for i in range(1, len(L)): flag = True for j in range(len(S)): if L[i] < S[j]: before_j = S[: j] # elements in S before index j new_j = [L[i]] # list containing just one element after_j = S[j: ] # elements in S starting from index j # what is the size of S now? S = before_j + new_j + after_j # what is the size of S now? flag = False break if flag: S.append(L[i]) print(S)
- ```python
[90, 47, 8, 18, 10, 7]
- ```python
[7, 10, 18, 8, 47, 90]
- ```python
[7, 8, 10, 18, 47, 90]
- ```python
[90, 47, 18, 10, 8, 7]
- ```python
[90, 7, 8, 10, 18 47]
Status: Yes, the answer is correct.
Score: Score: 2
Feedback:
Observe closely as to how we are arranging the elements of list L in S. For 1 <= i <= len(S) - 1, what can you say about the expression S[i - 1] <= S[i]? What do you conclude from this? If you work through the code, you will see that the list is being sorted in ascending order. Do you see why that is the case?
Accepted Answers:
python[7, 8, 10, 18, 47, 90]
Question 6
Consider the following grid of integer points in the plane: Pij=(i,j), 0≤i,j≤4.

We wish to represent this grid of points as a list of tuples. The name of the list should be points. Each element of the list should be a tuple of the form (x, y). The first element of the tuple is the x-coordinate (horizontal) of the point, the second is the y-coordinate (vertical).
We have employed the standard Cartesian coordinate system. Select all the correct implementations of this program. (MSQ)
- ```python
points = [ ]
for x in range(0, 5):
for y in range(0, 5):
points.append(x, y)
- ```python
points = [ ]
for x in range(0, 5):
for y in range(0, 5):
points.append([x, y])
- ```python
points = ( )
for x in range(0, 5):
for y in range(0, 5):
points.append((x, y))
- ```python
points = [ ]
for x in range(0, 5):
for y in range(0, 5):
points.append((x, y))
Status: Yes, the answer is correct.
Score: Score: 2
Feedback:
Option-(a) is syntactically wrong.
Option-(b) is wrong as it appends a list and not a tuple to points.
Option-(c) is wrong as it initializes points to be a tuple
Option-(d) is correct
Accepted Answers:
pythonpoints = [ ] for x in range(0, 5): for y in range(0, 5): points.append((x, y))
Question 7
pythonL = [y - x for x in [1, 2, 3] for y in [3, 4, 5] if y > x]
Which of the following codes are equivalent to the above code? [MSQ]
- ```python
L = [ ]
for x in [1, 2, 3]:
for y in [3, 4, 5]:
if y > x:
L.append(y - x)
- ```python
L = [ ]
for y in [3, 4, 5]:
for x in [1, 2, 3]:
if y > x:
L.append(y - x)
- ```python
L = [ ]
for x in [1, 2, 3]:
for y in [3, 4, 5]:
if y > x:
L += [y - x]
- ```python
L = []
for y in [3, 4, 5]:
for x in [1, 2, 3]:
if y > x:
L += [y -x]
Status: Yes, the answer is correct.
Score: Score: 2
Feedback:
This is more about understanding the syntax behind list comprehension with nested loops.
Accepted Answers:
pythonL = [ ] for x in [1, 2, 3]: for y in [3, 4, 5]: if y > x: L.append(y - x)
pythonL = [ ] for x in [1, 2, 3]: for y in [3, 4, 5]: if y > x: L += [y - x]
Question 8
We wish to find all integer triplets (x,y,z) such that:
x2+y2=z2, and 0<x<y<z<100
Select all snippets of code that create a list called triplets and store each triplet as a tuple. [MSQ]
- ```python
triplets = [(x, y, z) for x in range(1, 100)
for y in range(x + 1, 100)
for z in range(y + 1, 100)
if x ** 2 + y ** 2 == z ** 2]
- ```python
triplets = [ ]
for x in range(1, 100):
for y in range(x + 1, 100):
for z in range(y + 1, 100):
if x ** 2 + y ** 2 == z ** 2:
triplets.append((x, y, z))
- ```python
triplets = [(x, y, z) for x in range(1, 100)
for y in range(1, 100)
for z in range(1, 100)
if x ** 2 + y ** 2 == z ** 2 and x < y < z]
Status: Yes, the answer is correct.
Score: Score: 2
Feedback/Explanation:
pythontriplets = [(x, y, z) for x in range(1, 100) for y in range(x + 1, 100) for z in range(y + 1, 100) if x ** 2 + y ** 2 == z ** 2]
pythontriplets = [ ] for x in range(1, 100): for y in range(x + 1, 100): for z in range(y + 1, 100): if x ** 2 + y ** 2 == z ** 2: triplets.append((x, y, z))
pythontriplets = [(x, y, z) for x in range(1, 100) for y in range(1, 100) for z in range(1, 100) if x ** 2 + y ** 2 == z ** 2 and x < y < z]
Accepted Answers:
pythontriplets = [(x, y, z) for x in range(1, 100) for y in range(x + 1, 100) for z in range(y + 1, 100) if x ** 2 + y ** 2 == z ** 2]
pythontriplets = [ ] for x in range(1, 100): for y in range(x + 1, 100): for z in range(y + 1, 100): if x ** 2 + y ** 2 == z ** 2: triplets.append((x, y, z))
pythontriplets = [(x, y, z) for x in range(1, 100) for y in range(1, 100) for z in range(1, 100) if x ** 2 + y ** 2 == z ** 2 and x < y < z]
Question 9
L is a list of names. Create a list P of names that contain only those names in L that begin with a capital letter. Select all correct implementations. [MSQ]
- ```python
P = [name for name in L if 'a' <= name[0] <= 'z']
- ```python
P = [name for name in L if 'A' <= name[0] <= 'Z']
- ```python
P = [ ]
for name in L:
if 'A' <= name[0] <= 'Z':
P.append(name)
Status: Yes, the answer is correct.
Score: Score: 2
Feedback/Explanation:
pythonP = [name for name in L if 'A' <= name[0] <= 'Z']
pythonP = [ ] for name in L: if 'A' <= name[0] <= 'Z': P.append(name)
Accepted Answers:
pythonP = [name for name in L if 'A' <= name[0] <= 'Z']
pythonP = [ ] for name in L: if 'A' <= name[0] <= 'Z': P.append(name)
Question 10
Accept a sequence of comma-separated strings as input from the user and populate a list of strings that do not have the letter 'e' in them. Print this list as output to the console. Select all snippets of code that achieve this. [MSQ]
- ```python
P = input().split(',')
L = [ ]
for word in P:
if 'e' not in word:
L.append(word)
print(L)
- ```python
L = [word for word in input().split(',') if 'e' not in word]
print(L)
- ```python
print([word for word in input().split(',') if 'e' not in word])
Status: Yes, the answer is correct.
Score: Score: 2
Feedback/Explanation:
pythonP = input().split(',') L = [ ] for word in P: if 'e' not in word: L.append(word) print(L)
pythonL = [word for word in input().split(',') if 'e' not in word] print(L)
pythonprint([word for word in input().split(',') if 'e' not in word])
Accepted Answers:
pythonP = input().split(',') L = [ ] for word in P: if 'e' not in word: L.append(word) print(L)
pythonL = [word for word in input().split(',') if 'e' not in word] print(L)
pythonprint([word for word in input().split(',') if 'e' not in word])
Question 11
Consider the following snippet of code.
pythondef minmax(a, b): if a <= b: return a, b return b, a
x is a real number. When minmax(x, x) is called, which return statement in the function is executed?
- The return statement in line-3 which is inside the if-block.
- The return statement in line-4 which is outside the if-block.
- Both the return statements are executed.
- Neither return statement is executed.
Status: Yes, the answer is correct.
Score: Score: 2
Feedback:
The moment any one return statement is executed, control exits from the function.
Accepted Answers:
The return statement in line-3 which is inside the if-block.
Question 12
Select all correct implementations of a function named unique that accepts a non-empty list L of integers as an argument. The function should remove all duplicate elements from the list L. Specifically, It should return a list L_uniq that retains the first occurrence (from the left) of each distinct element in the input list L.
A few instances of the input-output behaviour of the function is given below. Note that these are just sample test cases. Your function should work for any non-empty list L of integers. (MSQ)
- ```python
def unique(L):
L_uniq = [ ]
for elem in L:
if elem not in L_uniq:
L_uniq.append(elem)
return L_uniq
- ```python
def unique(L):
L_uniq = [ ]
for elem in L:
if elem in L_uniq:
L_uniq.append(elem)
return L_uniq
- ```python
def unique(L):
L_uniq = [L[0]]
for i in range(1, len(L)):
if not(L[i] in L[:i]):
L_uniq.append(L[i])
return L_uniq
- ```python
def unique(L):
L_uniq = [ ]
for i in range(1, len(L)):
if not(L[i] in L[:i]):
L_uniq.append(L[i])
return L_uniq
- ```python
def unique(L):
L_uniq = [ ]
for i in range(0, len(L)):
if not(L[i] in L[i + 1: ]):
L_uniq.append(L[i])
return L_uniq
Status: Yes, the answer is correct.
Score: Score: 2
Feedback:
- Option-(a) appends an elem to L_uniq if it is not already present in it. So, only the first occurrence of every distinct element in L finds its way to L_uniq. All duplicates are naturally thrown out.
- Option-(b) is wrong as the condition if elem in L_uniq doesn't make any sense.
- Option-(c):
L[i] in L[:i]is the important statement here. This expression is True if the element L[i] is already present in the sliceL[: i]which represents the first i−1 elements of the list. So, we append L[i] to L_uniq only if it is not present in this slice. This helps us retain the first occurrence of all distinct elements in L. The initialization L_uniq = [L[0]] is very important here. Do you see why? Take a concrete example of a list and work it out, say L = [1, 2, 3, 4, 5]. - Option-(d): the main problem is that L_uniq is initialized as the empty list.
- Option-(e): this does remove all duplicates, but L_uniq doesn't retain the first occurrence of distinct elements. Instead, it retains the last occurrence of all distinct elements. Try out the case [1, 2, 1, 3, 4, 5, 4] and you will notice the difference.
Accepted Answers:
pythondef unique(L): L_uniq = [ ] for elem in L: if elem not in L_uniq: L_uniq.append(elem) return L_uniq
pythondef unique(L): L_uniq = [L[0]] for i in range(1, len(L)): if not(L[i] in L[:i]): L_uniq.append(L[i]) return L_uniq
Question 13
Given a Python list of the coefficients of a polynomial — L = [a_0, a_1, a_2,. . ., a_n] — write a function poly that accepts the list of coefficients L and a real number x_0 as arguments. It should return the polynomial evaluated at the value x_0.
For example poly([1, 2, 3], 5) should return the value 1+2×5+3×52=86. Select the correct implementation of this function.
- ```python
def poly(L, x_0):
n = len(L)
for i in range(n):
psum = psum + L[i] * (x_0 ** i)
return psum
- ```python
def poly(L, x_0):
psum = 0
n = len(L)
for i in range(1, n):
psum = psum + L[i] * (x_0 ** i)
return psum
- ```python
def poly(L, x_0):
psum = 0
n = len(L)
for i in range(n):
psum = psum + L[i] * (x_0 ** i)
return psum
- ```python
def poly(L, x_0):
psum = 0
n = len(L)
for i in range(n):
psum = psum + x_0 * (L[i] ** i)
return psum
Status: Yes, the answer is correct.
Score: Score: 2
Feedback:
- Option-(a): psum has not been initialized
- Option-(b): Why are we starting from range(1, n)? Isn't that wrong?
- Option-(d): The positions of L[i] and x_0 have been interchanged, which is wrong.
Accepted Answers:
pythondef poly(L, x_0): psum = 0 n = len(L) for i in range(n): psum = psum + L[i] * (x_0 ** i) return psum
Question 14
Write a function named poly_zeros that accepts the list of coefficients L, and two integers a and b as arguments. It should return a list named zeros, that consists of all integer-zeros of the polynomial in the range [a,b], endpoints inclusive, in ascending order. For example, poly_zeros([2, -3, 1], 0, 4) should return the list [1, 2].
Assume that you have access to the function poly that was defined in question (7). Select the correct implementation of this function.
Note: The returned list should not have any repeating elements.
- ```python
def poly_zeros(L, a, b):
zeros = [ ]
for x in range(a, b + 1):
if poly(L, x) != 0:
zeros.append(x)
return zeros
- ```python
def poly_zeros(L, a, b):
zeros = [ ]
for x in range(a, b + 1):
if poly(L, x) == 0:
zeros.append(x)
else:
return zeros
return zeros
- ```python
def poly_zeros(L, a, b):
zeros = [ ]
for x in range(a, b + 1):
if poly(L, x) == 0:
zeros.append(x)
- ```python
def poly_zeros(L, a, b):
zeros = [ ]
for x in range(a, b + 1):
if poly(L, x) == 0:
zeros.append(x)
return zeros
Status: Yes, the answer is correct.
Score: Score: 2
Feedback:
- Option-(a): Why poly(L, x) != 0? We should be checking for zeros.
- Option-(b): The else part is problematic. It is unnecessary here.
- Option-(c): zeros is not being returned here.
Accepted Answers:
pythondef poly_zeros(L, a, b): zeros = [ ] for x in range(a, b + 1): if poly(L, x) == 0: zeros.append(x) return zeros
Question 15
Using the two functions defined above, find the number of integer-zeros of the polynomial f(x)=x6−4x5−18x4+52x3+101x2−144x−180 in the range [0,4], endpoints inclusive. You must enter a non-negative integer. Note that we are asking for the number of integer-zeros and not their values. (NAT)
Your Answer:
2Status: Yes, the answer is correct.
Score: Score: 2
Feedback:
The entire setup is as follows:
def poly(L, x_0): psum = 0 n = len(L) for i in range(n): psum = psum + L[i] * (x_0 ** i) return psum def poly_zeros(L, a, b): zeros = [ ] for x in range(a, b + 1): if poly(L, x) == 0: zeros.append(x) return zeros L = [-180, -144, 101, 52, -18, -4, 1] a, b = 0, 4 zeros = poly_zeros(L, a, b) print(len(zeros))
Accepted Answers:
(Type: Numeric) 2