From e078249231d972c5e8fa8bd9b2e54d9f0a191000 Mon Sep 17 00:00:00 2001 From: Leon Date: Mon, 27 Nov 2017 22:41:39 +0900 Subject: [PATCH 1/9] =?UTF-8?q?=E6=9C=AC=E4=BD=93=E3=81=AE=E3=83=AA?= =?UTF-8?q?=E3=83=95=E3=82=A1=E3=82=AF=E3=82=BF=E3=83=AA=E3=83=B3=E3=82=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Array.transpose を使う * array という変数名は Array(クラス名)と紛らわしいので変更 * より意味が伝わりやすいよう、split(¥n) から each_line に変更 --- lib/transport.rb | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/lib/transport.rb b/lib/transport.rb index d930638..b870c37 100644 --- a/lib/transport.rb +++ b/lib/transport.rb @@ -1,11 +1,7 @@ def transport(source) - array = source.split("\n").map {|s| s.split(" ")} - rows_count = array.first.count + org_array = source.each_line.map {|s| s.split(" ")} - transported_array = [] - 0.upto(rows_count - 1) do |i| - transported_array << array.map {|a| a[i]} - end + transported_array = org_array.transpose transported_array.map {|s| s.join(" ")}.join("\n") end \ No newline at end of file From 8afdc8ac25deea89ab1a015eaaff826aed6d32d6 Mon Sep 17 00:00:00 2001 From: Leon Date: Mon, 27 Nov 2017 22:43:49 +0900 Subject: [PATCH 2/9] =?UTF-8?q?=E3=83=86=E3=82=B9=E3=83=88=E3=82=B3?= =?UTF-8?q?=E3=83=BC=E3=83=89=E3=81=AE=E3=83=AA=E3=83=95=E3=82=A1=E3=82=AF?= =?UTF-8?q?=E3=82=BF=E3=83=AA=E3=83=B3=E3=82=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * input/output行列の形・内容がひと目で分かるようヒアドキュメントを使う --- test/transport_test.rb | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/test/transport_test.rb b/test/transport_test.rb index 64126c8..ccdbe7f 100644 --- a/test/transport_test.rb +++ b/test/transport_test.rb @@ -3,12 +3,27 @@ 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 = <<~EOS.chomp + 1 2 3 + 4 5 6 + 7 8 9 + EOS + assert_equal <<~EOS.chomp, transport(input) + 1 4 7 + 2 5 8 + 3 6 9 + EOS - 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) + input = <<~EOS.chomp + 1 2 3 + 4 5 6 + 7 8 9 + 10 11 12 + EOS + assert_equal <<~EOS.chomp, transport(input) + 1 4 7 10 + 2 5 8 11 + 3 6 9 12 + EOS end end \ No newline at end of file From 384c5d8aa57bb33be7e53eb2ee58e5d49103f549 Mon Sep 17 00:00:00 2001 From: Leon Date: Tue, 5 Dec 2017 17:59:59 +0900 Subject: [PATCH 3/9] =?UTF-8?q?=E6=9C=AC=E4=BD=93=E3=81=AE=E3=83=AA?= =?UTF-8?q?=E3=83=95=E3=82=A1=E3=82=AF=E3=82=BF=E3=83=AA=E3=83=B3=E3=82=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * メソッドチェーン化 --- lib/transport.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/transport.rb b/lib/transport.rb index b870c37..98a58f3 100644 --- a/lib/transport.rb +++ b/lib/transport.rb @@ -1,7 +1,8 @@ def transport(source) - org_array = source.each_line.map {|s| s.split(" ")} - - transported_array = org_array.transpose - - transported_array.map {|s| s.join(" ")}.join("\n") + source + .each_line + .map {|s| s.split(" ")} + .transpose + .map {|s| s.join(" ")} + .join("\n") end \ No newline at end of file From 98e82faee79cd7099e3ee82f706a636cee8a63a3 Mon Sep 17 00:00:00 2001 From: Leon Date: Tue, 5 Dec 2017 20:45:37 +0900 Subject: [PATCH 4/9] =?UTF-8?q?[=E3=83=86=E3=82=B9=E3=83=88=E3=82=B3?= =?UTF-8?q?=E3=83=BC=E3=83=89]=20=E3=83=92=E3=82=A2=E3=83=89=E3=82=AD?= =?UTF-8?q?=E3=83=A5=E3=83=A1=E3=83=B3=E3=83=88=E3=81=8C=E4=B9=B1=E7=94=A8?= =?UTF-8?q?=E6=B0=97=E5=91=B3=E3=81=AA=E3=81=AE=E3=81=A7=E3=82=82=E3=81=86?= =?UTF-8?q?=E3=81=A1=E3=82=87=E3=81=84=E6=84=8F=E5=9B=B3=E3=81=8C=E4=BC=9D?= =?UTF-8?q?=E3=82=8F=E3=82=8A=E3=82=84=E3=81=99=E3=81=84=E3=82=88=E3=81=86?= =?UTF-8?q?=E3=81=AB=E8=A8=98=E8=BF=B0=E3=81=99=E3=82=8B=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/transport_test.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test/transport_test.rb b/test/transport_test.rb index ccdbe7f..3c9f24b 100644 --- a/test/transport_test.rb +++ b/test/transport_test.rb @@ -8,11 +8,12 @@ def test_transport 4 5 6 7 8 9 EOS - assert_equal <<~EOS.chomp, transport(input) + expect = <<~EOS.chomp 1 4 7 2 5 8 3 6 9 EOS + assert_equal expect, transport(input) input = <<~EOS.chomp 1 2 3 @@ -20,10 +21,13 @@ def test_transport 7 8 9 10 11 12 EOS - assert_equal <<~EOS.chomp, transport(input) + expect = <<~EOS.chomp 1 4 7 10 2 5 8 11 3 6 9 12 EOS + assert_equal expect, transport(input) + + # assert_equal "1 4\n2 5\n3 6", transport("1 2\r\n3 4\r5 6") end end \ No newline at end of file From b2c6d86cb396ad7571fa3c92b31e927fd4650b64 Mon Sep 17 00:00:00 2001 From: Leon Date: Tue, 5 Dec 2017 20:48:19 +0900 Subject: [PATCH 5/9] =?UTF-8?q?&:=20=E3=82=92=E4=BD=BF=E3=81=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/transport.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/transport.rb b/lib/transport.rb index 98a58f3..55f91bb 100644 --- a/lib/transport.rb +++ b/lib/transport.rb @@ -1,8 +1,11 @@ +# 文字列で表現された行列を転置して、文字列形式で返す +# 入力例: "1 2 3\n4 5 6\n7 8 9\n10 11 12" +# 出力例: "1 4 7 10\n2 5 8 11\n3 6 9 12" def transport(source) source .each_line - .map {|s| s.split(" ")} + .map(&:split) .transpose - .map {|s| s.join(" ")} + .map {|ary| ary.join(" ")} .join("\n") end \ No newline at end of file From 3aa49836cb5a06e9a62bb7d7173e48ae52ebf815 Mon Sep 17 00:00:00 2001 From: Leon Date: Tue, 5 Dec 2017 20:50:30 +0900 Subject: [PATCH 6/9] =?UTF-8?q?split=E5=91=A8=E3=82=8A=E3=80=81=E3=81=A1?= =?UTF-8?q?=E3=82=87=E3=81=A3=E3=81=A8=E3=82=84=E3=82=8A=E9=81=8E=E3=81=8E?= =?UTF-8?q?=E3=81=9F=E6=84=9F=E3=81=8C=E3=81=82=E3=82=8B=E3=81=AE=E3=81=A7?= =?UTF-8?q?=E5=85=83=E3=81=AB=E6=88=BB=E3=81=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/transport.rb | 10 +++++----- test/transport_test.rb | 2 -- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/lib/transport.rb b/lib/transport.rb index 55f91bb..8af138e 100644 --- a/lib/transport.rb +++ b/lib/transport.rb @@ -1,11 +1,11 @@ # 文字列で表現された行列を転置して、文字列形式で返す -# 入力例: "1 2 3\n4 5 6\n7 8 9\n10 11 12" -# 出力例: "1 4 7 10\n2 5 8 11\n3 6 9 12" +# ex) 入力: "1 2 3\n4 5 6\n7 8 9\n10 11 12" +# 出力: "1 4 7 10\n2 5 8 11\n3 6 9 12" def transport(source) source - .each_line - .map(&:split) + .split("\n") + .map {|s| s.split(" ")} .transpose - .map {|ary| ary.join(" ")} + .map {|a| a.join(" ")} .join("\n") end \ No newline at end of file diff --git a/test/transport_test.rb b/test/transport_test.rb index 3c9f24b..22af2ce 100644 --- a/test/transport_test.rb +++ b/test/transport_test.rb @@ -27,7 +27,5 @@ def test_transport 3 6 9 12 EOS assert_equal expect, transport(input) - - # assert_equal "1 4\n2 5\n3 6", transport("1 2\r\n3 4\r5 6") end end \ No newline at end of file From 6eef2f1cf50b2019979257b0685ae3946ba7723d Mon Sep 17 00:00:00 2001 From: Leon Date: Tue, 5 Dec 2017 21:33:04 +0900 Subject: [PATCH 7/9] =?UTF-8?q?[=E3=83=86=E3=82=B9=E3=83=88=E3=82=B3?= =?UTF-8?q?=E3=83=BC=E3=83=89]=20=E3=83=92=E3=82=A2=E3=83=89=E3=82=AD?= =?UTF-8?q?=E3=83=A5=E3=83=A1=E3=83=B3=E3=83=88=E3=81=A7=E4=BD=9C=E3=81=A3?= =?UTF-8?q?=E3=81=9F=E6=96=87=E5=AD=97=E5=88=97=E3=81=A8=E6=99=AE=E9=80=9A?= =?UTF-8?q?=E3=81=AE=E6=96=87=E5=AD=97=E5=88=97=E3=81=8C=E7=AD=89=E3=81=97?= =?UTF-8?q?=E3=81=84=E3=81=93=E3=81=A8=E3=82=92=E7=A2=BA=E8=AA=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/transport_test.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/transport_test.rb b/test/transport_test.rb index 22af2ce..57261f4 100644 --- a/test/transport_test.rb +++ b/test/transport_test.rb @@ -28,4 +28,14 @@ def test_transport EOS assert_equal expect, transport(input) end + + def test_compare_heredoc_to_string + literal = "1 2 3\n4 5 6\n7 8 9" + heredoc = <<~EOS.chomp + 1 2 3 + 4 5 6 + 7 8 9 + EOS + assert_equal heredoc, literal + end end \ No newline at end of file From 6b52f7d48be5f68defd0f64946f14b6cee910d11 Mon Sep 17 00:00:00 2001 From: Leon Date: Tue, 5 Dec 2017 22:00:32 +0900 Subject: [PATCH 8/9] =?UTF-8?q?Revert=20"[=E3=83=86=E3=82=B9=E3=83=88?= =?UTF-8?q?=E3=82=B3=E3=83=BC=E3=83=89]=20=E3=83=92=E3=82=A2=E3=83=89?= =?UTF-8?q?=E3=82=AD=E3=83=A5=E3=83=A1=E3=83=B3=E3=83=88=E3=81=A7=E4=BD=9C?= =?UTF-8?q?=E3=81=A3=E3=81=9F=E6=96=87=E5=AD=97=E5=88=97=E3=81=A8=E6=99=AE?= =?UTF-8?q?=E9=80=9A=E3=81=AE=E6=96=87=E5=AD=97=E5=88=97=E3=81=8C=E7=AD=89?= =?UTF-8?q?=E3=81=97=E3=81=84=E3=81=93=E3=81=A8=E3=82=92=E7=A2=BA=E8=AA=8D?= =?UTF-8?q?"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 6eef2f1cf50b2019979257b0685ae3946ba7723d. --- test/transport_test.rb | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/test/transport_test.rb b/test/transport_test.rb index 57261f4..e0183cb 100644 --- a/test/transport_test.rb +++ b/test/transport_test.rb @@ -26,16 +26,5 @@ def test_transport 2 5 8 11 3 6 9 12 EOS - assert_equal expect, transport(input) - end - - def test_compare_heredoc_to_string - literal = "1 2 3\n4 5 6\n7 8 9" - heredoc = <<~EOS.chomp - 1 2 3 - 4 5 6 - 7 8 9 - EOS - assert_equal heredoc, literal end end \ No newline at end of file From f084df890af21640f97c80f6dfe57997ff11a352 Mon Sep 17 00:00:00 2001 From: Leon Date: Thu, 7 Dec 2017 21:11:36 +0900 Subject: [PATCH 9/9] =?UTF-8?q?[=E3=83=86=E3=82=B9=E3=83=88=E3=82=B3?= =?UTF-8?q?=E3=83=BC=E3=83=89]=20=E5=89=8D=E5=9B=9E=E3=81=AECommit?= =?UTF-8?q?=E3=81=A7assert=E3=82=92=E5=89=8A=E9=99=A4=E3=81=97=E3=81=A6?= =?UTF-8?q?=E3=81=97=E3=81=BE=E3=81=A3=E3=81=A6=E3=81=84=E3=81=9F=E3=81=AE?= =?UTF-8?q?=E3=81=A7=E3=80=81=E3=81=9D=E3=81=A3=E3=81=A8=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/transport_test.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/test/transport_test.rb b/test/transport_test.rb index e0183cb..22af2ce 100644 --- a/test/transport_test.rb +++ b/test/transport_test.rb @@ -26,5 +26,6 @@ def test_transport 2 5 8 11 3 6 9 12 EOS + assert_equal expect, transport(input) end end \ No newline at end of file