r/dailyprogrammer • u/Elite6809 1 1 • Jul 27 '15
[2015-07-27] Challenge #225 [Easy/Intermediate] De-columnizing
(Easy/Intermediate): De-columnizing
Often, column-style writing will put images and features to the left or right of the body of text, for example:
24
This is an example piece of text. This is an exam-
ple piece of text. This is an example piece of
text. This is an example
piece of text. This is a +-----------------------+
sample for a challenge. | |
Lorum ipsum dolor sit a- | top class |
met and other words. The | feature |
proper word for a layout | |
like this would be type- +-----------------------+
setting, or so I would
imagine, but for now let's carry on calling it an
example piece of text. Hold up - the end of the
paragraph is approaching - notice
+--------------+ the double line break for a para-
| | graph.
| |
| feature | And so begins the start of the
| bonanza | second paragraph but as you can
| | see it's only marginally better
| | than the other one so you've not
+--------------+ really gained much - sorry. I am
certainly not a budding author
as you can see from this example input. Perhaps I
need to work on my writing skills.In order to fit into the column format, some words are hyphenated. For the purpose of the challenge, you may assume that any hyphens at the end of a line join a single un-hyphenated word together (for example, the exam- and ple in the above input form the word example and not exam-ple). However, hyphenated words that do not span multiple lines should retain their hyphens. Side features will only appear at the far left or right of the input, and will always be bordered by the +---+ style shown above. They will also never have 'holes' in them, like this:
+--------------------+
| |
| Inside the feature |
| |
| +----------------+ |
| | | |
| | Outside | |
| | | |
| +----------------+ |
| |
+--------------------+Paragraphs in the input are separated by double line breaks, like Reddit markdown. Your task today is to extract just the paragraph text from the input, removing the feature-boxes.
Formal Inputs and Outputs
Input Specification
You'll be given a number N on one line, followed by N further lines of input like the example in the description above.
Output Description
Output just the paragraph text, de-hyphenating words where appropriate (ie. a line of text ends with a hyphen).
Sample Inputs and Outputs
Example 1
This corresponds to the input given in the Description.
Output
This is an example piece of text. This is an example piece of text. This is an example piece of text. This is an example piece of text. This is a sample for a challenge. Lorum ipsum dolor sit amet and other words. The proper word for a layout like this would be typesetting, or so I would imagine, but for now let's carry on calling it an example piece of text. Hold up - the end of the paragraph is approaching - notice the double line break for a paragraph.
And so begins the start of the second paragraph but as you can see it's only marginally better than the other one so you've not really gained much - sorry. I am certainly not a budding author as you can see from this example input. Perhaps I need to work on my writing skills.
Example 2
Input
22
+-------------+ One hundred and fifty quadrillion,
| | seventy-two trillion, six hundred
| 150 072 626 | and twenty-six billion, eight hun-
| 840 312 999 | dred and fourty million, three
| | hundred and thirteen thousand sub-
+-------------+ tract one is a rather large prime
number which equals one to five if
calculated modulo two to six respectively.
However, one other rather more in- +-------------+
teresting number is two hundred | |
and twenty-one quadrillion, eight | 221 806 434 |
hundred and six trillion, four | 537 978 679 |
hundred and thirty-four billion, | |
five hundred and thirty-seven mil- +-------------+
million, nine hundred and seven-
ty-eight thousand,
+-----------------------------+ six hundred and
| | seventy nine,
| Subscribe for more Useless | which isn't prime
| Number Facts(tm)! | but is the 83rd
+-----------------------------+ Lucas number.Output
One hundred and fifty quadrillion, seventy-two trillion, six hundred and twenty-six billion, eight hundred and fourty million, three hundred and thirteen thousand subtract one is a rather large prime number which equals one to five if calculated modulo two to six respectively.
However, one other rather more interesting number is two hundred and twenty-one quadrillion, eight hundred and six trillion, four hundred and thirty-four billion, five hundred and thirty-seven milmillion, nine hundred and seventy-eight thousand, six hundred and seventy nine, which isn't prime but is the 83rd Lucas number.
Example 3
Input
16
+----------------+ Lorem ipsum dolor sit amet,
| | consectetur adipiscing elit,
| Aha, now you | sed do eiusmod tempor incid-
| are stumped!! | idunt ut labore et dolore
| | magna aliqua. Ut enim ad mi-
| +--------+ nim veniam, quis nostrud ex-
| top | ercitation ullamco laboris
| kek | nisi ut aliquip ex.
| | +-------------+
+-------+ Duis aute irure dolor | |
in repre-henderit in voluptate | Nothing to |
velit esse cillum dolore eu fu- | see here. |
giat nulla pariatur. Excepteur | |
sint occaecat cupidatat non +-------------+
proident, sunt in culpa qui of-
ficia deserunt mollit anim id est laborum.Output
Lorem ipsum dolor sit amet, consectetur adipiscing 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.
Duis aute irure dolor in repre-henderit 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.
Extension (Intermediate)
At the start of each paragraph in your output, list the text of each feature associated with that paragraph. A feature is "associated" with a paragraph if the top of the feature box (the +--------+) starts on or below the line that the paragraph starts on. For example, the outputs for the above three examples would be:
Example 1 Output
(top class feature) (feature bonanza) This is an example piece of text. This is an example piece of text. This is an example piece of text. This is an example piece of text. This is a sample for a challenge. Lorum ipsum dolor sit amet and other words. The proper word for a layout like this would be typesetting, or so I would imagine, but for now let's carry on calling it an example piece of text. Hold up - the end of the paragraph is approaching - notice the double line break for a paragraph.
And so begins the start of the second paragraph but as you can see it's only marginally better than the other one so you've not really gained much - sorry. I am certainly not a budding author as you can see from this example input. Perhaps I need to work on my writing skills.
Example 2 Output
(150 072 626 840 312 999) One hundred and fifty quadrillion, seventy-two trillion, six hundred and twenty-six billion, eight hundred and fourty million, three hundred and thirteen thousand subtract one is a rather large prime number which equals one to five if calculated modulo two to six respectively.
(221 806 434 537 978 679) (Subscribe for more Useless Number Facts(tm)!) However, one other rather more interesting number is two hundred and twenty-one quadrillion, eight hundred and six trillion, four hundred and thirty-four billion, five hundred and thirty-seven milmillion, nine hundred and seventy-eight thousand, six hundred and seventy nine, which isn't prime but is the 83rd Lucas number.
Example 3 Output
(Aha, now you are stumped! top kek) (Nothing to see here.) Lorem ipsum dolor sit amet, consectetur adipiscing 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.
Duis aute irure dolor in repre-henderit 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.
Finally
Got any cool challenge ideas? Submit them to /r/DailyProgrammer_Ideas!
5
u/[deleted] Jul 28 '15 edited Jul 28 '15
...
4
u/mdskrzypczyk Jul 28 '15
Python 2.7, using regular expressions this becomes a breeze! Decided I'd rather take my input from a file if that's okay.
2
u/adrian17 1 4 Jul 27 '15 edited Jul 27 '15
Python.
Wow, I started the challenge thinking "hm, looks interesting, maybe a bit too hard for [easy]"... And then I noticed extension. Then I notices paragraphs. And then I noticed Example 3. Wat.
For simplicity, I skipped Input #3 but still did the Extension. It works pretty nicely as long as the features are rectangular. The code turned to be much longer than I expected, there is also some duplication that would be hard to remove so I didn't bother.
9
u/galaktos Jul 27 '15 edited Jul 27 '15
sed
I’ll turn this into a prettier script in a moment.
EDIT: You bastard, I just saw example 3. Grr, hang on…
EDIT 2015-07-27T20:54+0200 : Here’s a script:
EDIT 2015-07-27T20:55+0200 : Some notes:
s/^[0-9]+$//of course, but that would also delete numbers in the main text.)s/\n//gis well-defined in POSIX sed. Tested with GNU sed.EDIT 2015-07-27T21:04+0200 : I updated the script above (but not the one-line version), now example 3 works too. I’m not going to do the extension, so I consider myself done now. This was fun, thanks for the challenge :)
4
u/AdrianOkanata Jul 27 '15
Ruby one-liner. Works for examples 1, 2, and 3, but not the extension:
4
u/Contagion21 Jul 27 '15 edited Jul 27 '15
C# w/ Regex
EDIT: Hmm.. still a minor bug with paragraphs.. regex isn't treating whitespace as I would expect...
EDIT2: Fixed. Had to make \s* non-greedy even though I didn't expect it to span lines in multiline mode.
1
u/chunes 1 2 Jul 28 '15
I just wanted to say this challenge is really evil. I've re-written my solution at least five times and I'm still no closer to a generalized solution that can handle input 3.
1
u/Fulgere Aug 19 '15
I'm happy enough with my Java solution (even though I know it is far from perfect). My biggest concern as I was working on this was how many 'best practices' I was unwittingly breaking. I'm guessing others will find my code hard to read and that I should be more aggressively implementing an OO solution, but alas, this is where I am!
Any suggestions on improving the process I write my code or how to better format for others would be greatly appreciated. Thanks!
3
u/skeeto -9 8 Jul 27 '15 edited Jul 27 '15
C, processing input one character at a time, doing just the easy version. Edit: just noticed it messes up the "seven ty" bit. That's tricky!
3
u/Hells_Bell10 Jul 27 '15 edited Jul 28 '15
C++, it is a mess but it works.
3
u/chrissou Jul 28 '15
Only using
sed, I don't use it so much so it's been good practice. Some regex may be merged, feedback appreciatedAlthough it works for the 3 examples, it may not work on more complex cases... Would try using
awkif I find the time since it seems more powerful than sed for this particular purpose2
u/hutsboR 3 0 Jul 27 '15
Elixir:
Output: #2
2
u/Godspiral 3 3 Jul 27 '15 edited Jul 27 '15
In J, basically last week's solution
This is an example piece of text. This is an example piece of text. This is an example piece of text. This is an example piece of text. This is a sample for a challenge. Lorum ipsum dolor sit a met and other words. The proper word for a layout like this wo uld be type setting, or so I would imagine, but for now let's carry on calling it an example piece of text. Hold up the end of the paragraph is approaching notice the double line break for a para graph. And so begins the start of the second paragraph but a s you can see it's only marginally better than the other one so you've not really gained much sorry. I am certainly not a budding author as you can see from this example input. Perhaps I need to work on my writing skills.
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incid idunt ut labore et dolore magn a aliqua. Ut enim ad mi nim veniam, quis nostrud ex ercitation ullamco laboris nisi ut aliquip ex. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fu giat nulla pariatur. Excepteur sint occaecat cup idatat non proident, sunt in culpa qui of ficia deserunt mollit anim id est laborum.
2
u/Wiggledan Jul 27 '15 edited Jul 28 '15
C99, here's my hobbled solution for the easy challenge. Ugly, but it works. Give it a filename as the first argument for input.
Any suggestions on how to clean it up or do things better would be appreciated.
2
u/[deleted] Aug 02 '15 edited Aug 02 '15
Mathematica:
It's basically unreadable, but works on all three challenge inputs.
1
u/Ledrug 0 2 Jul 27 '15
Simple minded Perl code, no extension. Assuming "|" and "+---+" never appears in main text.
1
u/jnazario 2 0 Jul 27 '15 edited Jul 27 '15
scala. handles easy 1 2 and 3 EDITED to handle paragraph breaks
1
u/octbop Jul 27 '15 edited Jul 27 '15
Java
1
u/glenbolake 2 0 Jul 27 '15
Python 2.7, with extension, but written before I saw example 3. I'll tackle that one tomorrow.
1
u/ReckoningReckoner Jul 27 '15 edited Jul 28 '15
EDIT: Now does extension:
Ruby:
Outputs:
1
u/steffiwilson Jul 27 '15 edited Jul 27 '15
Java, solution for the Easy variant on gist.
As I noted in the comments of my code, my solution would not work for boxes shaped like:
Feedback (particularly for efficiency, any errors in my output that I might have missed, or suggestions to allow for the box types above) is welcome.
1
u/a_Happy_Tiny_Bunny Jul 28 '15
Haskell
Probably the ugliest Haskell code I have ever written. In fact, it may only be second to a C++ barebones SQL parser in ugliness among all the code I've written. I probably should stop writing parsers :p
Disclaimer: The non-extension code is actually easy to follow, except for perhaps one line.
The program takes any amount of arguments. If any argument is given, it does the extension challenge. Otherwise it does the normal thing.
The extension seems to be working, but there are two spacing nitpicks: there is now a space after paragraphs, and there is also as many spaces as there were linebreaks between fragments of the feature texts (e.g. there are two spaces before "top kek" instead of one).
I can think of a few things to improve the code, but feedback is still appreciated. I may come back tomorrow to straighten up the code.
1
u/Zeno_of_Elea Jul 28 '15 edited Jul 28 '15
Python 3 - I'd love some constructive criticism and maybe an indication as to what I've done wrong and how I might fix it. I figure I've made some false assumptions, but that's about all I can say might be wrong.
I've been stumped by example 3 (and features that extend past the length of the paragraph, due to the way my code functions).
Anyways, I'll post my code which works for the first challenge and almost for the second. There are some significant flaws and it's probably hard to read due to a lack of helpful comments, but at least it's somewhat short.
Output (all three examples bundled together) When I pasted it, things got a bit weird, so sorry if a space is missing because I had to put these together manually:
1
u/[deleted] Jul 28 '15 edited Jul 28 '15
Java. Quite ugly, but I freshened my memory and remembered how to regex. The program takes a path to a txt file containing a text to decolumnize. It assumes that text doesn't contain '|' character and that there is at least one whitespace between the border of a frame and the text inside it.
Output:
I'll work on the bonus challenge later.
1
u/alisterr Jul 28 '15
Java. I can't believe, how much I struggled for all extensions! There's more to this challenge, than I did realize at first sight.
1
u/glenbolake 2 0 Jul 28 '15 edited Jul 28 '15
Second submission, also Python 2.7. This one handles all 3 examples and shows the contents of each box. The only problem with this one is that it will get thrown off if the content of a feature box include a
+or|. Super-long because I commented it heavily.1
u/mpm_lc Jul 29 '15
Super short ruby solution. There are a few instances where a double space is created that can likely be fixed with a couple of regex tweaks but I didn't have time to comb through it right now. None the less, it gets the important parts done:
1
u/milliDavids Jul 29 '15
Ruby (without extension)
1
u/DigBlocks Jul 29 '15
A solution in Python 2.7 (Doesn't do Extension) It works fine for the three example inputs, but I'm not sure about how efficient is with so many loops:
1
u/99AFCC Jul 30 '15
Python 3.4
Kind of ugly, not a generic solution. Lots of little tweaks to get the output to match the answer.
1
u/melombuki Jul 30 '15
Another Ruby solution. It was largely inspired by mpm_lc's solution, but is a little bit different. Works perfectly for examples 1, 2, and 3, but not the extension.
1
u/philhartmonic Jul 31 '15
Python
For Example 2 I got this output:
1
u/skav3n Aug 01 '15 edited Aug 01 '15
Python3:
One mistake, output:
1
u/Iprefervim Aug 02 '15 edited Aug 02 '15
Did it in Common Lisp (Clozure). This is my first time programming in Common Lisp, so please point out any glaring issues that I might have done. I wish I could have made it shorter than it is (it's only slightly shorter than the naive Python implementation that I coded up to understand the question), but that's probably because I don't know what libraries to use (apart from split-sequence)
1
u/crossroads1112 Aug 03 '15 edited Aug 03 '15
Python 3
This is just the solution to the easy part. I'll do the intermediate if I have time later. I tried to get as close to a 'one liner' as possible. This would screw up if the input had null bytes though.
Or in its gloriously unreadable unindented form:
1
u/ironboy_ Sep 04 '15
A JavaScript solution (including the extension problem) that's a mix of a walk through each char to separate features, and som reg exps. (I tried a pure reg exp approach first - but it got to unreadable).
1
u/BumpitySnook Sep 09 '15
shell piping + sed + awk. Newlines added for clarity:
Feed input on stdin, e.g., append
< example1.txtto the line.The
sedlines, respectively:The
awklines, respectively: