The Joy of Computing using Python | Week 10

Session: JAN-APR 2024

Course Name: The Joy of Computing using Python

Course Link: Click Here

For answers or latest updates join our telegram channel: Click here to join

These are the Joy of Computing using Python Assignment 10 Answers


Q1. In the Josephus problem, a group of soldiers forms a circle, and every kth
soldier is eliminated until only one remains. What would be the output of the following code:
3
14
19
13

Answer: 14


Q2. What is the purpose of the lower() and upper() methods in Python when applied to strings?
lower() converts a string to all lowercase letters, while upper() converts a string to all uppercase letters.
lower() converts a string to all uppercase letters, while upper() converts a string to all lowercase letters.
Both lower() and upper() methods perform the same task of converting a string to title case.
lower() removes all whitespace from a string, while upper() adds whitespace to each character in a string.

Answer: lower() converts a string to all lowercase letters, while upper() converts a string to all uppercase letters.


For answers or latest updates join our telegram channel: Click here to join

These are the Joy of Computing using Python Assignment 10 Answers


Q3. What will be the output of the code snippet provided above?
orange, banana, cherry, orange, date
orange, banana, cherry, apple, date
apple, banana, cherry, apple, date
apple, banana, cherry, orange, orange

Answer: orange, banana, cherry, orange, date


Q4. Slicing in lists works in the same manner as slicing in strings. If P is a non-empty list of length n, we wish to create a list Q that has the first n−1 elements in P. Select the correct implementation(s) of this program.
Q = P[0: len(P) – 1]
Q = P[:len(P) – 1]
Q = P[0:-1]
Q = P[:-1]

Answer: a), b), c), d)


For answers or latest updates join our telegram channel: Click here to join

These are the Joy of Computing using Python Assignment 10 Answers


Q5. Select all correct options about the matrix given below.
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]

len(matrix) is the number of rows in the matrix
len(matrix[0]) is the number of columns of the matrix
The dimension of matrix is 4×3
The dimension of matrix is 3×4

Answer: a), b), c)


Q6. L is a non-empty list of integers and x is an integer. Assume that both L and x have already been defined. The following code does not throw any error when executed. Lines 1 and 6 will be used to refer to the state of the variables before and after key sections of the code are executed.
If the value of count is 10 at the end of the execution of the code (line 6), which of the following statements are true?

x is an element of L at line 1.
L has at least two different (unequal) elements in it at line 1
Length of L is 10 at line 1
Length of L is 0 at line 6

Answer: a), d)


For answers or latest updates join our telegram channel: Click here to join

These are the Joy of Computing using Python Assignment 10 Answers


Q7. If flag is True at the end of execution of the code given above, which of the following statements are true? Note that the options should be true for any list L that satisfies the conditions given in the common data. Multiple options could be correct.
y is an element in the list L
y is the greatest number in the list
y is the average (arithmetic mean) of the numbers in the list
y is the element at index len(L) // 2 in the list L

Answer: a), c), d)


Q8. Assume that L is a list of the first n positive integers, where n>0. Under what conditions will the variable flag be True at the end of the execution of the code given above?
n is an odd integer
n is an even integer
n is a complex number
None of the above

Answer: n is an odd integer


For answers or latest updates join our telegram channel: Click here to join

These are the Joy of Computing using Python Assignment 10 Answers


Q9. Which NumPy function is used to create a matrix of a specified shape with random values between 0 and 1?
np.random.randn()
np.random.random()
np.random.rand()
np.random.randint()

Answer: np.random.rand()


Q10. Select the correct implementation of a program that creates a text file named pattern.txt with the following contents:
a)
b)
c)
d)

Answer: c)


For answers or latest updates join our telegram channel: Click here to join

These are the Joy of Computing using Python Assignment 10 Answers


The Joy of Computing using Python Programming Assignment

Question 1

word is a string that contains one or more parentheses of the following types: “{ }”, “[ ]”, “( )”. The string is said to be balanced if all the following conditions are satisfied.
When read from left to right:
(1) The number of opening parentheses of a given type is equal to the number of closing parentheses of the same type.
(2) An opening parenthesis cannot be immediately followed by a closing parenthesis of a different type.
(3) Every opening parenthesis should be eventually closed by a closing parenthesis of the same type.

Solution:

def rotate(mat):
    if not mat:
        return list()

    rows = len(mat)
    cols = len(mat[0])

    rotated = [[0] * rows for csk in range(cols)]

    for ipl in range(rows):
        for jk in range(cols):
            rotated[jk][rows - 1 - ipl] = mat[ipl][jk]

    return(rotated)

For answers or latest updates join our telegram channel: Click here to join

These are the Joy of Computing using Python Assignment 10 Answers


Question 2

Consider the problem about balanced expressions discussed in 1. Programming Assignment | Week 9. We have a balanced expression (string) that has only the flower brackets: ‘( )’. We can recursively define a concept called nesting depth for each pair of opening and closing brackets.
The nesting depth of a pair that lies within another pair is one more than the nesting depth of the pair that immediately englobes it. For a pair that is not surrounded by any other pair, the nesting depth is 1.

