# The Joy of Computing using Python | Week 4

Session: JULY-DEC 2023

Course Name:  The Joy of Computing using Python

#### These are answers of 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 answers of 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 answers of 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 answers of 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 answers of The Joy of Computing using Python Assignment 4 Answers

Session Jan-Apr 2023

Course Name:  The Joy of Computing using Python

These are answers of 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 answers of 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 answers of 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 answers of 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 answers of 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 answers of 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 answers of The Joy of Computing using Python Assignment 4 Answers

More Nptel courses: https://progiez.com/nptel/

Session JUL-DEC 2022

#### These are answers of 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 answers of 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 answers of 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 answers of 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 answers of 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 answers of 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 answers of 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``

This is an assignment for The Joy of Computing using Python Week 4 Assignment

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``

This is an assignment for The Joy of Computing using Python Week 4 Assignment

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

Solution:

``//Code``

These are answers of The Joy of Computing using Python Assignment 4 Answers

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

This content is uploaded for study, general information, and reference purpose only.