Last active
March 7, 2020 17:42
-
-
Save ivanobulo/4d03f496a165ec473934fc76ee044b4b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def buildLine(words: Vector[String], maxWidth: Int, wordsWidth: Int): String = { | |
val spacePlacements = words.size - 1 | |
val missingSpaces = maxWidth - wordsWidth | |
val numSpace = missingSpaces / Math.max(spacePlacements, 1) | |
val remSpaces = missingSpaces % Math.max(spacePlacements, 1) | |
words.zipWithIndex.foldLeft(new StringBuffer()) { case (buf, (word, pos)) => | |
buf.append(word) | |
if (pos == 0 || pos < words.size - 1) { // first or not last | |
buf.append(Array.fill(numSpace + (if (pos < remSpaces) 1 else 0))(' ')) | |
} else { | |
buf | |
} | |
}.toString | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment