64
4
I've heard that your code can run faster if you indent it in reverse, so that the compiler can process it like a tree design pattern from the very top of the "branches" down. This helps because gravity will speed up the time it takes for your code to be compiled, and the data structure efficiency is improved. Here's an example, in Java scripting:
function fib(n) {
var a = 1, b = 1;
while (--n > 0) {
var tmp = a;
a = b;
b += tmp;
if (a === Infinity) {
return "Error!";
}
}
return a;
}
But for some reason Notepad doesn't have a setting to do this automatically, so I need a program to do it for me.
Description
Submissions must take a code snippet as input, reverse the indentation, and output the resulting code.
This is done by the following procedure:
Split the code up into lines. Each line will start with zero or more spaces (there will be no tabs).
Find all unique indentation levels in the code. For example, for the above example, this would be
0 4 8 12
Reverse the order of this list of indentation levels, and map the reversed list to the original list. This is hard to explain in words, but for the example, it would look like
0 — 12 4 — 8 8 — 4 12 — 0
Apply this mapping to the original code. In the example, a line with 0-space-indentation would become indented by 12 spaces, 4 spaces would become 8 spaces, etc.
Input / Output
The input and output can be provided however you would like (STDIN/STDOUT, function parameter/return value, etc.); if your language does not support multiline input (or you just don't want to), you can use the |
character to separate lines instead.
The input will consist of only printable ASCII + newlines, and it will not contain empty lines.
Test cases
Input:
function fib(n) {
var a = 1, b = 1;
while (--n > 0) {
var tmp = a;
a = b;
b += tmp;
if (a === Infinity) {
return "Error!";
}
}
return a;
}
Output: the example code above.
Input:
a
b
c
d
e
f
g
h
Output:
a
b
c
d
e
f
g
h
Input:
1
2
3
2
1
Output:
1
2
3
2
1
Input:
foo
Output:
foo
21Its "JavaScript" not "Java scripting" :/ – Optimizer – 2014-12-18T21:36:59.977
75@Optimizer I see that my goal of infuriating as many people as possible with the first two paragraphs has been achieved. ;) – Doorknob – 2014-12-18T21:42:13.040
71 != as many people as possible. – Optimizer – 2014-12-18T21:44:50.380
WAT. How could anyone possibly think this is a good idea? – John Dvorak – 2014-12-18T21:49:54.363
23@JanDvorak The same guys that invented MLA style citations think this is a good idea. – Rainbolt – 2014-12-18T21:53:33.153
1
Are these the same guys who made the comefrom statement?
– FryAmTheEggman – 2014-12-18T22:03:06.830Are we allowed to append a single newline? – Martin Ender – 2014-12-18T23:15:33.270
6Supposedly, it's faster. Let's assign a committee to it and wait a few years while we forget the purpose of it. – Conor O'Brien – 2014-12-18T23:19:16.693
1This explains why Python is so slow... – MikeTheLiar – 2014-12-19T14:26:08.953
4I literally laughed out loud at "for some reason, Notepad doesn't do this automatically" – Greg d'Eon – 2014-12-19T19:30:24.837
Before anything, this made me cringe like hell. – Joe Z. – 2015-04-14T23:09:48.980
2Gravity pulls sideways? coooooool – Rɪᴋᴇʀ – 2016-04-02T00:03:40.200
1This is almost as bad as using spaces for indenting – 12Me21 – 2017-02-18T22:58:33.370
This is infuriating...good job – SomeShinyObject – 2017-06-07T03:57:21.710