# The Joy Of Computing Using Python Week 4 : Assignment 1 | NPTEL | Answers July 2023

## What is a magic square?

a. A square grid of letters
b. A square grid of numbers where the sum of the rows, columns, and diagonals are equal
c. A special kind of card trick
d. A term used in cryptography

b. A square grid of numbers where the sum of the rows, columns, and diagonals are equal

A magic square is a square grid of numbers where the sum of the values in each row, column, and diagonal is the same. Each number in the grid is usually unique, and the arrangement of the numbers follows a specific pattern that ensures the magic property.

The most common type of magic square is one composed of integer numbers, but variations exist where other mathematical properties are used. Magic squares have been known and studied for centuries, and they have fascinated mathematicians and artists alike due to their intriguing properties and patterns.

## In a 3x3 magic square, what is the magic constant?

a. 3
b. 6
c. 9
d. 15

d. 15

In a 3x3 magic square, the magic constant is 15.
The magic constant is the sum of numbers in any row, column, or diagonal of the magic square. For normal magic squares of orders n = 3, 4, 5, 6, 7, and 8, the magic constants are, respectively: 15, 34, 65, 111, 175, and 260

## Which of the following is NOT a property of a magic square?

a. The sum of each row is equal
b. The sum of each column is equal
c. The sum of each diagonal is equal
d. The sum of each individual element is equal

d. The sum of each individual element is equal

The property of a magic square that is NOT true is "The sum of each individual element is equal". While the sum of each row, column, and diagonal is equal, the sum of individual elements is not necessarily equal

## What will be the output of the following code?

import string
import random

A = string.ascii_letters
n = int(input("Enter the number of rows/columns: "))

for i in range(n):
L = []
for j in range(n):
L.append(random.choice(A))

for element in L:
print(element, end='\t')  # Print elements in the same row
print()  # Move to the next row
a. A magic square of size 2.
b. A magic square of size n.
c. A magic square of an even size.
d. A magic square of an odd size.

b. A magic square of size n.

The code generates a grid with n rows and n columns, where each cell contains a random letter from the set of ASCII letters (both uppercase and lowercase).

Output:
Enter the number of rows/columns: 3
b	j	l
D	K	f
i	j	C

## What will be the output of the following code?

import random

L = []  # Initialize an empty list

for i in range(10):
L.append(random.randint(0, 10))  # Append random integers to the list

L.sort()  # Sort the list in ascending order
L.reverse()  # Reverse the sorted list to get descending order

print(L)
a. Sorted List(L) containing random elements between 0-10 in descending order.
b. Sorted List containing random elements between 0-10 in ascending order.
c. Sorted List containing elements between 0-10.
d. Sorted List containing elements between 0-9 in ascending order.

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

The code you've provided generates a list of 10 random integers between 0 and 10, sorts them in ascending order, and then reverses the sorted list to get a descending order. The correct description for the output would be:

"Sorted List containing random elements between 0-10 in descending order."

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

for i in range(2, 101):
flag = 0
for j in range(2, i):
if i % j == 0:
flag = 1
break
if flag == 0:
print(i)

## In the birthday paradox, as the number of people in a group increases, what happens to the probability that two people share a birthday?

a. It increases b. It decreases c. It stays the same d. It becomes impossible

a. It increases

In the birthday paradox, as the number of people in a group increases, the probability that two people share a birthday actually increases. This counterintuitive phenomenon is due to the fact that the number of possible pairs of people in the group also increases as the group size grows. This increase in pairs outweighs the decrease in the probability that a specific person doesn't share a birthday with another specific person. As a result, the likelihood of finding at least one pair of people with the same birthday becomes more likely as the group size increases.

## Which module is used to generate random numbers in Python?

a. math
b. random
c. stats
d. numpy

b. random

The module used to generate random numbers in Python is the random module. The random module is an in-built module of Python that can be used to generate pseudo-random numbers, meaning they are not truly random. The random module can be used to perform random actions such as generating random numbers, printing random values for a list or string, etc. The random module offers various functions to generate random numbers, including randint(), random(), choice(), and shuffle()

## Which function is used to shuffle a list in Python?

a. random.shuffle()
b. shuffle()
c. list.shuffle()
d. random_list()

a. random.shuffle()

The function used to shuffle a list in Python is random.shuffle()

## What is the output of the following code?

import random

nums = [1, 2, 3, 4, 5]
random.shuffle(nums)
print(nums)
a. [1, 2, 3, 4, 5]
b. [5, 4, 3, 2, 1]
c. A random ordering of the numbers 1 through 5
d. An error