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]

import org.scalatest.{Matchers, FlatSpec} import com.wordpress.technologyconversations.learning.kata.solutions.WordWrap._ class WordWrapTest extends FlatSpec with Matchers { "WordWrap" should "return empty string is no text is specified" in { "".wordWrap(10) should be ("") } it should "work with a single word" in { "kata".wordWrap(4) should be ("kata") } it should "return wrapped text with small sample" in { val input = "This kata" val expected = "Thisnkata" input.wordWrap(4) should be (expected) } it should "return wrapped text" in { val input = "This kata should be easy unless there are hidden, or not so hidden, obstacles. Let's start!" val expected = "This katanshould beneasy unlessnthere arenhidden, ornnot sonhidden,nobstacles.nLet's start!" input.wordWrap(12) should be (expected) } it should "return the same text if max length is the same as the length of the text" in { val input = "Lorem ipsum dolor sit amet." val expected = "Lorem ipsum dolor sit amet." input.wordWrap(input.length) should be (expected) } }

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

[ONE POSSIBLE SOLUTION]

object WordWrap { implicit class StringImprovements(s: String) { def wordWrap(maxLength: Int): String = { s.split(" ").foldLeft(Array(""))( (out, in) => { if ((out.last + " " + in).trim.length > maxLength) out :+ in else out.updated(out.size - 1, out.last + " " + in) }).mkString("n").trim } } }

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.