8
I've got some Java pseudocode that uses whitespace instead of curly braces, and I want you to convert it.
I/O
Your program should take an input file along with a number designating how many spaces are used to indent a block. Here's an example:
$ convert.lang input.p 4 // Convert using 4 spaces as the block delimiter $ convert.lang input.p 2 // Convert using 2 spaces as the block delimiter
It should then convert the result using the specified block delimiter and output the result to stdout.
The meat of the program
Blocks open with :
and each line within the block is indented using the block delimiter, like Python code.
while(true): System.out.println("Test");
Each :
is replaced with a {
, and a }
is appended to the end of the block.
while(true) { System.out.println("Test"); }
Examples
Input:
public class Test: public static void main(String[] args): System.out.println("Java is verbose...");
Output:
$ convert Test.pseudojava 4 public class Test { public static void main(String[] args) { System.out.println("Java is verbose..."); } }
Input:
main(): printf("Hello World");
Output:
$ convert test.file 2 main() { printf("Hello World"); }
Input:
def generic_op(the_stack, func): # Generic op handling code b = the_stack.pop() if isinstance(b, list): if b: return top = b.pop(0) while b: top = func(top, b.pop(0)) the_stack.push(top) else: a = the_stack.pop() return func(a, b)
Output:
$ convert code.py 4 def generic_op(the_stack, func){ # Generic op handling code b = the_stack.pop() if isinstance(b, list) { if b { return } top = b.pop(0) while b { top = func(top, b.pop(0)) } the_stack.push(top) } else { a = the_stack.pop() return func(a, b) } }
Scoring
The code with the least amount of bytes wins!
1Can we assume that the input contains no comments? – Martin Ender – 2015-10-19T07:19:37.170
1@MartinBüttner It may contain comments, but the comments won't contain ':'. Basically, yes. – phase – 2015-10-19T07:32:45.460
3What about labels, which are the usual reason that a line would end in a colon in valid Java source? – Peter Taylor – 2015-10-19T08:11:29.410
1I've never seen a label anywhere but the beginning of a line. – SuperJedi224 – 2015-10-19T13:20:15.257
@PeterTaylor Just ignore those. Let's assume the programmer who made it knew not to use them. – phase – 2015-10-19T14:29:41.110
2I've just been reminded of how much I hate Java. – lirtosiast – 2015-11-16T04:46:57.947
Is the indentation block size a required parameter? – Not that Charles – 2015-11-16T07:49:31.063
@NotthatCharles not if you can detect it automatically, but otherwise yes – phase – 2015-11-16T07:50:34.063
Why would anyone want to convert OUT of <s>Python</s> pseudocode. – Morgan Thrapp – 2015-11-16T17:24:00.183
System.out.println("Java is verbose...");
ಠ_ಠ – user8397947 – 2016-06-26T20:56:05.523