NPTEL The Joy of Computing using Python Week 4 Programming Assignment Answers Solution | July 2024

 




Week 4: Programming Assignment 1

Due on 2024-08-22, 23:59 IST
Create a Python program that identifies the presence of a "saddle point" in a given matrix. A saddle point is defined as an element in the matrix that is the smallest in its row and the largest in its column. The matrix is represented as a nested list, where each nested list corresponds to a row of the matrix. The program should determine if at least one saddle point exists in the matrix. If a saddle point is found, the program/function should print 1; otherwise, it should print 0.

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

Output Format:
The output is 1 if a saddle point exists, otherwise 0.


Example:

Input:

3
3
2 0 3
2 1 4
4 2 6

Output:
1

Reason:
Element 2 in third row and second column is minimum in its row and maximum in its column.
So, it is a saddle point.

Your last recorded submission was on 2024-08-20, 20:11 IST
Select the Language for this assignment. 
r = int(input())
c = int(input())
A = [ ]
for i in range(r):
  row = [ ]
  for x in input().split(' '):
    row.append(int(x))
  A.append(row)
flag = 0
for i in range(r):
  if flag:
    break
  for j in range(c):
    element = A[i][j]
    if element == min(A[i]):
      if A[i].count(element) > 1:
        continue
      col = []
      for m in range(r):
        col += [A[m][j]]
      if element == max(col):
        flag = 1
        break
print(flag, end='')
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
3
3
2 2 3
2 1 4
4 0 6
0
0
Passed
Test Case 2
3
3
2 0 3
2 1 4
4 2 6
1
1
Passed
Test Case 3
4
4
7 3 5 8
4 2 9 1
2 2 10 11
14 1 7 3
1
1
Passed



Week 4: Programming Assignment 2

Due on 2024-08-22, 23:59 IST

Create a Python program that multiplies 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, multiply 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 multiplying the transpose of the original matrix by the scalar.


Example:

Input:

2
3
1 2 3
4 5 6
2

Output:
2 8
4 10
6 12

Your last recorded submission was on 2024-08-11, 19:19 IST
Select the Language for this assignment. 
r = int(input())
c = int(input())
A = [ ]
for i in range(r):
  row = [ ]
  for x in input().split(' '):
    row.append(int(x))
  A.append(row)
s = int(input())
T = [ ]
for j in range(c):
  row = [ ]
  for i in range(r):
    row.append(A[i][j])
  T.append(row)
for i in range(c):
  for j in range(r):
    if j == r-1:
      print(T[i][j]*s, end="")
    else :
      print(T[i][j]*s, end=" ")
  if (i,j)!=(c-1,r-1):
    print()
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
2
3
1 2 3
4 5 6
2
2 8\n
4 10\n
6 12
2 8\n
4 10\n
6 12
Passed
Test Case 2
3
2
1 2
3 4
5 6
3
3 9 15\n
6 12 18
3 9 15\n
6 12 18
Passed
Test Case 3
2
2
-1 0
2 -3
4
-4 8\n
0 -12
-4 8\n
0 -12
Passed



Week 4: Programming Assignment 3

Due on 2024-08-22, 23:59 IST
Create a Python program that checks whether a given square matrix is skew-symmetric. A matrix is skew-symmetric if its transpose is equal to the negative of the matrix itself, i.e =  .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 skew-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 skew-symmetric, otherwise 0.


Example:

Input:

3
0 2 -1
-2 0 -4
1 4 0

Output:
1

Your last recorded submission was on 2024-08-11, 19:22 IST
Select the Language for this assignment. 
r = int(input())
A = [ ]
for i in range(r):
  row = [ ]
  for x in input().split(' '):
    row.append(int(x))
  A.append(row)
T = [ ]
for j in range(r):
  row = [ ]
  for i in range(r):
    row.append(A[i][j])
  T.append(row)
flag = 0
for i in range(r):
  for j in range(r):
    if A[i][j] == -1 * T[i][j]:
      continue
    else :
      print(0, end='')
      flag = 1
      break
  if flag :
    break
else:
  print(1, end='')
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
3
0 2 -1
-2 0 -4
1 4 0
1
1
Passed
Test Case 2
3
0 2 -1
-2 0 4
1 4 0
0
0
Passed
Test Case 3
4
0 2 -3 4
-2 0 5 -6
3 -5 0 7
-4 6 -7 0
1
1
Passed



No comments

VR23 Web Technologies Lab MCA II Year I Semester Experiment 7:

  Aim: PHP script to a) Find the length of a string. b) Count no of words in a string. c) Reverse a string. d) Search for a specific string....