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 389: 11.22

Answer

code

Work Step by Step

# 11.22 (Even number of 1s) Write a program that generates a two-dimensional # matrix filled with 0s and 1s, displays the matrix, and checks to see if every row # and every column has an even number of 1s. import random def main(): n = 6 matrix = [] # Initialize matrix and display it for i in range(n): matrix.append([]) for j in range(n): matrix[i].append(random.randint(0, 1)) print(matrix[i][j], end=" ") print() if isEvenParity(matrix): print("All rows and columns are even") else: print("Not all rows and columns are even") def isEvenParity(matrix): for i in range(len(matrix)): sum = 0 for j in range(len(matrix[i])): sum += matrix[i][j] if sum % 2 != 0: return False for j in range(len(matrix[0])): sum = 0 for i in range(len(matrix)): sum += matrix[i][j] if sum % 2 != 0: return False return True main()
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.