Avatar of w1zeman1p

w1zeman1p's solution

to Transpose in the Ruby Track

Published at Mar 06 2019 · 0 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution was written.

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 = <<-INPUT.gsub(/^ {6}/, '').strip

    INPUT

    expected = <<-EXPECTED.gsub(/^ {6}/, '').strip

    EXPECTED

    assert_equal expected, Transpose.transpose(input)
  end

  def test_two_characters_in_a_row
    skip
    input = <<-INPUT.gsub(/^ {6}/, '').strip
      A1
    INPUT

    expected = <<-EXPECTED.gsub(/^ {6}/, '').strip
      A
      1
    EXPECTED

    assert_equal expected, Transpose.transpose(input)
  end

  def test_two_characters_in_a_column
    skip
    input = <<-INPUT.gsub(/^ {6}/, '').strip
      A
      1
    INPUT

    expected = <<-EXPECTED.gsub(/^ {6}/, '').strip
      A1
    EXPECTED

    assert_equal expected, Transpose.transpose(input)
  end

  def test_simple
    skip
    input = <<-INPUT.gsub(/^ {6}/, '').strip
      ABC
      123
    INPUT

    expected = <<-EXPECTED.gsub(/^ {6}/, '').strip
      A1
      B2
      C3
    EXPECTED

    assert_equal expected, Transpose.transpose(input)
  end

  def test_single_line
    skip
    input = <<-INPUT.gsub(/^ {6}/, '').strip
      Single line.
    INPUT

    expected = <<-EXPECTED.gsub(/^ {6}/, '').strip
      S
      i
      n
      g
      l
      e
       
      l
      i
      n
      e
      .
    EXPECTED

    assert_equal expected, Transpose.transpose(input)
  end

  def test_first_line_longer_than_second_line
    skip
    input = <<-INPUT.gsub(/^ {6}/, '').strip
      The fourth line.
      The fifth line.
    INPUT

    expected = <<-EXPECTED.gsub(/^ {6}/, '').strip
      TT
      hh
      ee
        
      ff
      oi
      uf
      rt
      th
      h 
       l
      li
      in
      ne
      e.
      .
    EXPECTED

    assert_equal expected, Transpose.transpose(input)
  end

  def test_second_line_longer_than_first_line
    skip
    input = <<-INPUT.gsub(/^ {6}/, '').strip
      The first line.
      The second line.
    INPUT

    expected = <<-EXPECTED.gsub(/^ {6}/, '').strip
      TT
      hh
      ee
        
      fs
      ie
      rc
      so
      tn
       d
      l 
      il
      ni
      en
      .e
       .
    EXPECTED

    assert_equal expected, Transpose.transpose(input)
  end

  def test_mixed_line_length
    skip
    input = <<-INPUT.gsub(/^ {6}/, '').strip
      The longest line.
      A long line.
      A longer line.
      A line.
    INPUT

    expected = <<-EXPECTED.gsub(/^ {6}/, '').strip
      TAAA
      h   
      elll
       ooi
      lnnn
      ogge
      n e.
      glr
      ei 
      snl
      tei
       .n
      l e
      i .
      n
      e
      .
    EXPECTED

    assert_equal expected, Transpose.transpose(input)
  end

  def test_square
    skip
    input = <<-INPUT.gsub(/^ {6}/, '').strip
      HEART
      EMBER
      ABUSE
      RESIN
      TREND
    INPUT

    expected = <<-EXPECTED.gsub(/^ {6}/, '').strip
      HEART
      EMBER
      ABUSE
      RESIN
      TREND
    EXPECTED

    assert_equal expected, Transpose.transpose(input)
  end

  def test_rectangle
    skip
    input = <<-INPUT.gsub(/^ {6}/, '').strip
      FRACTURE
      OUTLINED
      BLOOMING
      SEPTETTE
    INPUT

    expected = <<-EXPECTED.gsub(/^ {6}/, '').strip
      FOBS
      RULE
      ATOP
      CLOT
      TIME
      UNIT
      RENT
      EDGE
    EXPECTED

    assert_equal expected, Transpose.transpose(input)
  end

  def test_triangle
    skip
    input = <<-INPUT.gsub(/^ {6}/, '').strip
      T
      EE
      AAA
      SSSS
      EEEEE
      RRRRRR
    INPUT

    expected = <<-EXPECTED.gsub(/^ {6}/, '').strip
      TEASER
       EASER
        ASER
         SER
          ER
           R
    EXPECTED

    assert_equal expected, Transpose.transpose(input)
  end
end
class Transpose
  def self.transpose(input)
    lines = input.lines.map(&:chomp)
    max_length = lines.map(&:length).max

    lines
      .map{|l| l.ljust(max_length)}
      .map(&:chars)
      .transpose
      .map(&:join)
      .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?