Introduction to Programming using Python 1st Edition

Published by Pearson
ISBN 10: 0132747189
ISBN 13: 978-0-13274-718-9

Chapter 11 - Multidimensional Lists - Programming Exercises - Page 390: 11.25

Answer

code

Work Step by Step

# 11.25 (Markov matrix) An n*n matrix is called a positive Markov matrix if each element # is positive and the sum of the elements in each column is 1. Write the following # function to check whether a matrix is a Markov matrix: # def isMarkovMatrix(m): # Write a test program that prompts the user to enter a 3*3 matrix of numbers and # tests whether it is a Markov matrix. def isMarkovMatrix(m): for row in m: for e in row: if e < 0: return False if sum(row) != 1: return False return True mat = [] print('Enter a 3-by-3 matrix row by row:') for i in range(3): x = input().split() x = [eval(r) for r in x] mat.append(x) transpose = [[row[i] for row in mat] for i in range(len(mat))] print(isMarkovMatrix(transpose))
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.