From 4f10043ee9311a23fdfe6169ac5fbc2b992b1b78 Mon Sep 17 00:00:00 2001 From: Megumi Mochizuki Date: Wed, 6 Dec 2017 01:36:42 +0900 Subject: [PATCH] refactor. --- lib/transport.rb | 31 ++++++++++++++++++++++++++----- test/transport_test.rb | 2 +- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/lib/transport.rb b/lib/transport.rb index d930638..5998c1d 100644 --- a/lib/transport.rb +++ b/lib/transport.rb @@ -1,11 +1,32 @@ def transport(source) - array = source.split("\n").map {|s| s.split(" ")} + array = split_numbers(split_rows(source)) rows_count = array.first.count transported_array = [] - 0.upto(rows_count - 1) do |i| - transported_array << array.map {|a| a[i]} + rows_count.times do |i| + transported_array << set_number(array, i) end - transported_array.map {|s| s.join(" ")}.join("\n") -end \ No newline at end of file + join_rows(join_numbers(transported_array)) +end + +def split_rows(string) + string.split("\n") +end + +def split_numbers(array) + array.map{|a| a.split(" ")} +end + +def join_rows(array) + array.join("\n") +end + +def join_numbers(array) + array.map{|a| a.join(" ")} +end + +def set_number(array, i) + array.map {|a| a[i]} +end + diff --git a/test/transport_test.rb b/test/transport_test.rb index 64126c8..56569d3 100644 --- a/test/transport_test.rb +++ b/test/transport_test.rb @@ -11,4 +11,4 @@ def test_transport output = "1 4 7 10\n2 5 8 11\n3 6 9 12" assert_equal output, transport(input) end -end \ No newline at end of file +end