#### The Joy of Computing using Python Week 7 - Programming Assignment 1,2 and 3 | NPTEL | Answer with Explanation

## The Joy of Computing using Python Week 7: Programming Assignment 1

## Given a sqaure matrix M, write a function DiagCalc that calculates the sum of left and right diagonals and prints it respectively.

Input: A Number M A Matrix with M rows and M columns [[1,2,3],[3,4,5],[6,7,8]] Output 13 13 Explanation: Sum of left diagonal is 1+4+8 = 13 Sum of right diagonal is 3+4+6 = 13

## Answer

def DiagCalc(m): l=0 r=0 j=len(m)-1 for i in range(len(m)): l=l+m[i][i] r=r+m[i][j] j=j-1 print(l) print(r,end="") n = int(input()) M = [] for i in range(n): L = list(map(int, input().split())) M.append(L) DiagCalc(M)

## The Joy of Computing using Python Week 7: Programming Assignment 2

## Given a matrix M write a function Transpose which accepts a matrix M and return the transpose of M. Transpose of a matrix is a matrix in which each row is changed to a column or vice versa.

Input A matrix M [[1,2,3],[4,5,6],[7,8,9]] Output Transpose of M [[1,4,7],[2,5,8],[3,6,9]]

## Answer

def Transpose(N): M=[] for i in range(len(N)): k=[] for j in range(len(N[i])): k.append(N[j][i]) M.append(k) return M

## The Joy of Computing using Python Week 7: Programming Assignment 3

## Given a matrix M write a function snake that accepts a matrix M and returns a list which contain elements in snake pattern of matrix M. (See explanation to know what is a snake pattern)

Input A matrix M 91 59 21 63 81 39 56 8 28 43 61 58 51 82 45 57 Output [91, 59, 21, 63, 8, 56, 39, 81, 28, 43, 61, 58, 51, 82, 45, 57]

## Answer

def snake(M): s=[] for i in range(len(M)): if i%2==0: s+=M[i] else: s+=M[i][::-1] return(s)

Disclaimer:

"This page contains multiple choice questions (MCQs) related to The Joy of Computing using Python . The answers to these questions are provided for educational and informational purposes only.These answers are provided only for the purpose to help students to take references. This website does not claim any surety of 100% correct answers. So, this website urges you to complete your assignment yourself."

## 0 Comments