Find zero in a 2D array and create the corresponding row and col 0

This is my code that works, but it is too big. I want to reorganize it.

req_row = -1 req_col = -1 a.each_with_index do |row, index| row.each_with_index do |col, i| if col == 0 req_row = index req_col = i break end end end if req_col > -1 and req_row > -1 a.each_with_index do |row,index| row.each_with_index do |col, i| print (req_row == index or i == req_col) ? 0 : col print " " end puts "\r" end end 

Input: 2D Array

 1 2 3 4 5 6 7 8 9 10 0 11 12 13 14 15 

Required Conclusion:

 1 2 0 4 5 6 0 8 0 0 0 0 12 13 0 15 
+6
source share
5 answers

I am surprised that the Matrix class is no longer used:

 a = [[ 1, 2, 3, 4], [ 5, 6, 7, 8], [ 9, 10, 0, 11], [12, 13, 14, 15]] require 'matrix' m = Matrix.rows(a) #=> Matrix[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 0, 11], [12, 13, 14, 15]] r, c = m.index(0) #=> [2, 2] Matrix.build(m.row_count, m.column_count) {|i,j| (i==r || j==c) ? 0 : m[i,j]}.to_a #=> [[ 1, 2, 0, 4], # [ 5, 6, 0, 8], # [ 0, 0, 0, 0], # [12, 13, 0, 15]] 

Note Matrix objects are immutable. To change individual elements, you must create a new matrix.

A slight modification is required if you want to do this for every zero in the matrix:

 a = [[ 1, 2, 3, 4], [ 5, 6, 7, 8], [ 9, 10, 0, 11], [ 0, 13, 14, 15]] require 'set' m = Matrix.rows(a) #=> Matrix[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 0, 11], [0, 13, 14, 15]] zero_rows = Set.new zero_columns = Set.new m.each_with_index { |e,i,j| (zero_rows << i; zero_columns << j) if e.zero? } zero_rows #=> #<Set: {2, 3}> zero_columns #=> #<Set: {2, 0}> Matrix.build(m.row_count, m.column_count) do |i,j| (zero_rows.include?(i) || zero_columns.include?(j)) ? 0 : m[i,j] end.to_a #=> [[0, 2, 0, 4], # [0, 6, 0, 8], # [0, 0, 0, 0], # [0, 0, 0, 0]] 
+4
source

Try this code:

 req_row = req_col = -1 a.each_with_index do |row, index| req_col = row.index(0) # searching index having value 0. if req_col req_row = index break end end a.each_with_index do |row,index| row.each_with_index do |col, i| print ((req_row == index or i == req_col) ? 0 : col).to_s + " " end puts "\r" end 
+1
source

Depending on the name of your question, here is a solution that looks for positions of zero values ​​( fixate ), then actually nulls the corresponding row and column ( clear , matches the contents of your question more)

 def fixate matrix, search=0, replace=0 rcs = [] matrix.each_with_index do |row,r| row.each_with_index do |col,c| rcs << [ r, c ] if col == search end end rcs.each do |(row, col)| clear matrix, row, col, replace end matrix end def clear matrix, row, col, val=0 matrix[row].map! { |_| val } # Clear rows matrix.each { |r| r[col] = val } # Clear columns matrix end 

Quick test:

 fixate [ # [ [ 1, 2, 3, 4 ], # [ 1, 2, 0, 4 ], [ 5, 6, 7, 8 ], # [ 5, 6, 0, 8 ], [ 9, 10, 0, 11 ], # [ 0, 0, 0, 0 ], [ 12, 13, 14, 15 ] # [ 12, 13, 0, 15 ] ] # ] 
+1
source

Here is what I came up with:

 zero_rows=[] a.map do |col| col.each_with_index do |el, index| zero_rows.push(index) if el==0 end col.fill(0) if col.include?(0) end a.map{|col| zero_rows.each{|index| col[index]=0} } 

First use map to repeat columns and fill with zeros if they contain at least one 0. but add indexes containing 0 to the zero_rows array.

Then, iterate over the array again and set the indices of each column corresponding to the index in zero_rows to 0.

Perhaps you know the map method as collect . They do the same thing.

Side note:

If the array contains multiple zeros, this code will be zero from each applicable column. The OP example and some other answers here will only be zero from the first column in which 0 will be found. If this is the behavior you expect, refer to @Doguita.

0
source

I don't know if this code is better than the other answers. I will check it later:

 ary = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 0, 11], [12, 13, 14, 15]] col = nil row = ary.index{ |a| col = a.index(0) } ary.each_with_index { |a, i| i == row ? a.fill(0) : a[col] = 0 } if col p ary # => [[1, 2, 0, 4], [5, 6, 0, 8], [0, 0, 0, 0], [12, 13, 0, 15]] 

Obs: This answer assumes there is only one 0 to search in the array

0
source

Source: https://habr.com/ru/post/988860/


All Articles