Programming assignment - Week 4

Programming assignment - Week 4 

Solutions

Note: There could be optimised ways to solve the problem, but the goal here is to solve it effectively. The most intuitive solution has been provided for clarity.

Programming assignment - 1

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.

Code:

def solve(matrix, r, c):
    for i in range(r):
        min_row_val = min(matrix[i])
        min_row_idx = matrix[i].index(min_row_val)
        is_saddle_point = True
        for j in range(r):
            if matrix[j][min_row_idx] > min_row_val:
                is_saddle_point = False
                break
        
        if is_saddle_point:
            print(1,end="")
            return
    print(0,end="")

r = int(input())
c = int(input())

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

solve(matrix, r, c)

Programming assignment - 2

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

Code:

def transpose_matrix(matrix, r, c):
    transposed = []
    for j in range(c):
        new_row = []
        for i in range(r):
            new_row.append(matrix[i][j])
        transposed.append(new_row)
    return transposed

def multiply_by_scalar(matrix, scalar):
    result = []
    for row in matrix:
        new_row = []
        for element in row:
            new_row.append(scalar * element)
        result.append(new_row)
    return result

def print_matrix(matrix):
    for row in matrix:
        print(" ".join(map(str, row)))

r = int(input())
c = int(input())

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

scalar = int(input())

transposed_matrix = transpose_matrix(matrix, r, c)
result_matrix = multiply_by_scalar(transposed_matrix, scalar)
print_matrix(result_matrix)

Programming assignment - 3

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 AT= −A.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

Code:

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

r = int(input())

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

print(is_skew_symmetric(matrix, r),end="")
Do share this with your friends !! Comment the queries, I will reach out to you.
Week 5 assignment solution coming soon ☺......

Comments

Popular posts from this blog

The Joy of Computing using python -Week 1