Solution:

def is_magic(matrix):
    n = len(matrix)

    magic_sum = sum(matrix[0])

    for r1 in matrix:
        if sum(r1) != magic_sum:
            return "NO"

    for j in range(n):
        column_sum = sum(matrix[i][j] for i in range(n))
        if column_sum != magic_sum:
            return "NO"

    diagonal_sum1 = sum(matrix[i][i] for i in range(n))
    diagonal_sum2 = sum(matrix[i][n - 1 - i] for i in range(n))
    if diagonal_sum1 != magic_sum or diagonal_sum2 != magic_sum:
        return "NO"

    return("YES")

For answers or latest updates join our telegram channel: Click here to join

These are the Joy of Computing using Python Assignment 10 Answers


Question 3

Write a recursive function named power that accepts a square matrix A and a positive integer m as arguments and returns Am.

Solution:

n = int(input())
station_dict =dict()

for chapri in range(n):
    train_name = input()
    m = int(input())
    compartments = {}

    for hardik in range(m):
        compartment, passengers = input().split(",")
        compartments[compartment] = int(passengers)

    station_dict[train_name] = compartments

For answers or latest updates join our telegram channel: Click here to join

These are the Joy of Computing using Python Assignment 10 Answers


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

More Nptel Courses: Click here


Session: JULY-DEC 2023

Course Name: The Joy of Computing using Python

Course Link: Click Here

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


Q1. What is the output of the following code?
HELLO EVERYONE
Hello Everyone
helloeveryone
hello everyone

Answer: hello everyone


Q2. In flames game when we will stop the iteration over FLAMES?
When only one letter is left in flames.
Only once.
Only the letter remaining times.
None of the above.

Answer: When only one letter is left in flames.


Q3. Output of the following code will be?
hello
h.e.l.l.o
.h.e.l..l.o
.h.e.l.l.o

Answer: .h.e.l..l.o


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


Q4. Which code snippet represents replacing all vowels with ‘_’ in a string?

Answer: d)

image 6

Q5. What will be the output of the following list slicing.
‘Joy of C’
‘ Joy of C’
‘Joy of Co’
‘ Joy of Co’

Answer: ‘ Joy of C’


Q6. What does the following code represent?
Replacing all letters at odd index with ‘_’.
Replacing all vowels at odd index with ‘_’.
Replacing all vowels at even index with ‘_’.
Replacing all letters at even index with ‘_’.

Answer: Replacing all vowels at even index with ‘_’.


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


Q7. What will be the output of the following code?
[4 6]
[3 7]
[3 4]
None of the above

Answer: [3 7]


Q8. What is the correct way to display the transpose of a matrix?

Answer: b), c)

image 7
image 8

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


Q9. Are Lossy and Lossless compressions the same?
Yes, they are identical.
No, they are different.
It depends on the context.
Not enough information provided.

Answer: No, they are different.


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


Q10. What is the shape of the following numpy array?
numpy.array([ [1,2,3], [4,5,6] ])

(2,3)
(3,2)
(3,3)
(2,2)

Answer: (2,3)


Q11. What will be the output of the following code?
a. [[6 6 6]
[6 6 6]]
b. [[ -7 -7 -17]
[ -6 -26 -16]]
c. [[ 7 7 17]
[ 6 26 16]]
d. [[ 9 11 23]
[14 36 28]]

Answer: c. [[ 7 7 17]
[ 6 26 16]]


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


The Joy of Computing using Python Programming Assignmnet

Question 1
Given a list L write a program to make a new list to fix the indexes of numbers to in list L to its same value in the new list. Put 0 at remaining indexes. Also print the elements of the new list in the single line. (See explanation for more clarity.)
Input:
[1,5,6]
Output:
0 1 0 0 0 5 6
Explanation:
List L contains 1,5,9 so at 1,5,9, index of new list the respective values are put and rest are initialized as zero.

Solution:

K=[0]*(max(L)+1)
for i in range(len(K)):
  if i in L:
    K[i]=i
print(*K,end="")

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


Question 2
Ram shifted to a new place recently. There are multiple schools near his locality. Given the co-ordinates of Ram P(X,Y) and schools near his locality in a nested list, find the closest school. Print multiple coordinates in respective order if there exists multiple schools closest to him. Write a function closestSchool that accepts (X ,Y , L) where X and Y are co-ordinates of Ram’s house and L contains co-ordinates of different school.
Distance Formula(To calculate distance between two co-ordinates): √((X2 – X1)² + (Y2 – Y1)²) where (x1,y1) is the co-ordinate of point 1 and (x2, y2) is the co-ordinate of point 2.

Solution:

