Week 4
Week 4
Rupesh Kumar
Friday 16 August 2024
6-8 pm
2. For which of the matrices would the following code snippet return True?
a. 8 1 6 = 15, 15
3 5 7 = 15, 15
4 9 2 = 15, 15
15 Yes this matrix satisfies the the criterion.
b. 2 9 4 = 15, 15
7 5 3 = 15, 15
6 1 8 = 15, 15
c. 6 1 8 = 15, 15
7 5 3 = 15, 15
2 9 4 = 15, 15
d. 2 7 6 = 15, 15
9 5 1 = 15, 15
4 3 8 = 15, 15
Accepted Answers:
All of the above
def does_something(matrix):
r = len(matrix) # r = 3
if(r==0 or r!= len(matrix[0])): # checking if the matrix is blank
return False # or if the matrix is not a square matrix
expected = sum(matrix[0]) # sum(8,1,6) = 15
for row in matrix: # iterating through each row of the matrix
if(sum(row)) != expected: # are the sum of values in each row equal to
the expected value
return False
for j in range(r): # Looping through total rows in the matrix
if (sum(matrix[i][j] for i in range(r)) != expected:
return False # adding up the values columnwise
if(sum(matrix[i][r-1-i] for i in range(r)) != expected:
return False
return True
816
3 5 7 = [[8, 1, 6], [3, 5, 7], [4, 9, 2]]
492
6. To implement a magic square, we use the concept of matrices. Select all the
statements that are True in this context.
a. One possible Implementation of matrices is through nested lists in Python.
b. NumPy is one of the standard libraries associated with implementing matrices.
c. Implementation of matrices is only through lists in Python.
d. None of the above
Answer: option a, b
123
3 4 5 [[1,2,3],[3,4,5]]
7. Consider the given dataset of 32 people born in 2000. Run your code using the given
data to check the count of people
whose
birthdays fall exactly on a Tuesday
a. 8
b. 9
c. 10
d. 11
1 January 2000 – Saturday
2 January 2000 – Sunday
7 January 2000 – Friday
8 January 2000 – Saturday
9 - Sunday
24 January 2000 – How many weeks have been passed till 24th : int(24/7) = 3
22nd January – Saturday (1, 1+7=8, 8+7=15, 15+7=22). 22 –24 = 2 days Monday
5th Feb 2000
How many days have passed since 1 st January 2000: 31 days in Jan and 5 in Feb – total 36
days
How many complete weeks have passed: 36 // 7 = 5 weeks. 35th day would have been a
Friday. Thus, the 36th day is a Saturday. So, 5th Feb 2000 was a Saturday.
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.
Programming Problems