New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor transport
method by @cheezenaan
#22
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,5 @@ | ||
def transport(source) | ||
array = source.split("\n").map {|s| s.split(" ")} | ||
rows_count = array.first.count | ||
|
||
transported_array = [] | ||
0.upto(rows_count - 1) do |i| | ||
transported_array << array.map {|a| a[i]} | ||
end | ||
|
||
transported_array.map {|s| s.join(" ")}.join("\n") | ||
end | ||
def transpose(source) | ||
source_array = source.split("\n").map(&:split) | ||
transposed_array = source_array.transpose | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [IMO] 今回タケシさんがやりたいことは |
||
transposed_array.map { |nums| nums.join(" ") }.join("\n") | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,36 @@ | ||
require 'minitest/autorun' | ||
require './lib/transport' | ||
|
||
class TransportTest < MiniTest::Test | ||
def test_transport | ||
input = "1 2 3\n4 5 6\n7 8 9" | ||
output = "1 4 7\n2 5 8\n3 6 9" | ||
assert_equal output, transport(input) | ||
|
||
input = "1 2 3\n4 5 6\n7 8 9\n10 11 12" | ||
output = "1 4 7 10\n2 5 8 11\n3 6 9 12" | ||
assert_equal output, transport(input) | ||
class Transpose < MiniTest::Test | ||
def test_transpose | ||
input = <<~EOS | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [IMO] [IMO] # たとえば、こんなかんじです
text = <~~EOS
劇場版 響け!ユーフォニアム
〜北宇治高校吹奏楽部へようこそ〜
EOS |
||
1 2 3 | ||
4 5 6 | ||
7 8 9 | ||
EOS | ||
|
||
output = <<~EOS | ||
1 4 7 | ||
2 5 8 | ||
3 6 9 | ||
EOS | ||
|
||
# <~~ 記法だと末尾に改行文字(\n)が入るため #String#chomp で取り除く | ||
assert_equal output.chomp, transpose(input) | ||
|
||
input = <<~EOS | ||
1 2 3 | ||
4 5 6 | ||
7 8 9 | ||
10 11 12 | ||
EOS | ||
|
||
output = <<~EOS | ||
1 4 7 10 | ||
2 5 8 11 | ||
3 6 9 12 | ||
EOS | ||
|
||
assert_equal output.chomp, transpose(input) | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[IMO]
&
演算子を使うと、ブロックを使った処理をもう少しシュッと書けるようになります😄ref. https://qiita.com/kasei-san/items/0392097791d3a5998216