Right hand brace is a style of code bracketing in which curly braces and semicolons are all aligned to a single point on the right side of a a file.
Generally, this is considered bad practice, for several reasons.
The Challenge
Take a multiline string through any method, and convert it's brace style to Right Hand Brace.
For this challenge, you only need it to work on Java code, however, it should theoretically work on any code that uses Braces and Semicolons.
You must grab all {};
characters in a row, with any amount of whitespace between them. EG. }}
, ; }
}\n\t\t}
, and line them up on the right side of the file through the use of whitespace.
for example:
a {
b;
{c
should become
a {
b ;{
c
Or, more abstractly, push any and all whitespace from the left of all {};
characters, to the right.
Indentation of lines should be otherwise preserved. Lines only containing whitespace after the movement of the {};
characters may optionally be removed.
For example:
a{
b{
c;
}
}
d;
May become either
a {
b {
c;}}
d ;
or
a {
b {
c;}}
d ;
Pushed to the right refers to all the {};
characters being aligned to a point no shorter than the longest line. Any amount of space after that is acceptable.
So all the below is acceptable:
a {
bc;
a {
bc ;
a {
bc ;
etc...
Lines in any code may contain {};
characters between other non-whitspace characters, the handling of this case isn't necessary, although if you're inclined, you should leave them in place. Lines may also not contain any {};
characters at all, and this should be handled correctly. As is shown below.
a {
b ;
c
d }
Because we don't want Code Review to see the horrible things we're doing, you need to make your code as small as possible.
Examples / Testcases
Generic Java
public class HelloWorld{
public static void main(String[] args){
System.out.println("Hello, World!");
}
}
becomes...
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!") ;}}
The image itself
public class Permuter{
private static void permute(int n, char[] a){
if (n == 0){
System.out.println(String.valueOf(a));
}else{
for (int i=0; i<= n; i++){
permute(n-1, a);
swap(a, n % 2 == 0 ? i : 0, n);
}
}
}
private static void swap(char[] a, int i, int j){
char saved = a[i];
a[i] = a[j];
a[j] = saved;
}
}
becomes...
public class Permuter {
private static void permute(int n, char[] a) {
if (n == 0) {
System.out.println(String.valueOf(a)) ;}
else {
for (int i=0; i<= n; i++) {
permute(n-1, a) ;
swap(a, n % 2 == 0 ? i : 0, n) ;}}}
private static void swap(char[] a, int i, int j) {
char saved = a[i] ;
a[i] = a[j] ;
a[j] = saved ;}}
Not so Perfectly Generic Python
For contrast
def Main():
print("Hello, World!");
Main();
becomes...
def Main():
print("Hello, World!") ;
Main() ;
Notes
- Standard Loopholes apply
- Standard IO applies
- This is code-golf, so shortest program in bytes wins!
- I am not Liable for damages related to programming in Right Hand Brace style
- Have Fun!
Edit Notes
I reworded the challenge details, Hopefully I didn't break anyone's view of the rules, I assure you it was unintentional. This should be a much more clear and less self-conflicting spec.
int a=0;System.out.println(a);
\$\endgroup\$;{}
characters gathered up if they're on separate lines (it's only clear from the example, not the rules, and in fact if a line consists of\t}
preserving the indentation would mean not moving}
up to the end of the previous line) \$\endgroup\$