【问题标题】:Trying to understand how to iterate over more-dim Arrays试图了解如何迭代更暗淡的数组
【发布时间】:2015-06-25 08:53:31
【问题描述】:

我正在尝试学习如何迭代数组,因此我自己编写了一些场景来练习。

假设我给定的矩阵是二维的,因此是二维的。大批。 mat =[[1,2,300,-400],[0,3,-1,9],[3,4,-5,1]]

任务 1) 返回值总和最大的数组。 任务 2)假设这个 Array 可以产生一个 nxm 矩阵,返回封闭数字之和最高的行和列的值。 为了更容易理解,让我们在这里使用不同的矩阵。 垫= [[1,1,1,1],[2,2,2,2],[3,3,3,3],[4,4,4,4]] 所以它看起来像这样:

1111

2222

3333

4444

结果将是 [2,1] 或 [2,2] 因为这些数字的总和 (2+2+2+3+3+4+4+4) = 24 是最高的。

到目前为止,这是我的实现:

任务 1) 我只能通过在类 Array 中添加 sum 函数来解决这个问题。

def max_row(mat)
  return mat.max{|a,b| a.sum <=> b.sum }
end

class Array
  def sum
    sum = 0
    self.each(){|x|
      sum += x
    }
    return sum
  end
end

我确实想在不使用额外方法的情况下解决它,但我不知道该怎么做。 到目前为止我的想法:

def max_row(mat)
  sum_ary = []
mat.each(){|ary|
  sum = 0
  ary.each(){|x|
    sum += x
  }
  sum_ary << [sum]
}

我在 sum_ary 上尝试了 find_index,但在实现时它返回第一个不为 false 的值,因此我无法使用它来搜索最大值。

实施任务 2): mat = [[1,1,1,1],[2,2,2,2],[3,3,3,3],[4,4,4,4]]

def max_neighbor_sum(mat)
  sum_result = []
  for  n in 0...mat.size()
    for m in 0...mat.size()
      sum = 0
      for a in (n-1)..(n+1)
        for b in (m-1)..(m+1)
          if m != nil && n !=nil && a>=0 && b>=0 && a<= (mat.size()-1)
#            print "n:#{n} m:#{m} a:#{a} b:#{b} \n"
#            p mat[a][b]
if mat[a][b] !=nil && !(n==a && m==b)
    sum += mat[a][b]
end
  end
end
          end
          sum_result << sum
# p sum_result

    end
  end
  return sum_result
end

我正确计算了所有的总和,但现在不知道如何获取行和列的索引。

希望你能理解我在哪里需要帮助。

【问题讨论】:

  • 旁注:您的Array#sum 方法可能会像reduce(&amp;:+) 一样简单地重写。试试:[1,2,3,4].reduce &amp;:+.
  • 谢谢,我会检查一下以了解语法。
  • 我不确定我的@yourname 是否有效,因此请查看我的最新评论以获取以下答案吗?

标签: arrays ruby matrix


【解决方案1】:

问题一:

arrays.map(&:sum).max

为每个数组调用 sum,然后选择其中最大的一个

问题 2 不能这么容易解决,但这应该可行:

max_sum = 0
max_index = []

for n in 0...mat.size
  for m in 0...mat.size
    sum = 0
    for a in (n-1)..(n+1)
      for b in (m-1)..(m+1)
        sum += mat[a][b] unless mat[a].nil? || mat[a][b].nil?
      end
    end
    if sum > max_sum
      max_sum = sum
      max_index = [n,m]
    end
  end
end

max_sum # => maximum sum of all neighbours
max_index # => a pair of indexes which have the max sum

如果您想保留所有最大索引,只需将其替换为对数组并在 sum 等于 max_sum 时推送。

【讨论】:

  • 非常感谢您的快速帮助,我会在周末研究这个问题。
  • 我在你的代码前面放了一个 def 和 end,然后在给定的矩阵 [[1,1,1,1],[2,2,2,2],[ 3,3,3,3],[4,4,4,4]] & 使用返回 max_index。在末尾。结果:[3,3],所以它没有按照预期的方式工作。
  • 好吧,我发现了问题:你写了 sum = max_sum 它必须是 max_sum = sum 才可以工作:) 不..不知何故它仍然是错误的。但你真的帮我解决了!
  • 抱歉打错字了,很高兴能帮上忙 :)
  • 好的,我已经重写了我的 sum 方法,所以它适用于对象。 def sum(ary) return ary.reduce(:+) end @mudasobwa 我在结果文档中没有找到 &,你为什么用它?那么我该如何重新编写任务 1 的解决方案呢?我不知道如何处理迭代的当前元素。
【解决方案2】:

这是我对任务 2 的解决方案,感谢 Piotr Kruczek。 感谢您的热心帮助!

def max_neighbour_sum(mat)
  sum_result = []
  max_sum = 0
  for  n in 0...mat.size()
    for m in 0...mat.size()
      sum = 0
      for a in (n-1)..(n+1)
        for b in (m-1)..(m+1)
          if m != nil && n !=nil && a>=0 && b>=0 && a<= (mat.size()-1)
            #            print "n:#{n} m:#{m} a:#{a} b:#{b} \n"
            #            p mat[a][b]
            if mat[a][b] !=nil && !(n==a && m==b)
              sum += mat[a][b]
            end
          end
        end
      end
      if sum > max_sum
         max_sum = sum
        sum_result = [n,m]
      end
      # p sum_result
    end
  end
  return sum_result
end

【讨论】:

    猜你喜欢
    • 2017-12-03
    • 1970-01-01
    • 2015-11-07
    • 1970-01-01
    • 2021-05-06
    • 1970-01-01
    • 2021-04-01
    • 2021-01-29
    • 1970-01-01
    相关资源
    最近更新 更多