红宝石阵列

编程入门 行业动态 更新时间:2024-10-27 08:25:50
本文介绍了红宝石阵列 - 查找对角线的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

以前没见过这一个,但我不知道你如何找到在Ruby中二维数组的两条对角线的总和。假设你有一个简单的数组,以3行3列。

阵列= [1,2,3,4,5,6,7,8,9]

我可以通过分解成三个一组

array.each_sli​​ce(3).to_a

现在是

[1,2,3],[4,5,6],[7,8,9][1,2,3][4,5,6][7,8,9]

在这种情况下,对角线

1 + 5 + 9 = 153 + 5 + 7 = 15

所以,总和将 15 + 15 = 30

我想我可以做类似

diagonal_sum = 0因为我在0..2  在0..2Ĵ    diagonal_sum + =阵列[I] [J]。  结束结束

解决方案

下面是我的尝试:

阵列= [1,2,3,4,5,6,7,8,9]切片= array.each_sli​​ce(3).to_a#由于切片大小为3,我花了2,即3 - 1(0..2).MAP {| I |切片[I] [I]}#=> [1,5,9](0..2).MAP {| I |切片[Ⅰ] [ - I-1]}#=> [3,5,7](0..2).MAP {| I |切片[I] [I]}。降低+#=> 15(0..2).MAP {| I |切片[Ⅰ] [ - I-1]}。降低+#=> 15

根据以上的观察的似乎在一次迭代中,你可以做解决:

left_diagonal,right_diagoal =(0..2).each_with_object([],[])做| I,A |  一个[0]&下;&下;切片[I] [I]  一个[1];&下;切片[Ⅰ] [ - I-1]结束left_diagonal.reduce(+)#=> 15right_diagonal.reduce(+)#=> 15

添加,的 OOP 的code的风格:

类方阵  attr_reader:数组:为了  高清初始化数组,正    @array = array.each_sli​​ce(N).to_a    @order = N  结束  高清collect_both_diagonal_elements    (0 ...顺序).collect_concat {| I | [数组[我] [我],数组[我] [ - I-1]}  结束  高清collect_left_diagonal_elements    (0 ...顺序).collect {| I |数组[我] [我]}  结束  高清collect_right_diagonal_elements    (0 ...顺序).collect {| I |阵列[Ⅰ] [ - I-1]}  结束  高清sum_of_diagonal_elements类型    案件类型    时:所有然后collect_both_diagonal_elements.reduce(0,+)    当:那么好吧collect_right_diagonal_elements.reduce(0,+)    时:左,那么collect_left_diagonal_elements.reduce(0,+)    结束  结束结束阵列= [1,2,3,4,5,6,7,8,9]平方米= SquareMatrix.new阵,3sqm.collect_both_diagonal_elements#=> [1,3,5,5,9日,7]sqm.sum_of_diagonal_elements:所有#=>三十sqm.collect_left_diagonal_elements#=> [1,5,9]sqm.sum_of_diagonal_elements:左#=> 15sqm.collect_right_diagonal_elements#=> [3,5,7]sqm.sum_of_diagonal_elements:右#=> 15

Haven't seen this one before, but I was wondering how you can find the sums of both diagonals of a 2D array in Ruby. Say you have a simple array, with 3 rows and 3 columns.

array = [1,2,3,4,5,6,7,8,9]

I can break it into groups of three by using

array.each_slice(3).to_a

Would now be

[1,2,3], [4,5,6], [7,8,9] [1,2,3] [4,5,6] [7,8,9]

In this case, the diagonals are

1 + 5 + 9 = 15 3 + 5 + 7 = 15

So the total sum would be 15 + 15 = 30

I was thinking I could do something like

diagonal_sum = 0 for i in 0..2 for j in 0..2 diagonal_sum += array[i][j] end end

解决方案

Here is my try :

array = [1,2,3,4,5,6,7,8,9] sliced = array.each_slice(3).to_a # As sliced size is 3, I took 2, i.e. 3 - 1 (0..2).map { |i| sliced[i][i] } #=> [1, 5, 9] (0..2).map { |i| sliced[i][-i-1] } # => [3, 5, 7] (0..2).map { |i| sliced[i][i] }.reduce :+ # => 15 (0..2).map { |i| sliced[i][-i-1] }.reduce :+ # => 15

As per the above observation it seems in one iteration you can do solve :

left_diagonal, right_diagoal = (0..2).each_with_object([[], []]) do |i, a| a[0] << sliced[i][i] a[1] << sliced[i][-i-1] end left_diagonal.reduce(:+) # => 15 right_diagonal.reduce(:+) # => 15

Added, OOP style of code :

class SquareMatrix attr_reader :array, :order def initialize array, n @array = array.each_slice(n).to_a @order = n end def collect_both_diagonal_elements (0...order).collect_concat { |i| [ array[i][i], array[i][-i-1] ] } end def collect_left_diagonal_elements (0...order).collect { |i| array[i][i] } end def collect_right_diagonal_elements (0...order).collect { |i| array[i][-i-1] } end def sum_of_diagonal_elements type case type when :all then collect_both_diagonal_elements.reduce(0, :+) when :right then collect_right_diagonal_elements.reduce(0, :+) when :left then collect_left_diagonal_elements.reduce(0, :+) end end end array = [1,2,3,4,5,6,7,8,9] sqm = SquareMatrix.new array, 3 sqm.collect_both_diagonal_elements # => [1, 3, 5, 5, 9, 7] sqm.sum_of_diagonal_elements :all # => 30 sqm.collect_left_diagonal_elements # => [1, 5, 9] sqm.sum_of_diagonal_elements :left # => 15 sqm.collect_right_diagonal_elements # => [3, 5, 7] sqm.sum_of_diagonal_elements :right # => 15

更多推荐

红宝石阵列

本文发布于:2023-11-30 10:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649624.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:红宝石   阵列

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!