NPTEL Joy Of Computing Using Python Week 4 Programming Assignment 2025

In this article, We discuss Solutions for NPTEL Joy Of Computing Using Python Week 4 Programming Assignment 2025. If you didn't complete the assignment for Week 3 Programming, the link is provided below. Also, join the telegram for updated answers every week. 

Also Read: NPTEL Joy Of Computing Using Python Week 3 Programming Assignment

 Join us on Telegram 👉 CLICK HERE

NPTEL Joy Of Computing Using Python Week 4 Programming Assignment 2025

NPTEL Joy Of Computing Using Python


The following topics are discussed in week 4. 

  • Magic Square
  • Dobble Game
  • Birthday Paradox
  • Favourite game
Last Date: 20-02-2025

Q1. Create a Python program that checks whether a given square matrix is diagonal. A diagonal matrix is a square matrix (same number of rows and columns) where all the entries outside the main diagonal are zero. The program should prompt the user to input the dimensions of the matrix and then input the matrix elements. The program should then determine whether the matrix is diagonal and print 1 if it is, otherwise print 0.

Input Format:
The first input is an integer r , the number of rows and columns in the matrix.
The next r lines each contain r integers, representing the elements of each row of the matrix.

Output Format:
The output is 1 if a matrix is diagonal, otherwise 0.

Example:

Input:

2
1 0
0 1

Output:
1

Program Code:

def is_diagonal_matrix(matrix, r):
    for i in range(r):
        for j in range(r):
            if i != j and matrix[i][j] != 0:
                return 0
    return 1

r = int(input())
matrix = []
for _ in range(r):
    row = list(map(int, input().split()))
    matrix.append(row)

print(is_diagonal_matrix(matrix, r), end="") {codeBox}





Q2.Create a Python program that adds the transpose of a given matrix by a scalar. The program should prompt the user to input the dimensions of the matrix, the elements of the matrix, and the scalar value. The program should then compute the transpose of the matrix, add it by the scalar, and print the resulting matrix.

Input:
The first input is an integer 𝑟 , the number of rows in the matrix.
The second input is an integer 𝑐 , the number of columns in the matrix.
The next 𝑟 lines each contain 𝑐 integers, representing the elements of the matrix.
The final input is an integer 𝑠, representing the scalar value.

Output Format:
The output consists of 𝑐 lines, each containing 𝑟 integers, representing the elements of the resulting matrix after adding the transpose of the original matrix by the scalar.

Example:

Input:

2
3
1 2 3
4 5 6
2

Output:

3 6
4 7
5 8

Program Code:


def read_matrix(rows, cols):
    matrix = []
    for _ in range(rows):
        matrix.append(list(map(int, input().split())))
    return matrix

def transpose_matrix(matrix, rows, cols):
    return [[matrix[j][i] for j in range(rows)] for i in range(cols)]

def add_scalar(matrix, scalar):
    return [[matrix[i][j] + scalar for j in range(len(matrix[0]))] for i in range(len(matrix))]

r = int(input())  
c = int(input()) 
matrix = read_matrix(r, c)
s = int(input())  

transposed_matrix = transpose_matrix(matrix, r, c)

result_matrix = add_scalar(transposed_matrix, s)

for row in result_matrix:
    print(" ".join(map(str, row))) {codeBox}




Q3. Create a Python program that checks whether a given square matrix is symmetric. A matrix is symmetric if its transpose is equal to the matrix itself. The program should prompt the user to input the dimensions of the matrix and then input the matrix elements. The program should then determine whether the matrix is symmetric and print 1 if it is, otherwise print 0.

Input Format:
The first input is an integer r , the number of rows and columns in the matrix.
The next r lines each contain r integers, representing the elements of each row of the matrix.

Output Format:
The output is 1 if a matrix is symmetric, otherwise 0.

Example:

Input:

2
1 2
2 1

Output:

1

Program Code:


def read_matrix(n):
    matrix = []
    for _ in range(n):
        matrix.append(list(map(int, input().split())))
    return matrix

def is_symmetric(matrix, n):
    for i in range(n):
        for j in range(n):
            if matrix[i][j] != matrix[j][i]:
                return 0
    return 1

n = int(input()) 
matrix = read_matrix(n)

print(is_symmetric(matrix, n), end="") {codeBox}



Conclusion

If the Code didn't work, don't worry. Message us on telegram.

  Join Telegram 👉 CLICK HERE

One Comment Please !

Post a Comment (0)
Previous Post Next Post