r/dailyprogrammer • u/jnazario 2 0 • May 02 '18
[2018-05-02] Challenge #359 [Intermediate] Unwrap Some Text
Description
Most of us are familiar with word wrap and justifying blocks of text. Our text editors do this for us - "wrap text to a width of 80 characters" and such. We've done challenges where we have made columns of text and we've also played with decolumnizing text. But this one's a bit different.
Given a block of text, can your program correctly identify the start of the next paragraph? You're free to use any heuristic you want. This one differs from previous challenges in that there is no whitespace between paragraphs like you had before. You may want to think about the statistics of lines the close a paragraph.
Challenge Input
The ability to securely access (replicate and distribute) directory
information throughout the network is necessary for successful
deployment. LDAP's acceptance as an access protocol for directory
information is driving the need to provide an access control model
definition for LDAP directory content among servers within an
enterprise and the Internet. Currently LDAP does not define an
access control model, but is needed to ensure consistent secure
access across heterogeneous LDAP implementations. The requirements
for access control are critical to the successful deployment and
acceptance of LDAP in the market place.
This section is divided into several areas of requirements: general,
semantics/policy, usability, and nested groups (an unresolved issue).
The requirements are not in any priority order. Examples and
explanatory text is provided where deemed necessary. Usability is
perhaps the one set of requirements that is generally overlooked, but
must be addressed to provide a secure system. Usability is a security
issue, not just a nice design goal and requirement. If it is
impossible to set and manage a policy for a secure situation that a
human can understand, then what was set up will probably be non-
secure. We all need to think of usability as a functional security
requirement.
Copyright (C) The Internet Society (2000). All Rights Reserved.
This document and translations of it may be copied and furnished to
others, and derivative works that comment on or otherwise explain it
or assist in its implementation may be prepared, copied, published
and distributed, in whole or in part, without restriction of any
kind, provided that the above copyright notice and this paragraph are
included on all such copies and derivative works. However, this
document itself may not be modified in any way, such as by removing
the copyright notice or references to the Internet Society or other
Internet organizations, except as needed for the purpose of
developing Internet standards in which case the procedures for
copyrights defined in the Internet Standards process must be
followed, or as required to translate it into languages other than
English.
The limited permissions granted above are perpetual and will not be
revoked by the Internet Society or its successors or assigns.
This document and the information contained herein is provided on an
"AS IS" basis and THE INTERNET SOCIETY AND THE INTERNET ENGINEERING
TASK FORCE DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING
BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION
HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.Challenge Output
Your program should emit something like this:
The ability to securely access (replicate and distribute) directory information throughout the network is necessary for successful deployment. LDAP's acceptance as an access protocol for directory information is driving the need to provide an access control model definition for LDAP directory content among servers within an enterprise and the Internet. Currently LDAP does not define an access control model, but is needed to ensure consistent secure access across heterogeneous LDAP implementations. The requirements for access control are critical to the successful deployment and acceptance of LDAP in the market place.
This section is divided into several areas of requirements: general, semantics/policy, usability, and nested groups (an unresolved issue). The requirements are not in any priority order. Examples and explanatory text is provided where deemed necessary. Usability is perhaps the one set of requirements that is generally overlooked, but must be addressed to provide a secure system. Usability is a security issue, not just a nice design goal and requirement. If it is impossible to set and manage a policy for a secure situation that a human can understand, then what was set up will probably be non- secure. We all need to think of usability as a functional security requirement.
Copyright (C) The Internet Society (2000). All Rights Reserved. This document and translations of it may be copied and furnished to others, and derivative works that comment on or otherwise explain it or assist in its implementation may be prepared, copied, published and distributed, in whole or in part, without restriction of any kind, provided that the above copyright notice and this paragraph are included on all such copies and derivative works. However, this document itself may not be modified in any way, such as by removing the copyright notice or references to the Internet Society or other Internet organizations, except as needed for the purpose of developing Internet standards in which case the procedures for copyrights defined in the Internet Standards process must be followed, or as required to translate it into languages other than English.
The limited permissions granted above are perpetual and will not be revoked by the Internet Society or its successors or assigns.
This document and the information contained herein is provided on an "AS IS" basis and THE INTERNET SOCIETY AND THE INTERNET ENGINEERING TASK FORCE DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
15
u/lukz 2 0 May 04 '18
Game boy assembly
Using the heuristics by /u/skeeto.
The cartridge ROM contains the input text. The text has been shortened to make the program shorter and the visual verification easier. The program writes the reformatted text into RAM at address C000h. We can verify it using the BGB emulator and its debugger.
Screenshot
8
u/skeeto -9 8 May 02 '18
C. If a line ends with punctuation and the following line's first word would have fit on this line, then it's considered the end of a paragraph and given an extra linebreak.
5
u/Godspiral 3 3 May 02 '18 edited May 02 '18
simple heuristic of last char on line is period, in J. There is a line feed after "All Rights Reserved." ?
using u/skeeto 's heuristic, gets a good answer.
4
u/Nyxisto May 02 '18 edited May 03 '18
F#, shamelessly stealing /u/skeeto 's heuristic
4
u/zatoichi49 May 02 '18 edited May 06 '18
Method:
Split the lines of the text into a list, and calculate the maximum line width. Going through each line in turn, if the line ends in punctuation and there's enough space left to hold the first word of the next line (without breaking the max line width), then add a line break. Return the string of all joined lines in the list.
Python 3:
6
u/thestoicattack May 02 '18
awk -- basically, if the first word of the next line would have fit on this line, we assume it's a paragraph break.
3
u/gandalfx May 02 '18 edited May 02 '18
Python3. If a line ends with a period and the first word on the next line would have fit without surpassing maximum line length it's the end of a paragraph.
Simple stdin/stdout based I/O:
Note: The input text uses double spaces between sentences. I don't think this is good style so I ignored it when connecting lines.
2
u/ProgrammingPython May 02 '18
C#. In hindsight I should have made a function that took a string and outputted a string instead of reading/writing from/to files. Feel free to critique if I did something wonky, I'm still learning.
2
u/ChazR May 02 '18 edited May 03 '18
Python 3 for a change.
I've designed this to have an easily extensible heuristic. One case that is likely to occur is a para ending with a full-stop inside quotes. I've made it simple to add rules to the heuristic to accommodate that sort of thing.
2
u/InSs4444nE May 03 '18 edited May 06 '18
Java
2
u/chunes 1 2 May 03 '18
A Factor solution. Since I didn't feel like copying skeeto's heuristic, I devised a hare-brained heuristic instead. Take the input, get its line lengths, and subtract the standard deviation of this data from the maximum line length: that is the threshold at which a line is considered suspiciously short and is likely a paragraph-ender.
For extra accuracy, I also stipulate that the line must end with punctuation. The second-last paragraph is not detected because it's long enough to beat my heuristic. But then again, I missed it when I scanned it with my eye, so I'm not too perturbed.
2
u/M4D5-Music May 04 '18
Java 8. My solution ended up with using more or less the same strategy as others. I noticed that the input as given has 2 spaces after most of its punctuation; I removed these extra spaces from my input.
2
u/octolanceae May 03 '18
C++
Reformats to 100 chars wide. Why 100? Why not?
1
u/TotalPerspective May 03 '18 edited May 03 '18
This should be input width agnostic.
Break if:
OR
Perl
Output
1
u/Dique_ May 03 '18
Made in ColdFusion :D
1
u/zqvt May 04 '18 edited May 04 '18
Haskell
1
u/shepherdjay May 04 '18
Python 3.6
Simply split lines and iterated. Before iteration I set a paragraph list and paragraph to an empty string. Then for each line checked if the string ending in
.and was below a certain length. If it was it was considered the end of the paragraph.https://github.com/shepherdjay/reddit_challenges/blob/challenge/359/challenges/challenge359_int.py
1
u/Gibby2 May 07 '18 edited Jun 03 '22
N RIAY YA OO
1
u/RiceCake6 May 08 '18
Python 3
Wasn't entirely sure if I was supposed to remove the newlines from the original string or not.
1
u/DEN0MINAT0R Jun 10 '18
C++
My first submission for C++. This program checks if the first word of each line would have fit on the previous line (based the highest character count line). It also checks to make sure that the previous line ended in a period. This heuristic isn't perfect, of course, for example if a paragraph ends, and the next word is very long, it might mistakenly think they belong together. Anyways, here's the code:
Output
1
u/2kofawsome Jun 30 '18
python3.6
I did not write out all the text into this comment, but for the real program it is all in there.