# The Joy of Computing using Python | Week 4

Session: JAN-APR 2024

Course Name: The Joy of Computing using Python

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

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]

These are the Joy of Computing using Python Assignment 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()

These are the Joy of Computing using Python Assignment 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.
– 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.
(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

These are the Joy of Computing using Python Assignment 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]

These are the Joy of Computing using Python Assignment 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:

These are the Joy of Computing using Python Assignment 4 Answers

### The Joy of Computing using Python Programming Assignment

Question 1

NA

Solution:

NA

These are the Joy of Computing using Python Assignment 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)))

These are the Joy of Computing using Python Assignment 4 Answers

Question 3

Accept a square matrix A and an integer s as input and print the matrix s⋅A as output. Multiplying a matrix by an integer s is equivalent to multiplying each element of the matrix by s. For example:
The first line of input is a positive integer, n, that denotes the dimension of the matrix A. Each of the next n lines contains a sequence of space-separated integers. The last line of the input contains the integer s.

Print the matrix sA as output. Each row of the matrix must be printed as a sequence of space-separated integers, one row on each line. There should not be any space after the last number on each line. If the expected output looks exactly like the actual output and still you are getting a wrong answer, it is because of the space at the end.

Solution:

n = int(input())  # Dimension of the matrix A
A = []  # Initialize matrix A

# Input matrix A
for pay in range(n):
row = list(map(int, input().split()))  # Read a row of integers
A.append(row)

s = int(input())  # Scalar value

# Multiply each element of matrix A by the scalar s
result = [[s * A[i][j] for j in range(n)] for i in range(n)]

# Print the result
for row in result:
print(*row)

These are the Joy of Computing using Python Assignment 4 Answers

Session: JULY-DEC 2023

Course Name: The Joy of Computing using Python

These are the Joy of Computing using Python Assignment 4 Answers

Q1. What is a magic square?
A square grid of letters
A square grid of numbers where the sum of the rows, columns, & diagonals are equal
A special kind of card trick
A term used in cryptography

Answer: A square grid of numbers where the sum of the rows, columns, & diagonals are equal

Q2. In a 3×3 magic square, what is the magic constant?
3
6
9
15

Q3. Which of the following is NOT a property of a magic square?
The sum of each row is equal
The sum of each column is equal
The sum of each diagonal is equal
The sum of each individual element is equal

Answer: The sum of each individual element is equal

These are the Joy of Computing using Python Assignment 4 Answers

Q4. What will be the output of the following code?
A magic square of size 2.
A magic square of size n.
A magic square of an even size.
A magic square of an odd size.

Answer: A magic square of size n.

Q5. What will be the output of the following code?
Sorted List(L) containing random elements between 0-10 in descending order.
Sorted List containing random elements between 0-10 in ascending order.
Sorted List containing elements between 0-10.
Sorted List containing elements between 0-9 in ascending order.

Answer: Sorted List(L) containing random elements between 0-10 in descending order.

Q6. Which code will generate all prime numbers between 0-100?

These are the Joy of Computing using Python Assignment 4 Answers

Q7. In the birthday paradox, as the number of people in a group increases, what happens to the probability that two people share a birthday?
It increases
It decreases
It stays the same
It becomes impossible

Q8. Which module is used to generate random numbers in Python?
math
random
stats
numpy

Q9. Which function is used to shuffle a list in Python?
random.shuffle()
shuffle()
list.shuffle()
random_list()

These are the Joy of Computing using Python Assignment 4 Answers

Q10. What is the output of the following code?
import random
nums = [1, 2, 3, 4, 5]
random.shuffle(nums)
print(nums)

[1, 2, 3, 4, 5]
[5, 4, 3, 2, 1]
A random ordering of the numbers 1 through 5
An error

Answer: A random ordering of the numbers 1 through 5

These are the Joy of Computing using Python Assignment 4 Answers

Session Jan-Apr 2023

Course Name: The Joy of Computing using Python

These are the Joy of Computing using Python Assignment 4 Answers

#### Q1. Which of the following statements are true with regards to magic square?a. The sum of each row should be m.b. The sum of each column should be m.c. The sum of each diagonal should be m.d. None of the above.

Q2. Which of the following statements hold true about N in the magic square? N denotes the number of rows and columns in the square.
a. N should be even.
b. N should be odd.
c. N can be even or odd.
d. N can take any value.

Answer: b. N should be odd.

Q3. Which of the following statements are true regarding the Magic Squares? (N = Number of rows or columns)
a. A Magic Square is always a square matrix.
b. A Magic Square can or cannot be a square matrix.
c. The Sum of each row and each column is N(N+1)/2
d. The Sum of each row and each column is N(N2+1 )/2.

These are the Joy of Computing using Python Assignment 4 Answers

Q4. What will be the output of the following code?

a. This is a sentence.
b. Error
c. No output
d. The program will not run.

Q5. Which of the following operator is used to raise the exponent to a number?
a. ^
b. *
c. **
d. ***

Q6. Suppose there is a movie with 3 letters, how many combinations of names are possible?
a. 26
b. 676
c. 17576
d. 456976

These are the Joy of Computing using Python Assignment 4 Answers

Q7. What should be the value of a, b, c, d respectively?

a. 1,3,9,7
b. 9,3,7,1
c. 1,7,3,9
d. 7,3,9,1

Q8. What will be the output of the following code?

a. Print unique movies of list L1
b. Print unique movies of list L2
c. Print unique movies of list L1 and L2
d. Shows an error

Answer: a. Print unique movies of list L1

Q9. What will be the output of the following code?

a. Print all perfect squares with square roots between 5-20 and divisible by 5.
b. Print all perfect squares with square roots between 5-20 and not divisible by 5.
c. Print all perfect squares with square roots between 5-19 and not divisible by 5.
d. Print all perfect squares with square roots between 5-19 and divisible by 5.

Answer: d. Print all perfect squares with square roots between 5-19 and divisible by 5.

These are the Joy of Computing using Python Assignment 4 Answers

Q10. A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself. For example, 6 is a perfect number as the sum of its divisors 1,2,3 is equal to 6.
Which function will return True if the number is a perfect number?

a.

b.

c.

d.

### The Joy of Computing using Python Programming Assignmnet

Question 1

\Write a program that takes a number `n` as input and prints the sum of the squares of the first `n` positive integers.
Example:
If n = 4, the program should output 30 (1^2 + 2^2 + 3^2 + 4^2 = 30)

Solution:

def sum_of_squares(n):
sum = 0
for i in range(1, n+1):
sum += i**2
return sum

n = int(input())
print(sum_of_squares(n))

These are the Joy of Computing using Python Assignment 4 Answers

Question 2

Write a program that takes a number `n` as input and prints the nth power of 2.
Example:
If n = 4, the program should output 16 (2^4 = 16)

Solution:

def power_of_two(n):
return 2**n

n = int(input())
print(power_of_two(n))

These are the Joy of Computing using Python Assignment 4 Answers

Question 3

Write a program that takes a number `n` as input and prints a pyramid of numbers with `n` rows.
Example:
If n = 5, the program should output the following

1
232
34543
4567654
567898765

Solution:

def pyramid(n):
for i in range(1, n+1):
spaces = " " * (n - i)
numbers = ""
for j in range(i, 2 * i):
numbers += str(j % 10)
for k in range(2 * i - 2, i - 1, -1):
numbers += str(k % 10)
print(spaces + numbers)

n = int(input())
pyramid(n)

These are the Joy of Computing using Python Assignment 4 Answers

Session JUL-DEC 2022

#### Course name: The Joy of Computing Using Python

These are the Joy of Computing using Python Assignment 4 Answers

Q1. Which of the following statements are true regarding the Magic Squares? (N = Number of rows or columns)
a. A Magic Square is always a square matrix.
b. A Magic Square can or cannot be a square matrix.
c. The Sum of each row and each column is N(N+1)/2
d. The Sum of each row and each column is N(N2 +1)/2.

Q2. What will be the output of the following code?
a. This is a sentence.
b. Error
c. No output
d. The program will not run

Q3. A perfect number is a number in which the sum of its proper divisors is equal to that number. For example, 6 is a perfect number as the sum of its divisors 1,2,3 is equal to 6. Which function returns True if the number is perfect?

These are the Joy of Computing using Python Assignment 4 Answers

Q4. Suppose there is a movie with 3 letters, how many combinations of names are possible?
a. 26
b. 676
c. 17576
d. 456976

Q5. What are the possible outputs of the following program?
a. Any number in the range between 0,4 (Both inclusive).
b. Any number in the range between 1,4 (Both inclusive).
c. Any number in the range between 0,5 (Both inclusive).
d. Any number in the range between 1,5 (Both inclusive).

Q6. Birthday Paradox can be simulated with approximately _______ people.
a. 365
b. 100
c. 40
d. 20

These are the Joy of Computing using Python Assignment 4 Answers

Q7. What is the command to print the last result of the ipython console?
a. ‘’
b. –
c. _
d. \\

These are the Joy of Computing using Python Assignment 4 Answers

Q8. What will be the output of the following program?
a. Passenger, Starwars
b. spiderman, jumanji
c. spiderman, jumanji, Passenger, Starwars?
d. spiderman, Starwars

These are the Joy of Computing using Python Assignment 4 Answers

Q9. In the ‘Dobble Game’, if there are 8 objects on 1 card and 10 objects on another, how many comparisons are possible?
a. 8
b. 10
c. 18
d. 80

These are the Joy of Computing using Python Assignment 4 Answers

Q10. What will be the output of the following code?
a. Display the number of consonants and name of the movie.
b. Display the number of letters and name of the movie.
c. Display the number of vowels and name of the movie.
d. Display the number of special characters and name of the movie.

These are the Joy of Computing using Python Assignment 4 Answers

## Python Assignment 4 Programming Solutions

Question 1
Take two numbers N and K as an input. Create a list L of length N and initialize it with zeros. Change the value to 1 of even indexes if k is even, otherwise change the value of odd indexes. Print list L in the end.(Consider 0 as even)
Input:
N
K
Output:
A list L
Example-
Input:
5
2
Output:
[1, 0, 1, 0, 1]

Solution:

//Code

These are the Joy of Computing using Python Assignment 4 Answers

Question 2
Write a program to take string S as an input and replace all vowels by *. Also print the modified string.
Input
A string S
Output
Modified string

Solution:

//Code

These are the Joy of Computing using Python Assignment 4 Answers

Question 3
Write a program to take an integer N as an input and display the pattern

Solution:

//Code

These are the Joy of Computing using Python Assignment 4 Answers

More NPTEL Solution: https://progiez.com/nptel

 The content uploaded on this website is for reference purposes only. Please do it yourself first.