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.23

Answer

code

Work Step by Step

# 11.23 (Game: find the flipped cell) Suppose you are given a 6*6 matrix filled # with 0s and 1s. All rows and all columns have the even number of 1s. Let the # user flip one cell (i.e., flip from 1 to 0 or from 0 to 1) and write a program to # find which cell was flipped. Your program should prompt the user to enter a # 6*6two-dimensional list with 0s and 1s and find the first row r and first # column c where the even number of 1s property is violated. The flipped cell # is at (r, c). mat = [] print("Enter 6*6 matrix: ") for i in range(0, 6): x = input() mat.append(x.split()) transpose = [[row[i] for row in mat] for i in range(len(mat))] for i in range(len(mat)): row = mat[i] col = transpose[i] c1 = row.count('1') c2 = col.count('1') if c1 % 2 != 0: ind = row.index('1') if col.count('1')%2 == 0: print('The flipped cell at (', i, ',', ind, ')') break if c2 % 2 != 0: ind = col.index('1') if row.count('1')%2 == 0: print('The flipped cell at (', ind, ',', i, ')') break
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.