r/shittyprogramming • u/calsosta • Feb 14 '20
Friday Challenge: Given a paragraph of text, write a function that adds a new line after the end of each sentence
So here's a new thing. A weekly challenge to prove your abilities.
Solve the challenge and provide a working example or code. The shittiest example that most solves the problem wins something. Flair maybe, and bragging rights.
If you need help coming up with a paragraph to test your solution with here is a catalog of Who's The Boss? Fan Fiction that should give some interesting results.
Edit: Found some edge cases:
- File names with an extension
- Ellipsisess's (these things
...) - Questions
- Please preserve the sentence/question/exclamation terminator
Should be able to handle Who's the Boss? Fan fiction
- Example:
I like Who's the Boss? very much
- Example:
- A.M. and P.M. and other such abbreviations
- Per /u/HINDBRAIN new new lines should only appear at the end of sentences, questions, exclamations
57
Upvotes
30
u/mistermashu Feb 14 '20 edited Feb 14 '20
edit: updated for eliptickles
15
u/lfmarques2 Feb 14 '20 edited Feb 14 '20
You're welcome!
Python3:
EDIT: In function format
9
u/Michaukso Feb 14 '20
https://gist.github.com/jabczyk/eace9323a299f2499cf2d7dd0d2b67cb
5
u/Scriptman777 Feb 14 '20
This was fun! As a year 1 CS student, I probably can't make it more shitty, but I tried!
4
u/selplacei Feb 14 '20 edited Feb 15 '20
Edit: updated https://paste.rs/vMG.py
Didn't have enough budget for testing but it should work. Sorry for keeping the comments.
4
u/Phailjure Shitty Challenge Winner Feb 14 '20
Alright, pretty sure I got all the edge cases. C#. Had to make a new data structure to properly navigate the sentence, nothing built in would do it right, certainly not as efficiently as mine.
https://dotnetfiddle.net/6UPQhR Â
13
u/HINDBRAIN Feb 14 '20
You didn't mention ONLY after each sentence.
10
u/[deleted] Feb 14 '20
public static string AddStringAfterEverySentence(string inputString, string otherSentences) => new Func<string, string, string>((s, c) => {List<string> Strings = new List<string>(s.Split(new char[] { '.', '?', '!' }));Strings.ForEach((s) => { s = string.Format("{0} {1}", s, c.Split(new char[] { '.', '?', '!' })[new Random().Next(0, otherSentences.Split(new char[] { '.', '?', '!' }).Length)]); });return string.Join(" ", Strings.ToArray());}).Invoke(inputString, otherSentences);sorry my return key broke7
u/wizzwizz4 Feb 14 '20 edited Feb 14 '20
My code is cross-platform! It uses the command line aswell. My friend Dave helped a bit near the end.
EDIT: dave says that 24 at the end should be a 26.
3
u/Sokusan_123 Feb 14 '20
Oh man, I've got a brilliant idea for this. Machine learning solution dropping in exactly 10 hours. I need to do some data collection.
3
u/cobrabb Feb 15 '20
Here's my 1 line solution in ruby:
It's extremely efficient because the newline character is nowhere in the program, escaped or otherwise. And it's cross platform because it relies on to_s to figure out string formatting.
5
u/littleprof123 Feb 14 '20 edited Feb 14 '20
With sed-style find and replace (not tested):
EDIT: tested new version with proper escaping. Works in vim.
1
u/jbokwxguy Feb 14 '20
thisIsAString = "This is a pargraph? Or is it a paragraph. IT IS A PARAGRAPH!"notPuncuation = []punctuationMarks =[]indexOfNotPunctuation = []count = 0for c in thisIsAString:if c == '?':punctuationMarks.append(c)elif c == '.':punctuationMarks.append(c)elif c == '!':punctuationMarks.append(c)else:notPuncuation.append(c)indexOfNotPunctuation.append(count)count++newString=""for i in range(len(thisIsAString)):for punc in punctuationMarks:if thisIsAString[i] == punc:newString += punc + '\n'else:continuefor notPunc in notPuncuation:if thisIsAString[i] == notPunc:newString += notPunc + '\n'else:continueprint(thisIsAString)Not tested.. Should work. Unless typo.
1
u/vke85d Feb 16 '20
Sorry, but an ellipsis should always be written as a HORIZONTAL ELLIPSIS (U+2026). If you use three periods and my code treats it as three sentence terminations, the bug is in your data, not in my program.