🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of kniknoo

kniknoo's solution

to Transpose in the Ruby Track

Published at Sep 11 2020 · 0 comments
Instructions
Test suite
Solution

Given an input text output it transposed.

Roughly explained, the transpose of a matrix:

ABC
DEF

is given by:

AD
BE
CF

Rows become columns and columns become rows. See https://en.wikipedia.org/wiki/Transpose.

If the input has rows of different lengths, this is to be solved as follows:

  • Pad to the left with spaces.
  • Don't pad to the right.

Therefore, transposing this matrix:

ABC
DE

results in:

AD
BE
C

And transposing:

AB
DEF

results in:

AD
BE
 F

In general, all characters from the input should also be present in the transposed output. That means that if a column in the input text contains only spaces on its bottom-most row(s), the corresponding output row should contain the spaces in its right-most column(s).


For installation and learning resources, refer to the Ruby resources page.

For running the tests provided, you will need the Minitest gem. Open a terminal window and run the following command to install minitest:

gem install minitest

If you would like color output, you can require 'minitest/pride' in the test file, or note the alternative instruction, below, for running the test file.

Run the tests from the exercise directory using the following command:

ruby transpose_test.rb

To include color from the command line:

ruby -r minitest/pride transpose_test.rb

Source

Reddit r/dailyprogrammer challenge #270 [Easy]. https://www.reddit.com/r/dailyprogrammer/comments/4msu2x/challenge_270_easy_transpose_the_input_text

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

transpose_test.rb

require 'minitest/autorun'
require_relative 'transpose'

# Common test data version: 1.1.0 92bc877
class TransposeTest < Minitest::Test
  def test_empty_string
    # skip
    input = ""

    expected = ""

    assert_equal expected, Transpose.transpose(input)
  end

  def test_two_characters_in_a_row
    skip
    input = "A1"

    expected = "A\n1"

    assert_equal expected, Transpose.transpose(input)
  end

  def test_two_characters_in_a_column
    skip
    input = "A\n1"

    expected = "A1"

    assert_equal expected, Transpose.transpose(input)
  end

  def test_simple
    skip
    input = "ABC\n123"

    expected = "A1\nB2\nC3"

    assert_equal expected, Transpose.transpose(input)
  end

  def test_single_line
    skip
    input = "Single line."

    expected = "S\ni\nn\ng\nl\ne\n \nl\ni\nn\ne\n."

    assert_equal expected, Transpose.transpose(input)
  end

  def test_first_line_longer_than_second_line
    skip
    input = "The fourth line.\nThe fifth line."

    expected = "TT\nhh\nee\n  \nff\noi\nuf\nrt\nth\nh \n l\nli\nin\nne\ne.\n."

    assert_equal expected, Transpose.transpose(input)
  end

  def test_second_line_longer_than_first_line
    skip
    input = "The first line.\nThe second line."

    expected = "TT\nhh\nee\n  \nfs\nie\nrc\nso\ntn\n d\nl \nil\nni\nen\n.e\n ."

    assert_equal expected, Transpose.transpose(input)
  end

  def test_mixed_line_length
    skip
    input = "The longest line.\nA long line.\nA longer line.\nA line."

    expected = "TAAA\nh   \nelll\n ooi\nlnnn\nogge\nn e.\nglr\nei \nsnl\ntei\n .n\nl e\ni .\nn\ne\n."

    assert_equal expected, Transpose.transpose(input)
  end

  def test_square
    skip
    input = "HEART\nEMBER\nABUSE\nRESIN\nTREND"

    expected = "HEART\nEMBER\nABUSE\nRESIN\nTREND"

    assert_equal expected, Transpose.transpose(input)
  end

  def test_rectangle
    skip
    input = "FRACTURE\nOUTLINED\nBLOOMING\nSEPTETTE"

    expected = "FOBS\nRULE\nATOP\nCLOT\nTIME\nUNIT\nRENT\nEDGE"

    assert_equal expected, Transpose.transpose(input)
  end

  def test_triangle
    skip
    input = "T\nEE\nAAA\nSSSS\nEEEEE\nRRRRRR"

    expected = "TEASER\n EASER\n  ASER\n   SER\n    ER\n     R"

    assert_equal expected, Transpose.transpose(input)
  end
end
class Transpose
  def self.transpose(input)
    input = input.each_line.map(&:chomp)
    longest = input.each.max_by(&:size) || ''
    expected = Array.new(longest.size, '')
    input.each do |l|
      l.ljust(longest.size).each_char.with_index { |c, ci| expected[ci] += c }
    end
    expected.map(&:rstrip).join("\n")
  end
end

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?