6
\$\begingroup\$

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?");
        }
    }
}

http://pastebin.com/0kPdqZqZ

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.

\$\endgroup\$

closed as unclear what you're asking by Martin Ender Oct 10 '15 at 8:08

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    \$\begingroup\$ Note: If anyone believes this to be a code golf rather than a code challenge question, feel free to change the tag. \$\endgroup\$ – GamrCorps Oct 9 '15 at 19:35
  • 3
    \$\begingroup\$ Does 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.) \$\endgroup\$ – Martin Ender Oct 9 '15 at 19:39
  • \$\begingroup\$ 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. \$\endgroup\$ – GamrCorps Oct 9 '15 at 19:51
  • 3
    \$\begingroup\$ I 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) ;) \$\endgroup\$ – Geobits Oct 9 '15 at 19:55
  • \$\begingroup\$ What about statements broken over multiple lines? Like int\nfoo=5;? \$\endgroup\$ – Martin Ender Oct 9 '15 at 20:59
6
\$\begingroup\$

CJam, 25 bytes - 83 removed = -58

Since Java doesn't have multiline strings nor significant indentation, we can simply remove all leading spaces. Also, since there won't be any comments, and statements will not be split across lines (as per the OP's comments), I think I should be able to remove all newlines as well.

That makes for safe and short code with pretty decent savings for the test file:

q_,\N/{S\+e`1>e~}%_oNos,-

Test it here.

For the test case it yields

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?");}}}
83
\$\endgroup\$
  • \$\begingroup\$ C̶a̶n̶'̶t̶ ̶y̶o̶u̶ ̶a̶l̶s̶o̶ ̶r̶e̶m̶o̶v̶e̶ ̶n̶e̶w̶l̶i̶n̶e̶s̶?̶ ̶D̶o̶n̶'̶t̶ ̶k̶n̶o̶w̶ ̶C̶J̶a̶m̶ ̶t̶h̶o̶u̶g̶h̶,̶ ̶s̶o̶ ̶m̶a̶y̶b̶e̶ ̶i̶t̶ ̶w̶i̶l̶l̶ ̶b̶e̶ ̶w̶o̶r̶t̶h̶ ̶l̶e̶s̶s̶ ̶p̶o̶i̶n̶t̶s̶.̶ Sorry, realized that you can't always remove newlines in java. \$\endgroup\$ – Zereges Oct 9 '15 at 19:59
  • \$\begingroup\$ @Zereges If I can (waiting for clarification from the OP) it will actually save a byte of code (I'm explicitly writing the newlines back with the second N). \$\endgroup\$ – Martin Ender Oct 9 '15 at 20:03
  • \$\begingroup\$ Well, I found out, that my counter example did not work, so maybe you can remove newlines. You can also remove spaces before and after following symbols { } ( ) [ ] , ; = . and maybe some more \$\endgroup\$ – Zereges Oct 9 '15 at 20:11
  • 1
    \$\begingroup\$ @Zereges only if they don't appear inside strings. \$\endgroup\$ – Martin Ender Oct 9 '15 at 20:15
  • \$\begingroup\$ The Java Language Specification does not permit newlines in string literals. edit: oh, I see- that apparently wasn't a point of confusion. \$\endgroup\$ – JohnE Oct 9 '15 at 20:19
3
\$\begingroup\$

JavaScript (ES6), 72 bytes - 92 removed = -20

(partially invalid?)

x=>(z=x.replace(/\s*[{}()=;]\s*/g,y=>y.trim()))+`
`+(x.length-z.length)

The output:

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?");}}

I believe the one space I could still remove is the one in (String[] args), but this would not be worth it. This is pretty much the lowest score JS can get (for this specific case, anyway).

\$\endgroup\$
  • \$\begingroup\$ You forgot to print the amount of whitespace removed (just like I did) \$\endgroup\$ – daniero Oct 9 '15 at 22:51
  • \$\begingroup\$ For input programs with a string literal containing any of {}()=; next to whitespace, it alters the string. \$\endgroup\$ – DankMemes Oct 10 '15 at 2:01
2
\$\begingroup\$

Ruby, 63 bytes - 104 removed = -41

edit: 104 bytes that is.

$><<j=(i=$<.read).gsub(/(?<=\W)\s|\s(?=\W)/,"")
p i.size-j.size

Removes all newlines, so the printed number at the end comes on the same line too:

$ curl -s http://pastebin.com/raw.php?i=0kPdqZqZ | ruby remove_whitespace.rb 
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?");}}}104
\$\endgroup\$
  • \$\begingroup\$ Did you count the newlines as \r\n? Otherwise, I'm not sure how you'd get 104. \$\endgroup\$ – Martin Ender Oct 10 '15 at 7:59
  • \$\begingroup\$ Also, this would fail if there were two significant spaces in a row anywhere (e.g. in a string or int<SP><SP>foo = 5;) \$\endgroup\$ – Martin Ender Oct 10 '15 at 8:02
  • \$\begingroup\$ @MartinBüttner I used wc before and after - curl -s http://pastebin.com/raw.php?i=0kPdqZqZ | wc -c gives a difference of 104 if I don't print the numbers afterwards ("104\n"). As for the "significant spaces", there are none in the example so I think we're good (with "the same whitespace complexity" mentioned in the question). edit: I agree with putting the question on hold, this is all unclear. \$\endgroup\$ – daniero Oct 10 '15 at 12:13
1
\$\begingroup\$

Python, 92 bytes - 88 removed = 4

import re,sys
p=sys.stdin.read()
s=re.sub('\s+(=|{|}|(|))\s+',r'\1',p)
print len(p)-len(s),s

Test it here

Once again, regex to the rescue. There's a strange discrepancy between ideone and my python install - I'm getting 90 chars removed.

local python

jprog.py source:

#!/usr/bin/env python

print """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?");
        }
    }
}"""
\$\endgroup\$
1
\$\begingroup\$

Java 8 : 79 Bytes

s->{System.out.print(s.replaceAll("(\\s+|\\n)?(\\{|\\}|=+|;)(\\s+|\\n)", "$2"));}

Regular expression with grouping can remove the extra white spaces between curly braces and equals sign used in assignment or equality check.

Regular expression is something like, (\s+|\n)?({|}|=+|;)(\s+|\n)

It will provide following output, which is valid Java program.

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?");}}}
\$\endgroup\$
  • 1
    \$\begingroup\$ right tool for the job lol \$\endgroup\$ – Rohan Jhunjhunwala Jul 25 '16 at 0:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.