def closestSchool(x, y, L):
  min=9999999
  distance=list()
  for ij in L: 
    dis=((x-ij[0])**2+(y-ij[1])**2)**0.5
    distance.append(dis)
    if dis<min:
      min=dis  
  for ij in range(len(distance)):
    if distance[ij]==min:
        print(L[ij])

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


Question 3
Given a string s write a program to convert uppercase letters into lowercase and lowercase letters into uppercase. Also print the resultant string.
Note: You need to talk the input and do not print anything while taking input.
Input:
The Joy Of Computing
Output
tHE jOY oF cOMPUTING

Solution:

print(input().swapcase(),end='')

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

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

More Nptel Courses: Click here


Session Jan-Apr 2023

Course Name: The Joy of Computing using Python

Course Link: Click Here

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


Q1. Which math problem flames is related to?
a. kadane’s problem
b. Josephus problem
c. Conjecture Collatz
d. Dijkstra Problem

Answer: b. Josephus problem


Q2. What will be the output of the following list slicing.

image 16

a. ‘Joy of C’
b. ‘ Joy of C’
c. ‘Joy of Co’
d. ‘ Joy of Co’

Answer: b. ‘ Joy of C’


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


Q3. What will be the output of the following program?

image 17

a. I zm zmzzed
b. I zm zmazed
c. I am zmzzed
d. I am amazed

Answer: d. I am amazed


Q4. What are the consequences of image compression?
a. Less size
b. Lower quality
c. More size
d. Higher quality

Answer: a, b


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


Q5. what is the output of the following code?

image 18

a. [[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
b. [[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
c. Error
d. [[1,2,3,4,5,6]
[7, 8, 9, 10, 11, 12]]

Answer: a. [[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]


These are the Joy of Computing using Python Assignment 10 Answers


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

image 19

a. [4 6]
b. [3 7]
c. [3 4]
d. None of the above

Answer: b. [3 7]


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


Q7. Amongst which of the following is / are the method of list?
a. append()
b. extend()
c. insert()
d. All of the mentioned above

Answer: d. All of the mentioned above


These are the Joy of Computing using Python Assignment 10 Answers


Q8. The output of the following program will be?

image 20

a. Pynhon
b. Pnthon
c. Python
d. Error

Answer: d. Error


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


Q9. Which of the following is not a method in string?
a. lower()
b. upper()
c. isalpha()
d. insert()

Answer: d. insert()


These are the Joy of Computing using Python Assignment 10 Answers


Q10. What is the output of the following code?

image 21

a. HELLO EVERYONE
b. Hello Everyone
c. helloeveryone
d. hello everyone

Answer: d. hello everyone


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


The Joy of Computing using Python Programming Assignmnet

Question 1

Given a list L write a program to make a new list and match the numbers inside list L to its respective index in the new list. Put 0 at remaining indexes. Also print the elements of the new list in the single line. (See explanation for more clarity.)
Input:
[1,5,6]
Output:
0 1 0 0 0 5 6
Explanation:
List L contains 1,5,9 so at 1,5,9, index of new list the respective values are put and rest are initialized as zero.

Solution:

L = list(map(int, input().split()))

m = max(L)
L1 = [0]*(m+1)

for i in L:
    L1[i] = i


for i in range(len(L1)-1):
    print(L1[i], end=' ')

print(L1[-1])

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


Question 2

Ram shifted to a new place recently. There are multiple schools near his locality. Given the co-ordinates of Ram P(X,Y) and schools near his locality in a nested list, find the closest school. Print multiple coordinates in respective order if there exists multiple schools closest to him. Write a function closestSchool that accepts (X ,Y , L) where X and Y are co-ordinates of Ram’s house and L contains co-ordinates of different school.
Distance Formula(To calculate distance between two co-ordinates): √((X2 – X1)² + (Y2 – Y1)²) where (x1,y1) is the co-ordinate of point 1 and (x2, y2) is the co-ordinate of point 2.
Closest pont/points to X, Y

Solution:

def closestSchool(x,y,L):
    min = 99999
    distance = []
    for i in L:
        dis=((x-i[0])**2+(y-i[1])**2)**0.5
        distance.append(dis)
        if dis<min:
            min = dis
    for i in range(len(distance)):
        if distance[i]==min:
            print(L[i])

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


Question 3

Given a string s write a program to convert uppercase letters into lowercase and lowercase letters into uppercase. Also print the resultant string.
Note: You need to talk the input and do not print anything while taking input.
Input:
The Joy Of Computing
Output
tHE jOY oF cOMPUTING

Solution:

s = input()
ns =''

for i in range(len(s)):
    if s[i].isupper():
        ns = ns+s[i].lower()
    elif s[i].islower():
        ns = ns+s[i].upper()
    else:
        ns += s[i]

print(ns)

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

More Weeks of The Joy of Computing using Python: Click Here

More Nptel courses: https://progiez.com/nptel-assignment-answers/


These are answers of The Joy of Computing using Python Assignment 10 Answers
The content uploaded on this website is for reference purposes only. Please do it yourself first.