My homework assignment is take a string and split it into pieces at every new line. I have no idea what to do! Please help!
Note: This is a code-trolling question. Please do not take the question and/or answers seriously. More information here.
My homework assignment is take a string and split it into pieces at every new line. I have no idea what to do! Please help! Note: This is a code-trolling question. Please do not take the question and/or answers seriously. More information here. |
||||
|
PythonI feel so bad that you were given such an obvious trick question as homework. A highly advanced language such as Python makes this a simple two liner:
Please upvote and accept. | |||||
|
CIn C it's really easy:
Call it like this:
Working example:
Why it's evil:
| ||||
|
RubyWell you see first you have to make it into an array like this
Now you must put the elements as strings
Oh also remove that last comma
Oops forgot, you must put the brackets to be an array
Now just use the string and you are done
Evilness:
| |||||||||||||||||||||
|
Visual BasicThe
| |||||
|
Congratulations! Your string should now be split. If your string is still not split, repeat the steps until it's split. If you have repeated the steps a couple of times and the string is till not split, try grab a sharper scissor. DISCLAIMER: I am not responsible for any damage applied to you during the process. | |||||
|
Lua
Example input: Oh and i forgot to mention that the code is O(n^2) | |||||||||
|
Node.JSThis is so simple, any programmer could this -.-.
There ya go :) | |||||||||
|
PythonWe can iteratively use Python's string
Running the above script, we obtain the expected output (note that both the leading and trailing whitespace are consistent with splitting the string at every new line occurrence):
| |||
|
Bash script
This splits the string by newlines. If you echo the Sample output:
| |||
|
C# This uses the technology of recursion to transform the newlines into commas. The resulting CSV string can be easily split into an array.
| |||||||||||||||||
|
ANSI CThis is a standard assignment that all of us have done. This is the generally accepted solition.
You need to include the library with the correct function to split and print. Create the string you want to split:
The input: Creates the output:
| |||
|
C++
| |||
|
RubyStrings in programming are made of Einsteintanium. As such they're extremely hard to split.
This is evil because:
| |||||||||||||||||
|
Python 3 (Neat and Clean)
| |||||||||
|
JavaThis does not read from a file. Regular expressions is used. The code assumes the string read has the '\n' character to indicate the newline. The numbers 1,2,3,4 are used to indicate the split.
| |||
|
C#
| ||||
|
PHP / GDSplitting strings is a very complicated matter. Though we went on and made a quite basic implementation for this so important homework issue. Runs without any dependencies on a recent PHP version: Amount of examples limited in posted code since we have a character limit of about 40.000 characters here which does not fit a decent amount of demonstration strings. Example version: http://codepad.viper-7.com/YnGvCn Exactly confirms to your specifications.
| |||
|
PerlReplace the "any text to split \n next line\n another line" (without quotes) with the text you want to split:
CBut you can do it much simpler. In C, you can include on a header file containing the solution.
| |||
|
Java
This isn't really horrific, but still totally not done. | ||||
|
Java In Java you can achieve that by just 2 lines of code
| |||||||||
|
Python As you may know, Python is slow sometimes. But if you know some undocumented internal features, heavy operations like searching and splitting could be optimized. Way faster than regular expressions!
| |||
|
C
Tricky problem for a beginning C programming class! First you have to understand a few basics about this complicated subject. A string is a sequence made up of only characters. This means that in order for programmers to indicate an "invisible" thing (that isn't a space, which counts as a character), you have to use a special sequence of characters somehow to mean that invisible thing.
(Interesting historical note: on older Macintoshes it was a different sequence of four characters: "\n\r"... totally backwards from how Unix did things! History takes strange roads.) It may seem that Linux is more wasteful than Windows, but it's actually a better idea to use a longer sequence. Because Windows uses such a short sequence the C language runtime cannot print out the actual letters (Note: If you're wondering how we're talking about But back to what we're working on. Let's imagine a string with three pieces and two newlines, like:
You can see the length of that string is 3 + 2 + 3 + 2 + 3 = 13. So you have to make a buffer of length 13 for it, and C programmers always add one to the size of their arrays to be safe. So make your buffer and copy the string into it:
Now what you have to do is look for that two-character pattern that represents the newline. You aren't allowed to look for just a backslash. Because C is used for string splitting quite a lot, it will give you an error if you try. You can see this if you try writing:
(Note: There is a setting in the compiler for if you are writing a program that just looks for backslashes. But that's extremely uncommon; backslashes are very rarely used, which is why they were chosen for this purpose. We won't turn that switch on.) So let's make the pattern we really want, like this:
When we want to compare two strings which are of a certain length, we use So let's step through our buffer, one character at a time, looking for the two character match to our pattern. Each time we find a two-character sequence of a slash followed by an n, we'll use the very special system call (or "syscall")
The output of this program is the desired result...the string split!
|
I don't want to be mean, so here's a working piece of Python code that splits your string into pieces. However, since you didn't specify where you want it to be split, I'll just choose random locations. I hope that's ok with you. | |||
|
Haskell
| |||||||||||||||||
|
Python
Make sure that you append after the string length has been met otherwise you'll be missing part of the assignment. It should work like this:
| ||||
|