Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Set Matrix Zero.py #12232

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions data_structures/arrays/Set Matrix Zero.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
def setMatrixZeroes(matrix):

Check failure on line 1 in data_structures/arrays/Set Matrix Zero.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N999)

data_structures/arrays/Set Matrix Zero.py:1:1: N999 Invalid module name: 'Set Matrix Zero'

Check failure on line 1 in data_structures/arrays/Set Matrix Zero.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N802)

data_structures/arrays/Set Matrix Zero.py:1:5: N802 Function name `setMatrixZeroes` should be lowercase
n = len(matrix)
m = len(matrix[0])

# To store which rows and columns are supposed to be marked with zeroes
row = [0] * n
col = [0] * m

# Traverse the matrix using nested loops
for i in range(n):
for j in range(m):
# If the cell contains zero, mark its row and column
if matrix[i][j] == 0:
row[i] = 1
col[j] = 1

# Update the matrix
for i in range(n):
for j in range(m):
# Set cells to zero if any of the row[i] or col[j] is marked
if row[i] or col[j]:
matrix[i][j] = 0

# Print the updated matrix
for row in matrix:
print(" ".join(map(str, row)))


# Driver Code
n = int(input("Enter number of rows: "))
m = int(input("Enter number of columns: "))

# Initialize matrix from user input
matrix = []
print("Enter the elements row-wise (space-separated):")
for i in range(n):

Check failure on line 36 in data_structures/arrays/Set Matrix Zero.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (B007)

data_structures/arrays/Set Matrix Zero.py:36:5: B007 Loop control variable `i` not used within loop body
row = list(map(int, input().split()))
matrix.append(row)

# Function call
setMatrixZeroes(matrix)
Loading