Scala Tutorial Through Katas: Word Wrap (Easy)

A programming kata is an exercise which helps a programmer hone his skills through practice and repetition.

This article is part of the series "Scala Tutorial Through Katas". Articles are divided into easy, medium and hard. Beginners should start with easy ones and move towards more complicated once they feel more comfortable programming in Scala.

For the complete list of Scala katas and solutions please visit the index page

The article assumes that the reader is familiar with the basic usage of ScalaTest asserts and knows how to run them from his favorite Scala IDE (ours is IntelliJ IDEA with the Scala plugin).

Tests that prove that the solution is correct are displayed below. Recommended way to solve this kata is to write the implementation for the first test, confirm that it passes and move to the next. Once all of the tests pass, the kata can be considered solved.

One possible solution is provided below the tests. Try to solve the kata by yourself first.

Word Wrap

Write Word Wrapper, that has a single static function named wrap that takes two arguments, a string, and a column number.
The function returns the string, but with line breaks inserted at just the right places to make sure that no line is longer than the column number.

Like a word processor, break the line by replacing the last space in a line with a newline.

Solution can assume that no word is longer than the maximum number characters in a line.

Following is a set of unit tests that can be used to solve this kata in the TDD fashion.

[UNIT TESTS]

Test code can be found in the GitHub WordWrap.scala.

[ONE POSSIBLE SOLUTION]

The solution code can be found in WordWrap.scala solution.

What was your solution? Post it as a comment so that we can compare different ways to solve this kata.

8 thoughts on “Scala Tutorial Through Katas: Word Wrap (Easy)

  1. Ajinkya

    Indeed, Katas are great way to learn a new language. I would probably try some concurrent algorithms and design as well e.g. producer/consumer.

    Reply
  2. Chris Searle

    Is it just me or does the example fail if you set the length to be the same as actual length of the string (it replaces the last space with a newline) ?

    Reply
    1. Viktor Farcic

      Chris, can you post a test that fails?

      Following was successful:

      it should “work with a single word” in {
      “kata”.wordWrap(4) should be (“kata”)
      }

      Reply
      1. Chris Searle

        Mixed java/scala project with tests in java right now – but I ran:

        public void testNoWrappingScala() {
            WordWrap wordWrap = new ScalaWordWrap();
            assertEquals(TEXT, wordWrap.wrap(TEXT, TEXT.length()));
        }
        

        Where TEXT is “Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.”

        And I get

        java.lang.AssertionError:

        expected:

        but was:

        That may not show up well here – but in the actual I get a newline before the final laborum

        Reply
      2. Chris Searle

        Aargh – the test results didn’t show – this time without any angle brackets:

        java.lang.AssertionError:

        expected:Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

        but was:Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est
        laborum.

        That may not show up well here – but in the actual I get a newline before the final laborum

        Reply
      3. Viktor Farcic

        You’re right. Trim was missing in the if statement. I added a new test that reproduces the bug and the solution.

        Before:
        if ((out.last + ” ” + in).length > maxLength) out :+ in

        After:
        if ((out.last + ” ” + in).trim.length > maxLength) out :+ in

        Reply
  3. Chris Searle

    Yes – that worked. Thanks 🙂

    Now – the only thing this doesn’t do that the different java versions I have do is splitting words that are longer than the maxLength at maxLength-1 and then append a hyphen before the newline – but my scala skills are sadly not good enough to try adding that 🙂

    Reply
  4. Alex

    Hi,

    I’ve done a tail recursive version:

    def wrap(someString: String, columnSize: Int) : String = {
    if (columnSize >= someString.length) {
    someString
    }
    else {
    someString.take(columnSize) + “\n” + wrap(someString.takeRight(someString.length – columnSize), columnSize)
    }
    }

    Reply

Leave a Reply