4 kyu

Text align justify

212 of 8,523kesheshyan

Description:

Your task in this Kata is to emulate text justification in monospace font. You will be given a single-lined text and the expected justification width. The longest word will never be greater than this width.

Here are the rules:

  • Use spaces to fill in the gaps between words.
  • Each line should contain as many words as possible.
  • Use '\n' to separate lines.
  • Last line should not terminate in '\n'
  • '\n' is not included in the length of a line.
  • Gaps between words can't differ by more than one space.
  • Lines should end with a word not a space.
  • Large gaps go first, then smaller ones ('Lorem--ipsum--dolor--sit-amet,' (2, 2, 2, 1 spaces)).
  • Last line should not be justified, use only one space between words.
  • Lines with one word do not need gaps ('somelongword\n').

Example with width=30:

Lorem  ipsum  dolor  sit amet,
consectetur  adipiscing  elit.
Vestibulum    sagittis   dolor
mauris,  at  elementum  ligula
tempor  eget.  In quis rhoncus
nunc,  at  aliquet orci. Fusce
at   dolor   sit   amet  felis
suscipit   tristique.   Nam  a
imperdiet   tellus.  Nulla  eu
vestibulum    urna.    Vivamus
tincidunt  suscipit  enim, nec
ultrices   nisi  volutpat  ac.
Maecenas   sit   amet  lacinia
arcu,  non dictum justo. Donec
sed  quam  vel  risus faucibus
euismod.  Suspendisse  rhoncus
rhoncus  felis  at  fermentum.
Donec lorem magna, ultricies a
nunc    sit    amet,   blandit
fringilla  nunc. In vestibulum
velit    ac    felis   rhoncus
pellentesque. Mauris at tellus
enim.  Aliquam eleifend tempus
dapibus. Pellentesque commodo,
nisi    sit   amet   hendrerit
fringilla,   ante  odio  porta
lacus,   ut   elementum  justo
nulla et dolor.

Also you can always take a look at how justification works in your text editor or directly in HTML (css: text-align: justify).

Have fun :)

Strings
Algorithms

More By Author:

Check out these other kata created by kesheshyan

Stats:

CreatedMay 22, 2014
PublishedMay 22, 2014
Warriors Trained43329
Total Skips14209
Total Code Submissions168179
Total Times Completed8523
JavaScript Completions3126
CoffeeScript Completions48
Python Completions3366
Haskell Completions243
Ruby Completions532
C# Completions645
PHP Completions212
Java Completions472
Scala Completions27
Total Stars1475
% of votes with a positive feedback rating89% of 1151
Total "Very Satisfied" Votes957
Total "Somewhat Satisfied" Votes144
Total "Not Satisfied" Votes50
Ad
Contributors
  • kesheshyan Avatar
  • jhoffner Avatar
  • tko Avatar
  • GiacomoSorbi Avatar
  • natan Avatar
  • Suor Avatar
  • Blind4Basics Avatar
  • ekamaks Avatar
  • joelharkes Avatar
  • Voile Avatar
  • Madjosz Avatar
  • Ching Ching Avatar
  • KataSideKick Avatar
  • bidouille Avatar
  • hobovsky Avatar
  • jlb071 Avatar
  • KayleighWasTaken Avatar
  • AmirTallap Avatar
Ad