Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions lib/transport.rb
Original file line number Diff line number Diff line change
@@ -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
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

2 changes: 1 addition & 1 deletion test/transport_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
end