2015-06-25 26 views
0

我想學習如何遍歷數組,因此構成了我自己的場景來練習。試圖瞭解如何遍歷更多昏暗的數組

假設我給定的矩陣是一個二維的,因此是一個二維的。陣列。 墊= [[1,2,300,-400],[0,3,-1,9-],[3,4,-5,1]]

任務1)返回與最高總和的陣列價值。 任務2)假設這個數組可以產生一個nxm矩陣,返回包含數字的和的最高的行和列的值。 爲了更容易理解,讓我們在這裏使用不同的矩陣。 mat = [[1,1,1,1],[2,2,2,2],[3,3,3,3],[4,4,4,4]] 所以它看起來像此:

而結果將是[2,1]或由於用於這些數字的總和[2,2] ( 2 + 2 + 2 + 3 + 3 + 4 + 4 + 4)= 24將是最高的。

這裏是到目前爲止我實現:

任務1) 我只能用增加一個求和函數的類Array解決這個問題。

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,但實現它返回的第一個值是不假,所以我不能用它來搜索最大的價值。

執行任務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 

我正確計算了所有的總和,但不知道如何獲得行和列的索引。

我希望你能理解我需要幫助的地方。

+0

_Sideote:_你的'Array#sum'方法可能會被重寫爲'reduce(&:+)'。試試:'[1,2,3,4] .reduce&:+'。 – mudasobwa

+0

謝謝,我會檢查這個瞭解語法。 – InDaPond

+0

我不確定我的@你的姓名是否有效,因此請你在下面的答案中查看我的最新評論? – InDaPond

回答

1

問題1:

arrays.map(&:sum).max 

呼籲總和每個陣列,然後選擇其中最大的

問題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時推送。

+0

非常感謝您的快速幫助,我會在週末期間對此進行調查。 – InDaPond

+0

我在你的代碼前面放了一個def並結束,然後在給定的矩陣[[1,1,1,1],[2,2,2,2],[3,3,3,3 ],[4,4,4,4]]&used return max_index。最後。結果:[3,3],所以它不按預期的方式工作。 – InDaPond

+0

好吧,我發現問題: 你worte sum = max_sum 它必須是max_sum =總和 然後它的工作:) 沒有..多少它仍然是錯誤的。但你真的幫我解決了它! – InDaPond

0

這是我對任務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