0% found this document useful (0 votes)
5 views7 pages

Week 4

Uploaded by

riyaranirr2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views7 pages

Week 4

Uploaded by

riyaranirr2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

The Joy of Computing using Python

Rupesh Kumar
Friday 16 August 2024
6-8 pm

1. Select the correct statements regarding Magic Square:


a. Magic Square of Order 1 is Trivial
b. Sum of 2 magic squares is a magic square
c. Magic Square of Order 2 is not possible
d. The magic constant (the sum of row/columns/diagonal elements) for a 7*7
possible magic square is 260
Answer: option a, c

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

matrix[i][r-1-i] for i in range(r)


i=0
matrix[0][2] + matrix[1][1] + matrix[2][0] != ExpectedValue, return
False
3. Suppose you want to simulate the Birthday Paradox by generating random birthdays
for a group of people. Which library function would you most likely use to create
random integers representing birthdays?
a. random.randint()
b. time.time()
c. random.random()
d. None of the above
Answer: option a

4. Which of the following are functions in the date-time module of Python?


a. datetime.now ()
b. datetime.present()
c. date.today()
d. date.now()
Answer: Option a, c
5. The "Masked Gun" game is a simple guessing game where the player attempts to
unlock a masked gun by correctly guessing a secret 4-digit code.
Arrange the steps in the correct sequence to create a Python code for a game.
1) Implement Game Loop:
- Set up a loop for the main game logic.
2) Display Masked Gun:
- Create a function to visually represent the masked gun.
3) Generate Secret Code
- Write a function to generate a random 4-digit secret code.
4) Provide Feedback and Manage Attempts for Guessing
- Display feedback based on the correctness of the guess and keep track of the
number of attempts left for guessing
5) End Game:
- End the game when the player correctly guesses the code or runs out of
attempts.
6) Validate and Compare Guess with Secret Code
- Check if the entered guess is a 4-digit numeric code and compare the player's
guess with the generated secret code.
Enter your answer as a sequence without commas (NAT):
(Hint: Use the Logic like Guess the Movie game discussed in the lecture)
Answer: 3->2->1->6->4->5
Step 1: Generate Secret Code
Step 2: Display masked Gun
Step 3: Implement the game loop
Step 4: Allow the user to guess the secret code given that the user has not run out
of maximum attempts allowed and Validate if the code is correct
Step 5: We can give feedback to the user if the code is correct or not (hints)
Step 6: End Game

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.

Saturday Sunday Monday Tuesday Wednesday Thursday Friday


1 7
5th March
How many dates from 1 Jan to 5th March (31 + 29 + 5 =65 days)
65 // 7 = 9 – 63rd date would have been a Friday
3rd March was Friday, so 5th is Sunday

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

a) [90, 47, 8, 18, 10, 7]


b) [7, 10, 18, 8, 47, 90]
c) [7, 8, 10, 18, 47, 90]
d) [90, 47, 18, 10, 8, 7]
Answer: Option c
9. What does the random. choice () function do?
a. Generates a random integer.
b. Selects a random element from a sequence.
c. Chooses a random floating-point number.
d. Generates a random Boolean value.
Answer: Option b

10. Identify the magic squares from the following:


Answer: Option a, b

Programming Problems

Write a program to compute matrix matrix sum.

Write a program that computes matrix vector product.

Write a program that computes matrix matrix product.

Write a program that computes elementwise matrix product.

You might also like