# The Joy of Computing Using Python NPTEL Assignment 4 Answers

Are you looking for The Joy of Computing Using Python NPTEL Week 4 Answers? You’ve come to the right place! Access the latest and most accurate solutions for your Assignment 4 in The Joy of Computing Using Python course

## The Joy of Computing Using Python NPTEL Week 4 Answers (July-Dec 2024)

1. Which of the following options provides the general formula for the magic constant of a magic square of size n, where all elements are distinct numbers from 1 to n²?

A) n(n² + 1) / 2
B) n³ / 2
C) n³ + 2 / 2
D) n⁴ + n² / 2n

Answer: A) n(n² + 1) / 2

2. What would the magic constant be for a magic square of size 6, given that all elements in the square are distinct numbers from 1 to 36?

A) 72
B) 111
C) 109
D) 110

3. Does transposing the magic square give us a new magic square?

A) Yes
B) No

The Joy of Computing Using Python NPTEL Week 4 Answers

4. Which of the following are valid magic squares?

A) ⎡⎣⎢⎢⎢10541531696132712811141⎤⎦⎥⎥⎥
B) ⎡⎣⎢⎢⎢20−e10−e8−e30−e6−e32−e18−e12−e26−e4−e14−e24−e16−e22−e28−e2−e⎤⎦⎥⎥⎥
C) ⎡⎣⎢⎢⎢18131214112745169151036⎤⎦⎥⎥⎥
D) ⎡⎣⎢⎢⎢π8π13π12π14π11π2π7π4π5π16π9π15π10π3π6π⎤⎦⎥⎥⎥

5. What is the minimum number of people required to ensure that at least two of them share the same 30-minute birth interval? The intervals start from 12:00 AM and each interval lasts for half an hour.

6. Calculate the magic constant for a 4×4 square, where all elements are distinct numbers from 1 to 16. Is it the same as the magic constant for Ramanujan’s magic square?

If yes, enter 0, else enter the absolute difference between the two.
Hint: Search about Ramanujan’s magic square.

The Joy of Computing Using Python NPTEL Week 4 Answers

7. What task does function1() perform?

A) Calculate factorial of number n.
B) Calculate factors of number n.
C) Calculate prime factors of number n.
D) Calculate factors of number n excluding n.

Answer: D) Calculate factors of number n excluding n.

8. For what n1, n2 will the variable flag inside function2() be not equal to true?

A) 2, 3
B) 0, 0
C) 1, 1
D) 1, 0

Answer: B) 0, 0
C) 1, 1
D) 1, 0

9. If all possible pairs of prime numbers between 0 and 20 are given to n1 and n2, for how many pairs would function2 print “Completed”?

A) It will not print “Completed” for any pair.
B) It will print “Completed” only for pairs (2,3), (3,5), (2,5), and for the remaining it would not print “Completed”.
C) It will print “Completed” only for pair (2,3), and for the remaining other pairs of primes it would not print “Completed”.
D) It will print “Completed” for all pairs of primes between 0 and 20.

Answer: D) It will print “Completed” for all pairs of primes between 0 and 20.

10. If numbers of pairs of primes which result in function2 to print “Completed” are greater than 0, can we edit the code in function2() so that “Completed” is never printed for any pair of primes?

A) Yes, we can change the logic for setting the flag variable to True.
B) Yes, we can change/increase the threshold for length of list2 in the last if block.
C) No, it is logically not possible.
D) Yes, we can change the initial value of flag to True, instead of False.

Answer: B) Yes, we can change/increase the threshold for length of list2 in the last if block.C) No, it is logically not possible.

The Joy of Computing Using Python NPTEL Week 4 Answers

All Weeks of The Joy of Computing Using Python: Click here

## The Joy of Computing Using Python NPTEL Week 4 Answers (Jan-Apr 2024)

Course Name: The Joy of Computing using Python

The Joy of Computing Using Python NPTEL Week 4 Answers

Q1. Select the correct statements regarding Magic Square:
Magic Square of Order 1 is Trivial
Sum of 2 magic squares is a magic square
Magic Square of Order 2 is not possible
The magic constant (the sum of row/columns/diagonal elements) for a 7*7 possible magic square is 260

Answer: a, b, c, d

Q2. For which of the matrices would the following code snippet return True?
a. [8 3 4 1 5 9 6 7 2 ]
b. [2 7 6 9 5 1 4 3 8]
c. [6 7 2 1 5 9 8 3 4]
d. [2 9 4 7 5 3 6 1 8]

Answer: a, b, c, d

The Joy of Computing Using Python NPTEL Week 4 Answers

Q3. 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?
random.randint()
time.time()
random.random()
None of the above

Q4. Which of the following are functions in the date-time module of Python?
datetime.now ()
datetime.present()
date.today()
date.now()

The Joy of Computing Using Python NPTEL Week 4 Answers

Q5. 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)

Q6. To implement a magic square, we use the concept of matrices. Select all the statements that are True in this context.
One possible Implementation of matrices is through nested lists in Python.
NumPy is one of the standard libraries associated with implementing matrices.
Implementation of matrices is only through lists in Python.
None of the above

The Joy of Computing Using Python NPTEL Week 4 Answers

Q7. Consider the given dataset of 60 people born in 2000. Run your code using the given data to check the count of people whose birthdays fall exactly on a Tuesday.
8
9
10
11

Q8. What is the output of the following snippet of code?
Hint:
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.
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.

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

Answer: [7, 8, 10, 18, 47, 90]

The Joy of Computing Using Python NPTEL Week 4 Answers

Q9. What does the random. choice () function do?
Generates a random integer.
Selects a random element from a sequence.
Chooses a random floating-point number.
Generates a random Boolean value.

Answer: Selects a random element from a sequence.

Q10. Identify the magic squares from the following:

Answer: a, b, c, d

The Joy of Computing Using Python NPTEL Week 4 Answers

### Programming Assignment

Question 1

NA

Solution:

``NA``

The Joy of Computing Using Python NPTEL Week 4 Answers

Question 2

Accept two square matrices A and B of dimensions n×n as input and compute their product AB.
The first line of the input will contain the integer n. This is followed by 2n lines. Out of these, each of the first n lines is a sequence of comma-separated integers that denotes one row of the matrix A. Each of the last n lines is a sequence of comma-separated integers that denotes one row of the matrix B.
Your output should again be a sequence of n lines, where each line is a sequence of comma-separated integers that denote a row of the matrix AB.

Solution:

``````def matrix_product(A, B):
n = len(A)
result = [[0] * n for ads in range(n)]

for i in range(n):
for j in range(n):
for k in range(n):
result[i][j] += A[i][k] * B[k][j]

return(result)

# Input
n = int(input())
A = []
B = []

# Input matrix A
for apple in range(n):
row = list(map(int, input().strip().split(',')))
A.append(row)

# Input matrix B
for mango in range(n):
row = list(map(int, input().strip().split(',')))
B.append(row)

# Compute matrix product AB
AB = matrix_product(A, B)

# Output
for row in AB:
print(','.join(map(str, row)))``````