24
1
End the tabs versus space war
So, there has been a great deal of debate of whether to use tabs or spaces to indent/format code. Can you help the university settle the dispute, by going to an incredibly crazy unique method of formatting.
Your job is to write a full program or function which expands all tabs into four spaces. And then replaces a run of n leading spaces with "/(n - two stars here)/". You will receive input over multiple lines in any reasonable format (single string array of strings for each new line. Columnar array etc.)
Sample input shamelessly stolen. Note that since tabs get automagically expanded to four spaces on SE I represent it as the "^" character, but you must handle tabs (codepoint 0x09) as well. All "^" characters represent a tabulation.
Calculate the value 256 and test if it's zero
If the interpreter errors on overflow this is where it'll happen
++++++++[>++++++++<-]>[<++++>-]
+<[>-<
Not zero so multiply by 256 again to get 65536
[>++++<-]>[<++++++++>-]<[>++++++++<-]
+>[>
# Print "32"
++++++++++[>+++++<-]>+.-.[-]<
<[-]<->] <[>>
# Print "16"
+++++++[>+++++++<-]>.+++++.[-]<
<<-]] >[>
# Print "8"
++++++++[>+++++++<-]>.[-]<
<-]<
# Print " bit cells\n"
+++++++++++[>+++>+++++++++>+++++++++>+<<<<-]>-.>-.+++++++.+++++++++++.<.
>>.++.+++++++..<-.>>-
Clean up used cells.
[[-]<]l
^this is preceded by a tab
^^two tabs
^^^three tabs etcetera!
Sample output
Calculate the value 256 and test if it's zero
If the interpreter errors on overflow this is where it'll happen
++++++++[>++++++++<-]>[<++++>-]
+<[>-<
/**/Not zero so multiply by 256 again to get 65536
/**/[>++++<-]>[<++++++++>-]<[>++++++++<-]
/**/+>[>
/******/# Print "32"
/******/++++++++++[>+++++<-]>+.-.[-]<
/**/<[-]<->] <[>>
/******/# Print "16"
/******/+++++++[>+++++++<-]>.+++++.[-]<
<<-]] >[>
/**/# Print "8"
/**/++++++++[>+++++++<-]>.[-]<
<-]<
# Print " bit cells\n"
+++++++++++[>+++>+++++++++>+++++++++>+<<<<-]>-.>-.+++++++.+++++++++++.<.
>>.++.+++++++..<-.>>-
Clean up used cells.
[[-]<]l
/**/this is preceded by a tab
/******/two tabs
/**********/three tabs etcetera!
Because the university needs space to download both Vim and Emacs, you are allowed very little storage for your code. Therefore this is code-golf and the shortest code wins. You may assume that input is well formed and lines with less than four spaces (after replacement of tabs) may result in undefined behavior.
Disclaimer
This "excellent" formatting strategy came courtesy of Geobits, and is reproduced with his permission. No programmers were harmed during the production of this challenge.
1Will tabs only occur at the start of lines (i.e. as indentation)? Can lines have mixed indentation (tabs + spaces)? – Lynn – 2016-09-01T02:08:35.553
20
Someone please submit an answer written in Whitespace.
– GuitarPicker – 2016-09-01T04:23:30.4172Should we consider lines starting with
/*
, or can that be assumed as not a "well formed input"? A C++ source file would have been a better test, because its multiline comment/* */
would possibly break some answers that replace first and last of the leading spaces with an/
, and then proceed to fill spaces with*
. – seshoumara – 2016-09-01T05:52:16.547I'm guessing we can't assume the number of spaces is a multiple of four, right? – someonewithpc – 2016-09-01T09:53:55.390
@someonewithpc no you may not – Rohan Jhunjhunwala – 2016-09-01T20:35:07.030
@sesvounara this formatting implementation need not result in compilable code. – Rohan Jhunjhunwala – 2016-09-01T20:36:05.317
1
The war has ended: https://medium.com/@hoffa/400-000-github-repositories-1-billion-files-14-terabytes-of-code-spaces-or-tabs-7cfe0b5dd7fd#.pmnalkp87 (Unless you're programming in C, apparently.)
– beaker – 2016-09-01T21:03:37.680@RohanJhunjhunwala Imagine this C++ line:
[4 spaces]int c; /* */
. According to your spec it should be transformed to/**/int c;/* */
, but wrong solutions when searching the regex/*
to fill spaces with asterisks between // might end up doing:/**/int c;/****/
. Is this considered a wrong output? – seshoumara – 2016-09-01T21:48:14.133@seshoumara yes that is incorrect output – Rohan Jhunjhunwala – 2016-09-01T23:33:59.340
1@RohanJhunjhunwala So now I ask my first question again, since it wasn't about compilable code. Imagine the same
/* */
C++ code, but this time at the beginning of the line. According to your spec it should be left as is. Here the trap is, and spotted wrong answers already, that a regex like say/\** /
used to fill those spaces between // with asterisks would turn the line into/***/
. I've seen this conversion as well/*//*/
. I assume both are incorrect. – seshoumara – 2016-09-01T23:51:29.800Could the "multiple line" also mean "a string separated by \n"? – Vale – 2016-09-02T08:03:39.540
@Rohan, please can you add a test-case for
/* */
->/* */
(i.e. no change for a line beginning with/*
), and for/* */
->/**//* */
? Thanks. Also fora
andb
(i.e. indentation less than 3 chars). – Toby Speight – 2016-09-02T14:26:59.987@Vale yes that is correct – Rohan Jhunjhunwala – 2016-09-03T20:51:42.817
@Toby you may assume that there will either be no indentation or more than four lines – Rohan Jhunjhunwala – 2016-09-03T20:53:24.317
@seshoumara both are incorrect – Rohan Jhunjhunwala – 2016-09-03T20:53:52.627