Sign change, loop and display with minimal padding
Java 8, 483 480 486† 467 bytes
(a,b)->{int f=0,l=b-a+3,z[][]=new int[l][l],y[]=new int[l],i,j,k=0;for(;b-a>=0;k++,a++,b--,f^=1)for(j=0,i=a;i<=b;i++)z[k][j++]=f<1?i:-i;String r="",s;for(i=0;i<l;y[i++]=k)for(j=0,k=1;j<l;k=f>k?f:k)f=(r+z[j++][i]).length();for(i=0;i<l;i++){k=z[i][0];if(i>0&&k==z[i][1]&k==z[i-1][2])break;for(j=0;j<l;){k=z[i][j];s="";for(f=(s+k).length();f++<y[j];s+=" ");f=z[i][++j];if(k==f){r+=(i>0&&z[i-1][1]==z[i][1]?s+0:"")+"\n";j=l;}else r+=s+k+(f==z[i][j+1]?"":",");}}return r;}
† Bytes raised due to bug-fix..
Ok, this took A LOT more time (and bytes) than I thought (in Java that is..). This can definitely be golfed some more, probably by using a completely different approach instead of creating an NxN grid-array to fill and then 'strip out' the zeros (with an annoying edge case for the test-case of -1,1
, as well as -12,12
).
Try it online.
Explanation:
(a,b)->{ // Method with two integer parameters and String return-type
int f=0, // Flag-integer, starting at 0
l=b-a+3, // Size of the NxN matrix,
// plus two additional zeros (so we won't go OutOfBounds)
z[][]=new int[l][l],
// Integer-matrix (default filled with zeros)
y[] = new int[l],
// Temp integer-array to store the largest length per column
i,j,k=0; // Index-integers
for(;b-a>=0 // Loop as long as `b-a` is not negative yet
; // After every iteration:
k++, // Increase `k` by 1
a++, // Increase `a` by 1
b--, // Decrease `b` by 1
f^=1) // Toggle the flag-integer `f` (0→1 or 1→0)
for(j=0,i=a;i<=b;i++)
// Inner loop `i` in the range [`a`, `b`]
z[k][j++]=// Set all the values in the matrix to:
f<1? // If the flag is 0:
i // Simply use `i`
: // Else (flag is 1):
-i; // Use the negative form of `i` instead
String r="", // The return-String
s; // Temp-String used for the spaces
for(i=0;i<l; // Loop `i` over the rows of the matrix
;y[i++]=k)// After every iteration: Set the max column-width
for(j=0,k=1;j<l;
// Inner loop `j` over the cells of each row
k=f>k?f:k)
// After every iteration: Set `k` to the highest of `k` and `f`
f=(r+z[j++][i]).length();
// Determine current number's width
// (NOTE: `f` is no longer the flag, so we re-use it as temp value)
for(i=0;i<l;i++){
// Loop `i` over the rows of the matrix again
k=z[i][0]; // Set `k` to the first number of this row
if(i>0 // If this isn't the first row
&&k==z[i][1]&k==z[i-1][2])
// and the first number of this row, second number of this row,
// AND third number of the previous row all equal (all three are 0)
break; // Stop loop `i`
for(j=0;j<l;){
// Inner loop `j` over the cells of each row
k=z[i][j];// Set `k` to the number of the current cell
s=""; // Make String `s` empty again
for(f=(s+k).length();f++<y[j];s+=" ");
// Append the correct amount of spaces to `s`,
// based on the maximum width of this column, and the current number
f=z[i][++j];
// Go to the next cell, and set `f` to it's value
if(k==f){ // If the current number `k` equals the next number `f` (both are 0)
r+= // Append result-String `r` with:
(i>0 // If this isn't the first row
&&z[i-1][1]==z[i][1]?
// and the second number of this and the previous rows
// are the same (both are 0):
s+0 // Append the appropriate amount of spaces and a '0'
: // Else:
"") // Leave `r` the same
+"\n";// And append a new-line
j=l;} // And then stop the inner loop `j`
else // Else:
r+=s // Append result-String `r` with the appropriate amount of spaces
+k // and the number
+(f==z[i][j+1]?"":",");}}
// and a comma if it's not the last number of the row
return r;} // Return the result `r`
Jelly, 25 24 20 bytes
rµḊṖNµÐĿZbȷG€Ỵ€Zj€”,
This is a dyadic link that returns an array of rows.
Try it online!
How it works
rµḊṖNµÐĿZbȷG€Ỵ€Zj€”, Dyadic link. Arguments: a, b
r Range; yield [a, ..., b].
µ µÐĿ Apply the enclosed chain until the results are no longer
unique. Return the array of results.
Ḋ Dequeue; remove the first item.
Ṗ Pop; remove the last item.
N Negate; multiply all remaining integers by -1.
Z Zip; transpose rows and columns.
bȷ Base 1000; map each n to [n].
G€ Grid each; in each row, pad all integers to the same length,
separating the (singleton) rows by linefeeds.
Ỵ€ Split each result at linefeeds.
Z Zip to restore the original layout.
j€”, Join each row, separating by commata.
05AB1E, 59 bytes
Once again I'm screwed over by the same bug I wrote a fix for months ago but never pushed...
Golfing should still be possible though.
Ÿ[Ðg1‹#ˆ¦(¨]\\¯vy€g}})J.Bvyð0:S})øvyZs\})U¯vyvyXNèyg-ú}',ý,
Try it online!