在2D数组中搜索零并创建相应的行和col 0

时间:2015-06-10 16:17:31

标签: arrays ruby logic

这是我的代码,它有效,但它太大了。我想重构它。

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

输入:2D数组

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

必需的输出:

1  2  0  4 
5  6  0  8
0  0  0  0
12 13 0  15  

5 个答案:

答案 0 :(得分:4)

我很惊讶Matrix课程的用法不多:

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]] 

注意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]] 

答案 1 :(得分:1)

试试这段代码:

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

答案 2 :(得分:1)

根据您问题的标题,这是搜索零值(fixate)位置的解决方案,然后实际将相应的行和列归零(clear,更多与你问题的内容一致):

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

快速测试:

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 ]
]                      # ]

答案 3 :(得分:0)

以下是我提出的建议:

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} }

首先,如果它们包含至少一个0,则使用map遍历列并使用零fill,但是,在这样做的同时,将包含0的索引添加到{{ 1}}数组。

然后,再次遍历数组并将每列的索引与zero_rows中的索引匹配为0.

您可能将zero_rows方法知道为map。他们做同样的事情。

旁注:

如果数组包含多个零,则此代码会将每个适用的列清零。 OP的示例和其他一些答案只会将找到0的第一列清零。如果这是您期望的行为,请参阅@ Doguita的回答。

答案 4 :(得分:0)

我不知道这段代码是否比其他答案更好。我稍后会测试一下:

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:这个答案假设只有一个0来搜索数组