NPTEL Joy Of Computing Using Python Week 7 Programming Assignment 2025

This Article describes solutions to the NPTEL Joy Of Computing Using Python Week 7 Programming Assignment 2025. The Code passed all the public and private test cases. The link is provided below if you didn't complete the Week 6 programming assignment of the joy of computing.

Also Read: NPTEL Joy of Computing Using Python Week 6 Programming Assignment Answers

Upcoming answers are provided in Telegram faster

Join us on Telegram 👉 CLICK HERE 

NPTEL Joy Of Computing Using Python Week 7 Programming Assignment 2025

NPTEL Joy Of Computing Using Python Week 7 Programming Assignment 2025

In this Week 7, the following topics are discussed: 

  • Snake and Ladders - Not on the board
  • Spiral Traversing - Let's animate
  • GPS - Track the route
Last Date: 13-03-2025

Q1. Magic Square

A magic square of order n is a square arrangement of n^2 numbers, typically distinct integers, where the sum of the n numbers in each row, each column, and both main diagonals is the same constant. It consists of the integers from 1 to.

The constant sum of each row, column, and diagonal in a magic square is known as the magic constant or magic sum, M. For a normal magic square, this constant depends solely on n and is given by the formula:

M=(n(n^2+1))/2

For normal magic squares of order n = 3,4,5,...,

the magic constants are: 15,34,65,111,175,260,...

Given a matrix, check whether it’s Magic Square or not.

Input Format:
  • An integer value for n (n>2)
  • An nXn matrix

Output Format:
If the given matrix is magic square then print “Magic Matrix”, otherwise “Not a Magic Matrix

Example :
Input
3
2 7 6
9 5 1
4 3 8

Output
Magic Matrix

Program Code: 

def is_magic_square(matrix, n):
    magic_sum = n * (n**2 + 1) // 2

    for row in matrix:
        if sum(row) != magic_sum:
            return "Not a Magic Matrix"

    for col in range(n):
        if sum(matrix[row][col] for row in range(n)) != magic_sum:
            return "Not a Magic Matrix"

    if sum(matrix[i][i] for i in range(n)) != magic_sum:
        return "Not a Magic Matrix"

    if sum(matrix[i][n - 1 - i] for i in range(n)) != magic_sum:
        return "Not a Magic Matrix"

    return "Magic Matrix"
  
n = int(input())
matrix = [list(map(int, input().split())) for _ in range(n)]
print(is_magic_square(matrix, n), end='') {codeBox}

Explanation:

  1. Calculate the magic sum using the formula 𝑀 = 𝑛(𝑛^2 + 1) / 2. 
  2. Check each row’s sum and return "Not a Magic Matrix" if any row sum differs from magic_sum.
  3. Check each column’s sum similarly.
  4. Check both diagonals' sums and verify they match magic_sum.
  5. If all conditions are met, return "Magic Matrix".

Q2.  Median of Matrix

You are given a row-wise sorted matrix of size n×m, where both the number of rows and the number of columns are always odd. Your task is to find the median of the matrix. The median is the middle number in a sorted list of numbers. In case the list has an even number of elements, the median is the leftmost of the two middle elements.

The matrix is sorted row-wise, but the entire matrix is not necessarily sorted.
You need to find the median from the matrix when all the elements are sorted in ascending order.

Input Format:

  • The first line contains two integers n and m, representing the number of rows and columns in the matrix.
  • The next n lines each contain m space-separated integers representing a row of the matrix.

Output Format:

Print the median of the matrix.

Constraints:
3 ≤ n, m ≤ 100
n and m are always odd.

Example :
Input:
3 3
1 3 5
2 6 9
3 6 9
Output:
5

Explanation:
The elements in sorted order: [1,2,3,3,5,6,6,9,9].
The total number of elements is 9, so the median is the element at 5th location, which is 5.

Program Code:

def find_median(matrix, n, m):
    sorted_elements = sorted([num for row in matrix for num in row])

    median_index = (n * m) // 2  # Since n * m is always odd, this gives the middle index

    return sorted_elements[median_index]

n, m = map(int, input().split())
matrix = [list(map(int, input().split())) for _ in range(n)]

print(find_median(matrix, n, m),end='') {codeBox}

Explanation:

  1. Flatten the matrix into a list.
  2. Sort the list to get all elements in ascending order.
  3. Find the middle element using the formula (n * m) // 2 since the total number of elements is always odd.
  4. Return the median.

Q3. 
       Rotate a Rectangular Image by 90 Degrees Clockwise

You are given an image represented by an m x n matrix. Your task is to rotate the image by 90 degrees in the clockwise direction. After rotation, the dimensions of the result matrix will be n x m, as the number of rows and columns will be swapped.

Rotate a Rectangular Image by 90 Degrees Clockwise


Input Format
  • Two integers m and n
  • A matrix of integers of dimension m X n

Output Format
  • A matrix of integers of dimension n X m

Example :

Input:

4 4

 1   2   3   4
 5   6   7   8
 9  10 11 12
13 14 15 16

Output:

13   9   5    1
14  10   6   2
15  11   7   3
16  12   8   4

Program Code: 

def rotate_matrix(matrix, m, n):
    rotated = [[matrix[m - 1 - row][col] for row in range(m)] for col in range(n)]

    for row in rotated:
        print(*row)

m, n = map(int, input().split())
matrix = [list(map(int, input().split())) for _ in range(m)]

rotate_matrix(matrix, m, n) {codeBox}

Explanation:


1. Rotating a matrix by 90° clockwise means:
  • The first column of the original matrix becomes the first row of the rotated matrix.
  • The second column of the original matrix becomes the second row of the rotated matrix, and so on.
 2. Implementation steps:
  • Create a new matrix of size 𝑛×𝑚.
  • Populate the new matrix such that:
      The first row of the new matrix is the last column of the original matrix.
      The second row of the new matrix is the second-last column of the original matrix, and so on
  • Print the rotated matrix.

Conclusion:

Join us on Telegram 👉 CLICK HERE

One Comment Please !

Post a Comment (0)
Previous Post Next Post