Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Revisions

7 of 7
Commonmark migration

MATL, (削除) 25 (削除ここまで) (削除) 22 (削除ここまで) 21 bytes

'|-+ '2:"1tiYsQ(]E!+)

Uses inputs with 1 added (allowed by the challenge).

Try it online!

Explanation

The code initially builds an array containing 1 for the column indices of non-space characters in the final result, and 0 otherwise. So if the first input is [2 1 4 1 3 1] (would be [1 0 3 0 2 0] in the 0-based format) this array will be

1 0 1 1 0 0 0 1 1 0 0 1 1

Note how the length of runs of zeros is related to the input. Specifically, this array is built as follows:

  1. Initiallize the array to a single 1.
  2. Compute the cumulative sum of the input and add 1. In the example this gives [3 4 8 9 12 13].
  3. Extend the array from step 1 by assigning 1 to the entries with (1-based) indices given by step 2. Intermediate entries are automatically set to 0.

A similar array is built for the rows. Second input [3 2 1 1] (or [2 1 0 0 ]) gives

1 0 0 1 0 1 1 1

Now the second array is multiplied by 2, transposed and added with broadcast to the first. This gives the 2D array

3 2 3 3 2 2 2 3 3 2 2 3 3
1 0 1 1 0 0 0 1 1 0 0 1 1
1 0 1 1 0 0 0 1 1 0 0 1 1
3 2 3 3 2 2 2 3 3 2 2 3 3
1 0 1 1 0 0 0 1 1 0 0 1 1
3 2 3 3 2 2 2 3 3 2 2 3 3
3 2 3 3 2 2 2 3 3 2 2 3 3
3 2 3 3 2 2 2 3 3 2 2 3 3

Indexing into the string '|-+ ' gives the final result as a 2D char array. Since indexing is modular and 1-based, index 0 corresponds to the last element (space).

'|-+ ' % Push this string
 2:" ] % Do this twice
 1 % Push 1 (initial array)
 t % Push another 1 (contents to be filled in)
 i % Take input
 Ys % Cumulative sum
 Q % Add 1
 ( % Fill 1 into those entries of the array
 E % Multiply by 2
 ! % Transpose
 + % Add, with broadcast
 ) % Index (modular, 1-based) into the string
Luis Mendo
  • 106.7k
  • 10
  • 139
  • 382

AltStyle によって変換されたページ (->オリジナル) /