Neural Sync Active
Week 4 - Graded Assignment 4
Registry Synced
Week 4 - Graded Assignment 4
2846 words
14 min read
Course: Jan 2026 - Python
Topic: Processing Large Datasets (Probability) | Marks: 2
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.
Accepted Answers:
(Type: Numeric) 100000
Topic: Probability Estimations from Lists | Marks: 2
Question 2
Execute the following snippet of code.
some_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.
Accepted Answers:
It represents the probability that a number chosen at random from the list rolls is a prime.
Topic: Algorithmic Logic (Average Identification) | Marks: 3
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.
S = 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 ^average-check-logic
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.
Accepted Answers:
y is an element in the list L
y is the average (arithmetic mean) of the numbers in the list
Topic: Number Theory Constraints (Parity) | Marks: 3
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.
Accepted Answers:
n is an odd integer
Topic: Selection Sort Pattern (Manual Trace) | Marks: 4
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.
L = [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) ^selection-sort-manual
- ```
[90, 47, 8, 18, 10, 7] - ```
[7, 10, 18, 8, 47, 90] - ```
[7, 8, 10, 18, 47, 90] - ```
[90, 47, 18, 10, 8, 7] - ```
[90, 7, 8, 10, 18 47]
Status: Yes, the answer is correct.
Accepted Answers:
[7, 8, 10, 18, 47, 90]
Topic: List of Tuples (Grid Representation) | Marks: 3
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)
- ```
points = [ ]
for x in range(0, 5):
for y in range(0, 5):
points.append(x, y) - ```
points = [ ]
for x in range(0, 5):
for y in range(0, 5):
points.append([x, y]) - ```
points = ( )
for x in range(0, 5):
for y in range(0, 5):
points.append((x, y)) - ```
points = [ ]
for x in range(0, 5):
for y in range(0, 5):
points.append((x, y))
Status: Yes, the answer is correct.
Accepted Answers:
points = [ ] for x in range(0, 5): for y in range(0, 5): points.append((x, y))
Topic: List Comprehension Equivalence | Marks: 3
Question 7
L = [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]
- ```
L = [ ]
for x in [1, 2, 3]:
for y in [3, 4, 5]:
if y > x:
L.append(y - x) - ```
L = [ ]
for y in [3, 4, 5]:
for x in [1, 2, 3]:
if y > x:
L.append(y - x) - ```
L = [ ]
for x in [1, 2, 3]:
for y in [3, 4, 5]:
if y > x:
L += [y - x] - ```
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.
Accepted Answers:
L = [ ] for x in [1, 2, 3]: for y in [3, 4, 5]: if y > x: L.append(y - x)
L = [ ] for x in [1, 2, 3]: for y in [3, 4, 5]: if y > x: L += [y - x]
Topic: Pythagorean Triplets (Comprehensions) | Marks: 3
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]
- ```
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] - ```
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)) - ```
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.
Accepted Answers:
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]
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))
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]
Topic: Filtering Strings by Case | Marks: 3
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]
- ```
P = [name for name in L if 'a' <= name[0] <= 'z'] - ```
P = [name for name in L if 'A' <= name[0] <= 'Z'] - ```
P = [ ]
for name in L:
if 'A' <= name[0] <= 'Z':
P.append(name)
Status: Yes, the answer is correct.
Accepted Answers:
P = [name for name in L if 'A' <= name[0] <= 'Z']
P = [ ] for name in L: if 'A' <= name[0] <= 'Z': P.append(name)
Topic: Comma-separated Input Processing | Marks: 3
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]
- ```
P = input().split(',')
L = [ ]
for word in P:
if 'e' not in word:
L.append(word)
print(L) - ```
L = [word for word in input().split(',') if 'e' not in word]
print(L) - ```
print([word for word in input().split(',') if 'e' not in word])
Status: Yes, the answer is correct.
Accepted Answers:
P = input().split(',') L = [ ] for word in P: if 'e' not in word: L.append(word) print(L)
L = [word for word in input().split(',') if 'e' not in word] print(L)
print([word for word in input().split(',') if 'e' not in word])
Topic: Function Return Execution (Flow Control) | Marks: 2
Question 11
Consider the following snippet of code.
def 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.
Accepted Answers:
The return statement in line-3 which is inside the if-block.
Topic: Deduplication (Unique Elements) | Marks: 4
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)
- ```
def unique(L):
L_uniq = [ ]
for elem in L:
if elem not in L_uniq:
L_uniq.append(elem)
return L_uniq
^unique-list-implementation - ```
def unique(L):
L_uniq = [ ]
for elem in L:
if elem in L_uniq:
L_uniq.append(elem)
return L_uniq - ```
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 - ```
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 - ```
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.
Accepted Answers:
def unique(L): L_uniq = [ ] for elem in L: if elem not in L_uniq: L_uniq.append(elem) return L_uniq
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
Topic: Polynomial Evaluation Function | Marks: 3
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.
- ```
def poly(L, x_0):
n = len(L)
for i in range(n):
psum = psum + L[i] * (x_0 ** i)
return psum - ```
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 - ```
def poly(L, x_0):
psum = 0
n = len(L)
for i in range(n):
psum = psum + L[i] * (x_0 ** i)
return psum
^poly-evaluation-func - ```
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.
Accepted Answers:
def poly(L, x_0): psum = 0 n = len(L) for i in range(n): psum = psum + L[i] * (x_0 ** i) return psum
Topic: Finding Roots in a Range | Marks: 3
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.
- ```
def poly_zeros(L, a, b):
zeros = [ ]
for x in range(a, b + 1):
if poly(L, x) != 0:
zeros.append(x)
return zeros - ```
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 - ```
def poly_zeros(L, a, b):
zeros = [ ]
for x in range(a, b + 1):
if poly(L, x) == 0:
zeros.append(x) - ```
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.
Accepted Answers:
def poly_zeros(L, a, b): zeros = [ ] for x in range(a, b + 1): if poly(L, x) == 0: zeros.append(x) return zeros
Topic: Practical Polynomial Root Finding | Marks: 2
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.
Accepted Answers:
(Type: Numeric) 2
🧭 Navigation
- 📘 Textbook Notes: Week 4 Notes
- 📝 Assignment: Week 4 Assignment
- ⬅️ Previous Week: Week 3 Notes