Created
August 1, 2014 02:26
-
-
Save kidach1/a84440c355c678192126 to your computer and use it in GitHub Desktop.
[Ruby] 二次元配列を列方向に足し合わせる(長さが異なる配列も可) ref: http://qiita.com/kidachi_/items/0e8b065b37d94c9c2362
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# [[1,1,1],[2,3],[4]] の場合、 | |
[1, 1 ,1] | |
[2, 3 ] | |
[4 ] | |
↓↓↓ 列(タテ)方向に足し合わせる | |
[7, 4, 1] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# [1,1,1],[2,3,4] のinputで、[3,4,5] のoutputが欲しい | |
[13] pry(main)> [[1,1,1],[2,3,4]].transpose.map{|ary| ary.inject(&:+)} | |
=> [3, 4, 5] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# [1,1,1],[2,3],[4] のinputで、[7,4,1] のoutputが欲しい | |
[14] pry(main)> [[1,1,1],[2,3],[4]].transpose.map{|ary| ary.inject(&:+)} | |
IndexError: element size differs (2 should be 3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[42] pry(main)> array = [[1,1,1],[2,3],[4]] | |
=> [[1, 1, 1], [2, 3], [4]] | |
[43] pry(main)> array.each_with_object([]) { |e, a| e.each_index { |i| a[i] = a[i].to_i + e[i] } } | |
=> [7, 4, 1] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Array | |
def safe_transpose | |
result = [] | |
max_size = self.max { |a,b| a.size <=> b.size }.size | |
max_size.times do |i| | |
result[i] = Array.new(self.first.size) | |
self.each_with_index { |r,j| result[i][j] = r[i] } | |
end | |
result | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[15] pry(main)> [[1,1,1],[2,3],[4]].safe_transpose.map{|ary|ary.inject(){|s,i|s=s+i.to_i}} | |
=> [7, 4, 1] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[16] pry(main)> [[1,1,1],[2,3],[4]].transpose | |
IndexError: element size differs (2 should be 3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Array | |
def safe_transpose | |
result = [] | |
max_size = self.max { |a,b| a.size <=> b.size }.size | |
max_size.times do |i| | |
result[i] = Array.new(self.first.size) | |
self.each_with_index { |r,j| result[i][j] = r[i] } | |
end | |
result | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[14] pry(main)> [[1,1,1],[2,3],[4]].safe_transpose | |
=> [[1, 2, 4], [1, 3, nil], [1, nil, nil]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[17] pry(main)> [[1,1,1],[2,3],[4]].safe_transpose.map{|ary|ary.inject(){|s,i|s=s+i.to_i}} | |
=> [7, 4, 1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment