6
2
In this challenge, you will be given the source code of a Java program. Your task is to write a function or full program that will remove the whitespace from this program. The whitespace-removed program should work exactly the same as the original program. Your program can be in any language. In this challenge, every space, tab, and newline will be considered whitespace.
Summary
Input can be through command-line arguments, function parameters, or STDIN/equivalent.
Output will be the input program without whitespace through STDOUT/equivalent, along with an integer representing the amount of whitespace removed. Your score will be determined by (number of bytes) - (whitespace removed in sample program)
Sample Program (You can remove 104 whitespace characters from this)
class Condition {
public static void main(String[] args) {
boolean learning = true;
if (learning == true) {
System.out.println("Java programmer");
}
else {
System.out.println("What are you doing here?");
}
}
}
Whitespace-free version: class Condition{public static void main(String[]args){boolean learning=true;if(learning==true){System.out.println("Java programmer");}else{System.out.println("What are you doing here?");}}}
Clarifications from Comments (see below)
- The programs passed as input will be at the same whitespace complexity as the sample program.
- The programs will not contain comments.
Closing question
Due to the amount of confusion involved with the question, I have decided to close the competition and accept @MartinBüttner's answer. Sorry for any misunderstanding. However, I would believe the answers to be good as a tool for java golfers, which is why I did not delete the question.
1Note: If anyone believes this to be a code golf rather than a code challenge question, feel free to change the tag. – GamrCorps – 2015-10-09T19:35:19.273
3Does the program just have to work with this single test case? If not, how do we ensure that the program is actually correct for all possible Java programs? I'm sure there must be tons of edge cases in a language as elaborate as Java. (You don't even have weird mixes of strings, character literals and comments.) – Martin Ender – 2015-10-09T19:39:04.290
Make it work with programs on the same level of complexity as the sample one (same indentation rules, etc.). I never would expect programs that could handle removing all 93 whitespace characters from even the sample. – GamrCorps – 2015-10-09T19:51:32.043
3I hope someone does write an overly long program that detects/removes them all. I'm sure it won't win on score, but it would be nice to have a snippet to run on my golfs instead of doing it manually (or with some online minifier) ;) – Geobits – 2015-10-09T19:55:17.560
What about statements broken over multiple lines? Like
int\nfoo=5;
? – Martin Ender – 2015-10-09T20:59:37.813@MartinBüttner statements like those will be contained in a single line. – GamrCorps – 2015-10-09T21:13:53.853
@mbomb007 Strings should remain as is. – GamrCorps – 2015-10-09T21:14:15.840
Can you show us the fully whitespace-fixed version, please? – ETHproductions – 2015-10-09T21:49:26.453
Isn't actually possible to remove 104 whitespaces from the example, including newlines? Tested on a raw wgot of the linked file and I'm pretty sure I removed 104 characters, and it still compiles. – daniero – 2015-10-09T22:13:31.707
@daniero if you can manage to remove that much, feel free to edit the post – GamrCorps – 2015-10-09T22:14:40.187
@daniero I believe you've counted the newlines as
\r\n
, giving 22 characters for removing newlines, instead of just 11. – Martin Ender – 2015-10-10T08:00:05.977I'm still not sure what "the same whitespace complexity" means. Can there be double-spaces inside strings? Can there be double spaces inside statements or expressions, e.g.
int<SP><SP>foo = 5;
, where<SP>
is a space (otherwise the browser would display only one)? Currently the answers seem to make vastly different assumptions, so I'm this on hold as unclear until this is sorted out. – Martin Ender – 2015-10-10T08:07:40.923@MartinBüttner That's probably what I did. ı used
wc -c
to check the difference, on the linked raw file from pastebin. It's not really stated how to deal with this, and my platform doesn't recognize\r\n
as one character. I think counting bytes is a better option. I also addressed this in my own answer post below. – daniero – 2015-10-10T12:22:52.247@daniero sorry for the confusion, please read this the new section in the question – GamrCorps – 2015-10-10T14:15:04.730