r/dailyprogrammer • u/fvandepitte 0 0 • Aug 18 '16
[2016-08-18] Challenge #279 [Intermediate] Text Reflow
Description:
Text reflow means to break up lines of text so that they fit within a certain width. It is useful in e.g. mobile browsers. When you zoom in on a web page the lines will become too long to fit the width of the screen, unless the text is broken up into shorter lines.
Input:
You will be given a text with a maximum line width of 80 characters.
Output:
Produce the same text with a maximum line width of 40 characters
Challenge Input:
In the beginning God created the heavens and the earth. Now the earth was
formless and empty, darkness was over the surface of the deep, and the Spirit of
God was hovering over the waters.
And God said, "Let there be light," and there was light. God saw that the light
was good, and he separated the light from the darkness. God called the light
"day," and the darkness he called "night." And there was evening, and there was
morning - the first day.Challenge Output:
In the beginning God created the heavens
and the earth. Now the earth was
formless and empty, darkness was over
the surface of the deep, and the Spirit
of God was hovering over the waters.
And God said, "Let there be light," and
there was light. God saw that the light
was good, and he separated the light
from the darkness. God called the light
"day," and the darkness he called
"night." And there was evening, and
there was morning - the first day.Bonus:
Let's get rid of the jagged right margin of the text and make the output prettier. Output the text with full justification; Adjusting the word spacing so that the text is flush against both the left and the right margin.
Bonus Output:
In the beginning God created the heavens
and the earth. Now the earth was
formless and empty, darkness was over
the surface of the deep, and the Spirit
of God was hovering over the waters.
And God said, "Let there be light," and
there was light. God saw that the light
was good, and he separated the light
from the darkness. God called the light
"day," and the darkness he called
"night." And there was evening, and
there was morning - the first day.Finally
This challenge is posted by /u/slampropp
Also have a good challenge idea?
Consider submitting it to /r/dailyprogrammer_ideas
7
u/skeeto -9 8 Aug 18 '16 edited Aug 18 '16
ANSI C, with bonus. The last line of a paragraph isn't justified since I prefer that aesthetic. Only the current line (input and output) is buffered, so it will work properly on arbitarily long inputs.
Spacing for justification is done by distributing spaces starting between the outermost words, working towards words in the middle.
4
u/nawap Aug 18 '16
Ruby
Mostly regex magic. Will post the bonus one soon.
1
u/dekx Aug 19 '16
I've been working on this with bonus, and keep getting hung up on the additional spacing to match the output. I get a solution that works as far as spacing and alignment, but the inner spacing does not match exactly the output provided. The question I have is... do most people work to match output to be exactly as described in the challenge, or output that matches the spirit of the challenge?
3
u/dinow Aug 18 '16 edited Aug 18 '16
Java Maybe not smart, and really not small ... Implements bonus. Adapted to include starting new lines
3
u/crystalgecko Aug 19 '16
Python 2
First time contributing due to never coming up with a solution different/smaller/whatever enough to the existing ones to be worth posting.
No bonus, but maybe I'll think about adding that when I have a little more time
2
u/adamnew123456 Aug 18 '16 edited Aug 18 '16
Python 3 (sans bonus). It could be reduced down to the first function, but I wanted it to respect paragraph boundaries.
2
u/Godspiral 3 3 Aug 19 '16
in J, wrong answer but pretty
2
u/dwolf555 Aug 19 '16 edited Aug 19 '16
Python2
2
u/weekendblues Aug 19 '16 edited Aug 19 '16
Haskell
A bit late to the show, but posting anyway. No bonus yet, although I may give it a shot. Works for arbitrary line lengths. Handles corner cases (ie. requests to word wrap with an extremely small width or extremely long words) relatively gracefully. Feedback more than welcome!
2
u/iWriteC0de Aug 25 '16
C# - Expanding/justifying the text was harder than I thought it would be. Bonus included. There are several edge cases I know I missed but this is the general idea.
1
u/afryk Aug 18 '16
Javascript ES6 (without bonus)
For the sake of learning, this solution is probably overcomplicated, but who cares. Text wrapper using Writer monad:
tuple.js is simple helper that wraps two values in object {fst, snd}; WriterMonad code:
Comments welcome. :)
1
u/[deleted] Aug 18 '16
Python 3 Only split() is necessary, everything else either reads or writes a file
1
u/[deleted] Aug 18 '16
Wrote a quick solution in C# that probably fails in some edge cases. Supports both the non-bonus and bonus situations.
1
u/Rashnok Aug 18 '16
Python 3 + bonus
1
u/WaywardTraveler_ Aug 19 '16
Swift 2.2
Swift strings aren't very intuitive to work with. Don't know if I like them - But they are safer, so it's a tradeoff.
1
u/__brogrammer Aug 19 '16
PHP - recursive - with bonus
1
u/[deleted] Aug 19 '16
Didn't do the bonus.
1
u/domlebo70 1 2 Aug 19 '16 edited Aug 19 '16
Edit:
Different approach. Folding is much simpler and easier to read. The code literally falls out of your head this way. This specific impl is not very efficient (i use appending to a List for instance), but it's readable.
1
u/[deleted] Aug 19 '16
Rust (no bonus)
1
u/Scroph 0 0 Aug 19 '16
C++11 with bonus :
This was very painful to write.
1
u/StopDropHammertime Aug 19 '16 edited Aug 19 '16
F# with bonus + (hopefully) accounting for words that are longer than the desired line length
1
u/thtoeo Aug 19 '16
C#. Trying to learn to object-oriented programming, so here's overly complicated try with classes :).
1
u/4kpics Aug 19 '16
Python 3
1
u/[deleted] Aug 20 '16
C# no bonus
1
u/Otifex Aug 20 '16
PYTHON 3
First time posting, any advice is welcome. Solution with bonus
1
u/Keep_Phishing Aug 20 '16 edited Aug 20 '16
Python 2
Far uglier than I had pictured, but ah well.
1
u/rmellema Aug 20 '16
Haskell
Probably quite inefficient, but it works. Might attempt bonus some time later.
1
u/[deleted] Aug 21 '16 edited Aug 21 '16
JAVA 8
This is my first time here and I a complete noob with any language. But since I'm was practicing with Java in these days, I want to see if I can make it work. ;) This exercise take me several hours. And I didn't want to use ArrayList for the first time, but well, it works. :P English is not my native language, so I did my best with the comments (probably too much of them). And no bonus implemented this time.
1
u/Shrubberer Aug 21 '16
Python 3
output:
1
u/shahbaz_man Aug 21 '16
Kotlin The basic code without the bonus:
The bonus code for justifying:
1
u/ma-int Aug 22 '16
Rust (with bonus).
I'm pretty new to rust and using this Sub to learn it. I tried creating an iterator for the reflow part...it works but I'm not particular happy with the result. Looks pretty verbose to me :/
1
u/sepehr500 Aug 23 '16
Super short C# solution
1
u/_dd97_ Aug 23 '16
1
u/RealLordMathis Aug 24 '16
Java with bonus (quite long but it works) :
1
u/radiataGhost Aug 24 '16 edited Aug 24 '16
Common Lisp
Still learning lisp so any feedback is appreciated.
1
u/draegtun Aug 24 '16 edited Aug 24 '16
Rebol (with bonus)
Basic challenge (reflowed text in-place):
Bonus challenge (prints reflowed/justified text):
NB. Tested in Rebol 3.
1
u/aQaTL Aug 25 '16 edited Aug 25 '16
Java, without bonus
1
u/kahuna_splicer Sep 04 '16 edited Sep 04 '16
C++ solution without the bonus, feedback is appreciated.
0
u/[deleted] Aug 18 '16
[deleted] — view deleted comment
0
u/[deleted] Aug 19 '16
[deleted] — view deleted comment