Given a multidimensional array of positive integers where all dimensions are the same length, animate finding the centre of it.
Simply output the array, then remove the first and last items of every array within it and output it, until the length of each dimension is less than 3 and can't have the first and last removed.
For example, with this 2-dimensonal array:
[ [7, 2, 3, 6, 4],
[2, 4, 9, 9, 8],
[7, 3, 1, 9, 8],
[9, 1, 7, 6, 9],
[4, 3, 4, 8, 5] ]
You'd output the array, then remove the first and last of each to output this:
[ [4, 9, 9],
[3, 1, 9],
[1, 7, 6] ]
And finally output just this:
[ [ 1 ] ]
Output format can be as an array of multidimensional arrays, each array printed separately, etcetera.
You may output a trailing empty list, and you don't have to output the original array.
Testcases
[[[4, 6, 7], [1, 6, 8], [3, 3, 4]], [[9, 2, 8], [4, 9, 4], [6, 3, 9]], [[3, 7, 8], [4, 3, 6], [2, 8, 2]]] ->
[[[4, 6, 7], [1, 6, 8], [3, 3, 4]], [[9, 2, 8], [4, 9, 4], [6, 3, 9]], [[3, 7, 8], [4, 3, 6], [2, 8, 2]]]
[[[9]]]
[[[6, 5, 9, 4], [8, 8, 5, 1], [9, 3, 1, 6], [1, 7, 7, 9]], [[8, 3, 7, 2], [6, 6, 9, 4], [5, 6, 8, 3], [2, 1, 9, 9]], [[4, 9, 6, 9], [6, 9, 3, 7], [3, 6, 8, 2], [7, 5, 8, 6]], [[8, 6, 2, 1], [3, 5, 9, 5], [2, 6, 1, 3], [3, 6, 3, 9]]] ->
[[[6, 5, 9, 4], [8, 8, 5, 1], [9, 3, 1, 6], [1, 7, 7, 9]], [[8, 3, 7, 2], [6, 6, 9, 4], [5, 6, 8, 3], [2, 1, 9, 9]], [[4, 9, 6, 9], [6, 9, 3, 7], [3, 6, 8, 2], [7, 5, 8, 6]], [[8, 6, 2, 1], [3, 5, 9, 5], [2, 6, 1, 3], [3, 6, 3, 9]]]
[[[6, 9], [6, 8]], [[9, 3], [6, 8]]]
[[[[[5, 1, 6], [8, 2, 9], [9, 2, 4]], [[7, 1, 8], [1, 1, 5], [6, 9, 4]], [[3, 6, 6], [9, 9, 9], [4, 2, 5]]], [[[9, 9, 8], [7, 2, 7], [1, 2, 6]], [[3, 1, 8], [1, 7, 9], [6, 9, 2]], [[6, 6, 1], [6, 8, 8], [2, 5, 6]]], [[[9, 5, 3], [3, 6, 9], [8, 8, 7]], [[4, 6, 4], [8, 7, 9], [8, 6, 7]], [[2, 4, 1], [4, 5, 2], [8, 7, 8]]]], [[[[8, 1, 6], [1, 4, 4], [7, 6, 6]], [[9, 9, 9], [8, 1, 7], [9, 2, 4]], [[9, 5, 7], [1, 4, 8], [3, 8, 1]]], [[[3, 8, 1], [8, 7, 6], [1, 5, 7]], [[8, 5, 1], [7, 2, 1], [7, 1, 3]], [[1, 2, 6], [4, 3, 7], [1, 1, 1]]], [[[8, 1, 1], [7, 5, 2], [9, 3, 6]], [[2, 1, 5], [1, 3, 3], [5, 5, 2]], [[7, 6, 2], [9, 5, 7], [3, 8, 1]]]], [[[[5, 8, 9], [7, 2, 5], [3, 9, 7]], [[7, 8, 7], [3, 4, 1], [4, 1, 3]], [[3, 8, 6], [7, 7, 9], [8, 4, 8]]], [[[7, 5, 4], [8, 1, 7], [9, 9, 1]], [[1, 1, 8], [2, 3, 7], [9, 5, 4]], [[2, 2, 6], [8, 8, 2], [1, 3, 6]]], [[[7, 2, 7], [1, 7, 3], [8, 3, 9]], [[6, 1, 4], [8, 3, 8], [8, 9, 5]], [[5, 7, 2], [9, 7, 6], [1, 3, 4]]]]] ->
[[[[[5, 1, 6], [8, 2, 9], [9, 2, 4]], [[7, 1, 8], [1, 1, 5], [6, 9, 4]], [[3, 6, 6], [9, 9, 9], [4, 2, 5]]], [[[9, 9, 8], [7, 2, 7], [1, 2, 6]], [[3, 1, 8], [1, 7, 9], [6, 9, 2]], [[6, 6, 1], [6, 8, 8], [2, 5, 6]]], [[[9, 5, 3], [3, 6, 9], [8, 8, 7]], [[4, 6, 4], [8, 7, 9], [8, 6, 7]], [[2, 4, 1], [4, 5, 2], [8, 7, 8]]]], [[[[8, 1, 6], [1, 4, 4], [7, 6, 6]], [[9, 9, 9], [8, 1, 7], [9, 2, 4]], [[9, 5, 7], [1, 4, 8], [3, 8, 1]]], [[[3, 8, 1], [8, 7, 6], [1, 5, 7]], [[8, 5, 1], [7, 2, 1], [7, 1, 3]], [[1, 2, 6], [4, 3, 7], [1, 1, 1]]], [[[8, 1, 1], [7, 5, 2], [9, 3, 6]], [[2, 1, 5], [1, 3, 3], [5, 5, 2]], [[7, 6, 2], [9, 5, 7], [3, 8, 1]]]], [[[[5, 8, 9], [7, 2, 5], [3, 9, 7]], [[7, 8, 7], [3, 4, 1], [4, 1, 3]], [[3, 8, 6], [7, 7, 9], [8, 4, 8]]], [[[7, 5, 4], [8, 1, 7], [9, 9, 1]], [[1, 1, 8], [2, 3, 7], [9, 5, 4]], [[2, 2, 6], [8, 8, 2], [1, 3, 6]]], [[[7, 2, 7], [1, 7, 3], [8, 3, 9]], [[6, 1, 4], [8, 3, 8], [8, 9, 5]], [[5, 7, 2], [9, 7, 6], [1, 3, 4]]]]]
[[[[[2]]]]]
[[[[1, 8, 1, 7, 8], [2, 6, 6, 2, 8], [9, 4, 2, 3, 3], [1, 9, 5, 8, 5], [6, 3, 9, 2, 2]], [[8, 2, 5, 5, 3], [3, 3, 8, 6, 6], [3, 3, 5, 6, 6], [4, 2, 5, 3, 6], [2, 2, 5, 5, 4]], [[4, 4, 1, 9, 7], [4, 3, 4, 3, 8], [3, 7, 1, 7, 7], [1, 2, 7, 5, 6], [5, 6, 5, 4, 6]], [[7, 8, 9, 7, 2], [9, 4, 9, 9, 2], [3, 1, 9, 2, 9], [3, 5, 8, 4, 7], [3, 3, 5, 9, 9]], [[5, 6, 2, 4, 3], [2, 9, 6, 6, 1], [6, 8, 2, 9, 7], [5, 2, 1, 8, 4], [7, 7, 9, 6, 1]]], [[[2, 2, 8, 1, 6], [3, 6, 9, 9, 9], [9, 7, 6, 8, 7], [9, 6, 3, 5, 6], [9, 1, 8, 5, 6]], [[2, 4, 2, 7, 3], [8, 9, 8, 7, 1], [1, 6, 1, 8, 4], [9, 9, 8, 8, 2], [5, 5, 7, 9, 5]], [[6, 4, 3, 1, 2], [6, 4, 4, 7, 6], [7, 7, 6, 1, 3], [8, 7, 1, 4, 7], [4, 7, 1, 1, 2]], [[1, 7, 7, 5, 3], [7, 8, 4, 1, 1], [8, 5, 7, 5, 3], [6, 9, 1, 9, 6], [5, 9, 7, 3, 1]], [[6, 3, 7, 1, 5], [1, 9, 4, 8, 2], [9, 6, 5, 9, 5], [2, 9, 4, 4, 6], [8, 3, 6, 7, 4]]], [[[2, 4, 7, 7, 8], [4, 2, 9, 4, 8], [5, 4, 2, 1, 3], [1, 6, 8, 1, 1], [4, 7, 4, 9, 5]], [[4, 9, 1, 3, 7], [3, 3, 7, 6, 4], [4, 6, 7, 4, 5], [7, 3, 2, 7, 6], [5, 5, 7, 9, 3]], [[7, 6, 7, 4, 1], [8, 3, 4, 7, 3], [1, 9, 9, 2, 4], [2, 7, 2, 8, 1], [5, 1, 4, 8, 8]], [[3, 5, 4, 8, 2], [1, 2, 7, 4, 5], [1, 7, 2, 2, 9], [8, 4, 2, 1, 4], [4, 9, 2, 3, 5]], [[1, 1, 9, 7, 8], [4, 8, 1, 7, 5], [3, 5, 2, 2, 7], [8, 6, 3, 2, 1], [6, 9, 9, 1, 3]]], [[[2, 8, 5, 5, 6], [6, 3, 4, 6, 4], [2, 3, 5, 7, 9], [2, 6, 1, 1, 2], [6, 4, 9, 8, 5]], [[8, 4, 7, 2, 1], [6, 5, 4, 8, 7], [7, 8, 3, 6, 8], [2, 2, 4, 7, 2], [2, 3, 6, 4, 4]], [[5, 2, 8, 4, 8], [6, 7, 2, 8, 7], [8, 2, 1, 9, 2], [2, 1, 1, 7, 2], [9, 6, 1, 5, 2]], [[3, 2, 9, 2, 2], [9, 4, 5, 1, 2], [9, 1, 6, 3, 7], [8, 8, 7, 1, 8], [3, 4, 8, 9, 6]], [[8, 6, 1, 6, 9], [1, 9, 6, 9, 4], [6, 6, 6, 9, 6], [2, 5, 3, 6, 5], [6, 6, 9, 5, 7]]], [[[2, 4, 3, 6, 1], [6, 8, 2, 8, 8], [5, 2, 9, 7, 7], [1, 3, 5, 6, 2], [5, 3, 5, 7, 9]], [[5, 3, 8, 8, 5], [2, 7, 5, 3, 7], [2, 5, 1, 6, 6], [5, 7, 1, 4, 2], [5, 1, 4, 5, 2]], [[1, 6, 2, 5, 3], [3, 8, 1, 9, 4], [3, 9, 6, 7, 1], [8, 5, 3, 7, 1], [3, 8, 8, 1, 4]], [[3, 1, 2, 1, 9], [5, 7, 1, 4, 2], [1, 8, 5, 7, 3], [2, 5, 8, 2, 1], [9, 9, 6, 9, 7]], [[4, 8, 3, 5, 6], [5, 6, 4, 2, 3], [2, 5, 3, 1, 6], [2, 9, 4, 9, 4], [4, 6, 8, 2, 2]]]] ->
[[[[1, 8, 1, 7, 8], [2, 6, 6, 2, 8], [9, 4, 2, 3, 3], [1, 9, 5, 8, 5], [6, 3, 9, 2, 2]], [[8, 2, 5, 5, 3], [3, 3, 8, 6, 6], [3, 3, 5, 6, 6], [4, 2, 5, 3, 6], [2, 2, 5, 5, 4]], [[4, 4, 1, 9, 7], [4, 3, 4, 3, 8], [3, 7, 1, 7, 7], [1, 2, 7, 5, 6], [5, 6, 5, 4, 6]], [[7, 8, 9, 7, 2], [9, 4, 9, 9, 2], [3, 1, 9, 2, 9], [3, 5, 8, 4, 7], [3, 3, 5, 9, 9]], [[5, 6, 2, 4, 3], [2, 9, 6, 6, 1], [6, 8, 2, 9, 7], [5, 2, 1, 8, 4], [7, 7, 9, 6, 1]]], [[[2, 2, 8, 1, 6], [3, 6, 9, 9, 9], [9, 7, 6, 8, 7], [9, 6, 3, 5, 6], [9, 1, 8, 5, 6]], [[2, 4, 2, 7, 3], [8, 9, 8, 7, 1], [1, 6, 1, 8, 4], [9, 9, 8, 8, 2], [5, 5, 7, 9, 5]], [[6, 4, 3, 1, 2], [6, 4, 4, 7, 6], [7, 7, 6, 1, 3], [8, 7, 1, 4, 7], [4, 7, 1, 1, 2]], [[1, 7, 7, 5, 3], [7, 8, 4, 1, 1], [8, 5, 7, 5, 3], [6, 9, 1, 9, 6], [5, 9, 7, 3, 1]], [[6, 3, 7, 1, 5], [1, 9, 4, 8, 2], [9, 6, 5, 9, 5], [2, 9, 4, 4, 6], [8, 3, 6, 7, 4]]], [[[2, 4, 7, 7, 8], [4, 2, 9, 4, 8], [5, 4, 2, 1, 3], [1, 6, 8, 1, 1], [4, 7, 4, 9, 5]], [[4, 9, 1, 3, 7], [3, 3, 7, 6, 4], [4, 6, 7, 4, 5], [7, 3, 2, 7, 6], [5, 5, 7, 9, 3]], [[7, 6, 7, 4, 1], [8, 3, 4, 7, 3], [1, 9, 9, 2, 4], [2, 7, 2, 8, 1], [5, 1, 4, 8, 8]], [[3, 5, 4, 8, 2], [1, 2, 7, 4, 5], [1, 7, 2, 2, 9], [8, 4, 2, 1, 4], [4, 9, 2, 3, 5]], [[1, 1, 9, 7, 8], [4, 8, 1, 7, 5], [3, 5, 2, 2, 7], [8, 6, 3, 2, 1], [6, 9, 9, 1, 3]]], [[[2, 8, 5, 5, 6], [6, 3, 4, 6, 4], [2, 3, 5, 7, 9], [2, 6, 1, 1, 2], [6, 4, 9, 8, 5]], [[8, 4, 7, 2, 1], [6, 5, 4, 8, 7], [7, 8, 3, 6, 8], [2, 2, 4, 7, 2], [2, 3, 6, 4, 4]], [[5, 2, 8, 4, 8], [6, 7, 2, 8, 7], [8, 2, 1, 9, 2], [2, 1, 1, 7, 2], [9, 6, 1, 5, 2]], [[3, 2, 9, 2, 2], [9, 4, 5, 1, 2], [9, 1, 6, 3, 7], [8, 8, 7, 1, 8], [3, 4, 8, 9, 6]], [[8, 6, 1, 6, 9], [1, 9, 6, 9, 4], [6, 6, 6, 9, 6], [2, 5, 3, 6, 5], [6, 6, 9, 5, 7]]], [[[2, 4, 3, 6, 1], [6, 8, 2, 8, 8], [5, 2, 9, 7, 7], [1, 3, 5, 6, 2], [5, 3, 5, 7, 9]], [[5, 3, 8, 8, 5], [2, 7, 5, 3, 7], [2, 5, 1, 6, 6], [5, 7, 1, 4, 2], [5, 1, 4, 5, 2]], [[1, 6, 2, 5, 3], [3, 8, 1, 9, 4], [3, 9, 6, 7, 1], [8, 5, 3, 7, 1], [3, 8, 8, 1, 4]], [[3, 1, 2, 1, 9], [5, 7, 1, 4, 2], [1, 8, 5, 7, 3], [2, 5, 8, 2, 1], [9, 9, 6, 9, 7]], [[4, 8, 3, 5, 6], [5, 6, 4, 2, 3], [2, 5, 3, 1, 6], [2, 9, 4, 9, 4], [4, 6, 8, 2, 2]]]]
[[[[9, 8, 7], [6, 1, 8], [9, 8, 8]], [[4, 4, 7], [7, 6, 1], [7, 1, 4]], [[8, 4, 1], [5, 7, 5], [9, 1, 9]]], [[[3, 7, 6], [6, 7, 4], [3, 2, 7]], [[3, 4, 7], [9, 9, 2], [7, 2, 8]], [[2, 7, 4], [7, 2, 2], [4, 2, 1]]], [[[5, 4, 8], [8, 3, 6], [2, 4, 7]], [[7, 2, 8], [2, 1, 9], [1, 1, 7]], [[4, 5, 1], [1, 6, 3], [8, 7, 1]]]]
[[[[9]]]]
-
\$\begingroup\$ Can we assume anything about the inputs, such as positive, non-zero, etc.? (re. suggested edit) \$\endgroup\$hyperneutrino– hyperneutrino ♦2022年01月28日 04:38:38 +00:00Commented Jan 28, 2022 at 4:38
-
\$\begingroup\$ Can we output a final empty list if the input has even length? \$\endgroup\$att– att2022年01月28日 06:47:44 +00:00Commented Jan 28, 2022 at 6:47
-
\$\begingroup\$ @att Sure, that's fine \$\endgroup\$emanresu A– emanresu A2022年01月28日 06:58:07 +00:00Commented Jan 28, 2022 at 6:58
-
\$\begingroup\$ "You may output a trailing empty list on testcases with even size." Why only with even size test cases?.. Can I always output a trailing empty list, even with odd size test cases? \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2022年01月28日 08:57:20 +00:00Commented Jan 28, 2022 at 8:57
-
\$\begingroup\$ Can we skip outputting the initial array, and only output subsequent steps? \$\endgroup\$Surculose Sputum– Surculose Sputum2022年01月28日 18:04:31 +00:00Commented Jan 28, 2022 at 18:04
15 Answers 15
Wolfram Language (Mathematica), (削除) 47 (削除ここまで) 44 bytes
#//.l_:>(Print@l;#0/@#[[2;;-2]]~Check~#&@l)&
Prints the steps, one per line. Includes a final empty list if the input has even length
# starting from input,
//. until a fixed point is reached:
l_:>(Print@l; ) output current value
#0/@#[[2;;-2]] &@l recursively take its middle on every level
~Check~# or unchanged if length<2
(this also handles recursion base case)
43 bytes, non-competing
{#}//.l_:>{#0/@#[[2;;-2]]~Check~#&@@l}⋃l&
Returns the lists in reverse order. I don't expect this is allowed, but it's an interesting variation.
-
\$\begingroup\$ Why not print to stdout instead of returning the result, so that you can
Echo
each step? \$\endgroup\$alephalpha– alephalpha2022年01月28日 08:23:12 +00:00Commented Jan 28, 2022 at 8:23 -
\$\begingroup\$ @alephalpha Besides my not liking to use
Echo
, no particular reason. \$\endgroup\$att– att2022年01月28日 21:07:06 +00:00Commented Jan 28, 2022 at 21:07
APL(Dyalog Unicode), (削除) (削除ここまで)17 bytes SBCS
×ばつ⍴⎕←⍵}⍣≡
A dfn submission which prints each iteration. Input is a multidimensional array.
-14 from ovs.
-
\$\begingroup\$ If you pass a vector left argument to
↓
, you can drop along multiple axes:{h↓⍵↓⍨-h←×⍴⎕←⍵}⍣≡
\$\endgroup\$ovs– ovs2022年01月28日 09:13:49 +00:00Commented Jan 28, 2022 at 9:13 -
\$\begingroup\$ I suppose axis specification never really is golfy \$\endgroup\$Razetime– Razetime2022年01月28日 09:18:17 +00:00Commented Jan 28, 2022 at 9:18
Pip -xp
, (削除) 25 (削除ここまで) (削除) 24 (削除ここまで) 22 bytes
W@<P@ggM:{0*a?fM@<Saa}
Takes the input array as a command line argument; outputs each step on a separate line. Gives an empty list at the end for even-size test-cases, as permitted. Attempt This Online!
Explanation
This is for a somewhat more straightforward 23-byte version. The 22-byte version works the same way, except that the main program manipulates g
(the list of all command-line args--in our case, a singleton list) instead of a
(the first command-line arg). This ends up saving a byte because Pip's function-call syntax is a bit verbose.
W@<Paa:({...}a)
; a is 1st cmdline arg (evaluated, thanks to -x flag)
W ; While...
a ; Current array
P ; Printed (in list format, thanks to -p flag)
@< ; Minus its last element
; ...is truthy (nonempty list), loop:
( ) ; Call
{...} ; This function (see below)
a ; With argument a
a: ; Assign the result back to a
{0*a?fM@<Saa} ; Recursive function to strip one hypercube layer:
a ; The function argument
0* ; Times 0 (either 0 for integer a, or list of 0s for list a)
? ; If truthy, the argument is a nonempty list:
Sa ; Argument minus its first element
@< ; Minus its last element
fM ; Map this function recursively to each remaining element
; Else, the argument is an integer:
a ; Return it unchanged
APL (Dyalog Unicode), 18 bytes
Anonymous tacit prefix function.
{⍵(⊢↓↓⍨∘-)×ばつ⍴⎕←⍵}⍣≡
{
...}⍣≡
apply lambda until stable
⎕←⍵
print the argument
⍴
get its shape
×ばつ
signum of that (gives list with one 1 per dimension)
⍵(
...)
apply the following tacit function with the argument as left argument:
...∘-
negate the right argument (the number of elements to drop, so they'll be dropped from the rear
↓⍨
drop that many elements from the left argument
⊢↓
drop right-argument elements from that
Python + NumPy, 74 bytes --> 64 bytes (@DLosc)
def x(a):
if len(a):print(a);x(a[(slice(1,-1),)*len(a.shape)])
Thank's @DLosc for removing the return. Nice spot.
-
2\$\begingroup\$ Welcome to Code Golf! Nice answer. \$\endgroup\$2022年01月28日 16:12:16 +00:00Commented Jan 28, 2022 at 16:12
-
1\$\begingroup\$ By removing
return
and using one space for the indent, you can get this down to 63 bytes: Try it online! (I used a smaller test case so the link would fit in the comment.) \$\endgroup\$DLosc– DLosc2022年01月28日 16:27:42 +00:00Commented Jan 28, 2022 at 16:27 -
\$\begingroup\$ Based on @DLosc 's golfing. This can be 61 bytes
def x(a):len(a)and x(a[(slice(1,-1,print(a)),)*len(a.shape)])
\$\endgroup\$tsh– tsh2022年01月29日 06:07:00 +00:00Commented Jan 29, 2022 at 6:07
JavaScript, 53 bytes
f=a=>[a,...g(a)+''&&f(b)]
g=a=>b=a.map?.(g).slice(1,-1)??a
f=a=>[a,...g(a)+''&&f(b)]
g=a=>b=a.map?.(g).slice(1,-1)??a
console.log(JSON.stringify(f([[[4, 6, 7], [1, 6, 8], [3, 3, 4]], [[9, 2, 8], [4, 9, 4], [6, 3, 9]], [[3, 7, 8], [4, 3, 6], [2, 8, 2]]])))
console.log(JSON.stringify(f([[[6, 5, 9, 4], [8, 8, 5, 1], [9, 3, 1, 6], [1, 7, 7, 9]], [[8, 3, 7, 2], [6, 6, 9, 4], [5, 6, 8, 3], [2, 1, 9, 9]], [[4, 9, 6, 9], [6, 9, 3, 7], [3, 6, 8, 2], [7, 5, 8, 6]], [[8, 6, 2, 1], [3, 5, 9, 5], [2, 6, 1, 3], [3, 6, 3, 9]]])))
console.log(JSON.stringify(f([[[[[5, 1, 6], [8, 2, 9], [9, 2, 4]], [[7, 1, 8], [1, 1, 5], [6, 9, 4]], [[3, 6, 6], [9, 9, 9], [4, 2, 5]]], [[[9, 9, 8], [7, 2, 7], [1, 2, 6]], [[3, 1, 8], [1, 7, 9], [6, 9, 2]], [[6, 6, 1], [6, 8, 8], [2, 5, 6]]], [[[9, 5, 3], [3, 6, 9], [8, 8, 7]], [[4, 6, 4], [8, 7, 9], [8, 6, 7]], [[2, 4, 1], [4, 5, 2], [8, 7, 8]]]], [[[[8, 1, 6], [1, 4, 4], [7, 6, 6]], [[9, 9, 9], [8, 1, 7], [9, 2, 4]], [[9, 5, 7], [1, 4, 8], [3, 8, 1]]], [[[3, 8, 1], [8, 7, 6], [1, 5, 7]], [[8, 5, 1], [7, 2, 1], [7, 1, 3]], [[1, 2, 6], [4, 3, 7], [1, 1, 1]]], [[[8, 1, 1], [7, 5, 2], [9, 3, 6]], [[2, 1, 5], [1, 3, 3], [5, 5, 2]], [[7, 6, 2], [9, 5, 7], [3, 8, 1]]]], [[[[5, 8, 9], [7, 2, 5], [3, 9, 7]], [[7, 8, 7], [3, 4, 1], [4, 1, 3]], [[3, 8, 6], [7, 7, 9], [8, 4, 8]]], [[[7, 5, 4], [8, 1, 7], [9, 9, 1]], [[1, 1, 8], [2, 3, 7], [9, 5, 4]], [[2, 2, 6], [8, 8, 2], [1, 3, 6]]], [[[7, 2, 7], [1, 7, 3], [8, 3, 9]], [[6, 1, 4], [8, 3, 8], [8, 9, 5]], [[5, 7, 2], [9, 7, 6], [1, 3, 4]]]]])))
console.log(JSON.stringify(f([[[[1, 8, 1, 7, 8], [2, 6, 6, 2, 8], [9, 4, 2, 3, 3], [1, 9, 5, 8, 5], [6, 3, 9, 2, 2]], [[8, 2, 5, 5, 3], [3, 3, 8, 6, 6], [3, 3, 5, 6, 6], [4, 2, 5, 3, 6], [2, 2, 5, 5, 4]], [[4, 4, 1, 9, 7], [4, 3, 4, 3, 8], [3, 7, 1, 7, 7], [1, 2, 7, 5, 6], [5, 6, 5, 4, 6]], [[7, 8, 9, 7, 2], [9, 4, 9, 9, 2], [3, 1, 9, 2, 9], [3, 5, 8, 4, 7], [3, 3, 5, 9, 9]], [[5, 6, 2, 4, 3], [2, 9, 6, 6, 1], [6, 8, 2, 9, 7], [5, 2, 1, 8, 4], [7, 7, 9, 6, 1]]], [[[2, 2, 8, 1, 6], [3, 6, 9, 9, 9], [9, 7, 6, 8, 7], [9, 6, 3, 5, 6], [9, 1, 8, 5, 6]], [[2, 4, 2, 7, 3], [8, 9, 8, 7, 1], [1, 6, 1, 8, 4], [9, 9, 8, 8, 2], [5, 5, 7, 9, 5]], [[6, 4, 3, 1, 2], [6, 4, 4, 7, 6], [7, 7, 6, 1, 3], [8, 7, 1, 4, 7], [4, 7, 1, 1, 2]], [[1, 7, 7, 5, 3], [7, 8, 4, 1, 1], [8, 5, 7, 5, 3], [6, 9, 1, 9, 6], [5, 9, 7, 3, 1]], [[6, 3, 7, 1, 5], [1, 9, 4, 8, 2], [9, 6, 5, 9, 5], [2, 9, 4, 4, 6], [8, 3, 6, 7, 4]]], [[[2, 4, 7, 7, 8], [4, 2, 9, 4, 8], [5, 4, 2, 1, 3], [1, 6, 8, 1, 1], [4, 7, 4, 9, 5]], [[4, 9, 1, 3, 7], [3, 3, 7, 6, 4], [4, 6, 7, 4, 5], [7, 3, 2, 7, 6], [5, 5, 7, 9, 3]], [[7, 6, 7, 4, 1], [8, 3, 4, 7, 3], [1, 9, 9, 2, 4], [2, 7, 2, 8, 1], [5, 1, 4, 8, 8]], [[3, 5, 4, 8, 2], [1, 2, 7, 4, 5], [1, 7, 2, 2, 9], [8, 4, 2, 1, 4], [4, 9, 2, 3, 5]], [[1, 1, 9, 7, 8], [4, 8, 1, 7, 5], [3, 5, 2, 2, 7], [8, 6, 3, 2, 1], [6, 9, 9, 1, 3]]], [[[2, 8, 5, 5, 6], [6, 3, 4, 6, 4], [2, 3, 5, 7, 9], [2, 6, 1, 1, 2], [6, 4, 9, 8, 5]], [[8, 4, 7, 2, 1], [6, 5, 4, 8, 7], [7, 8, 3, 6, 8], [2, 2, 4, 7, 2], [2, 3, 6, 4, 4]], [[5, 2, 8, 4, 8], [6, 7, 2, 8, 7], [8, 2, 1, 9, 2], [2, 1, 1, 7, 2], [9, 6, 1, 5, 2]], [[3, 2, 9, 2, 2], [9, 4, 5, 1, 2], [9, 1, 6, 3, 7], [8, 8, 7, 1, 8], [3, 4, 8, 9, 6]], [[8, 6, 1, 6, 9], [1, 9, 6, 9, 4], [6, 6, 6, 9, 6], [2, 5, 3, 6, 5], [6, 6, 9, 5, 7]]], [[[2, 4, 3, 6, 1], [6, 8, 2, 8, 8], [5, 2, 9, 7, 7], [1, 3, 5, 6, 2], [5, 3, 5, 7, 9]], [[5, 3, 8, 8, 5], [2, 7, 5, 3, 7], [2, 5, 1, 6, 6], [5, 7, 1, 4, 2], [5, 1, 4, 5, 2]], [[1, 6, 2, 5, 3], [3, 8, 1, 9, 4], [3, 9, 6, 7, 1], [8, 5, 3, 7, 1], [3, 8, 8, 1, 4]], [[3, 1, 2, 1, 9], [5, 7, 1, 4, 2], [1, 8, 5, 7, 3], [2, 5, 8, 2, 1], [9, 9, 6, 9, 7]], [[4, 8, 3, 5, 6], [5, 6, 4, 2, 3], [2, 5, 3, 1, 6], [2, 9, 4, 9, 4], [4, 6, 8, 2, 2]]]])))
Python 3, 75 bytes
f=lambda x:[x]+(x[2:]and f(q(x)))
q=lambda x:0!=x*0and[*map(q,x[1:-1])]or x
-7 bytes + fixed thanks to DLosc
-
\$\begingroup\$ Let us continue this discussion in chat. \$\endgroup\$DialFrost– DialFrost2022年01月28日 06:24:36 +00:00Commented Jan 28, 2022 at 6:24
Wolfram Language, (削除) 34 (削除ここまで) 32 bytes
#~ArrayPad~-1&~FixedPointList~#&
ArrayPad[array,-1]
negatively pad 1 element on every side regardless the dimension ofarray
.FixedPointList[f, x]
applies functionf
onx
repeatedly, until a fixed-point off
is reached.
The result is collected in a List
, extra code (Grid[...]
) is needed for aesthetic view.
Example for Notebook environment:
{{7, 2, 3, 6, 4}, {2, 4, 9, 9, 8}, {7, 3, 1, 9, 8}, {9, 1, 7, 6, 9}, {4, 3, 4, 8, 5}} //
#~ArrayPad~-1&~FixedPointList~#& //
Grid[#,Dividers->{False,Center},Alignment->Left]&
-
1\$\begingroup\$ Nice one, I didn't know
ArrayPad
did that.#//.l_:>ArrayPad[Print@l;l,-1]&
for 31 bytes printing output (or 27 if you're ok withEcho
instead ofPrint
). \$\endgroup\$att– att2022年01月30日 06:55:41 +00:00Commented Jan 30, 2022 at 6:55 -
\$\begingroup\$ @att Thanks! I'm fine with
Echo
, exceptEcho@#~ArrayPad~-1&~FixedPoint~#&
is longer than current version. \$\endgroup\$Silvia– Silvia2022年01月30日 07:48:55 +00:00Commented Jan 30, 2022 at 7:48 -
1\$\begingroup\$ Using
//.
will almost always be shorter thanFixedPoint
for golfing. \$\endgroup\$att– att2022年01月30日 07:56:26 +00:00Commented Jan 30, 2022 at 7:56 -
\$\begingroup\$ @att Thanks for the tips! I'm not a regular for code golfing. So is it ok to "derive" a language from existing ones just for golfing? Say, a "compact WL" where all the long/full names are replaced with single Unicode symbols. \$\endgroup\$Silvia– Silvia2022年01月30日 08:00:50 +00:00Commented Jan 30, 2022 at 8:00
-
1\$\begingroup\$ You can also check out this thread for a collection of golfing tips for Mathematica/WL in particular. \$\endgroup\$att– att2022年01月30日 08:36:34 +00:00Commented Jan 30, 2022 at 8:36
BQN, 20 bytesSBCS
×ばつ∘≠ ×ばつ∘≢⊸(-∘⊣↓↓)⍟⊒ ̃
...⍟⊒ ̃
Iterate 0 to length-1 times, collecting the results:
×ばつ∘≢⊸(-∘⊣↓↓)
Drop the outer face.
×ばつ∘≠ ̈⊸/
Remove empty arrays from the output.
Calculating the length of the output to avoid the last step is a byte longer:
×ばつ∘≢⊸(-∘⊣↓↓)⍟(↕·⌈2÷ ̃≠)
Brachylog, 12 bytes
ẉ&{ċbk↰m|N}↰
Outputs one array per line. Includes an empty array for even-length inputs. Try it online!
Explanation
ẉ&{ċbk↰m|N}↰
ẉ Write the input to stdout with a trailing newline
& And, with the input
{ } Apply this predicate to remove one hypercube layer:
ċ The input must be a list
b Remove the first element
k Remove the last element
↰m Map the current predicate recursively to each remaining element
| Or, if that definition fails, try this one:
N The input is a nonnegative integer (return it unchanged)
↰ After removing a layer, call the outer predicate recursively
The recursion continues until the predicate fails
05AB1E, 20 bytes
Δ=DdΔ€`}\N>"ε¦ ×ばつ¦.V
Will always output a single trailing empty list.
Try it online or verify all test cases.
Explanation:
Δ # Continue looping until the result no longer changes:
= # Print the current multi-dimensional matrix with trailing newline
# (without popping)
# (which will use the implicit input in the first iteration)
DdΔ€`}\N> # Determine the depth of this multi-dimensional matrix:
D # Duplicate it
d # Transform each inner integer to a 1 (with a >=0 check)
Δ # Inner loop until the result no longer changes:
€` # Flatten one level down
}\ # After the inner loop: discard the result
N> # Push the last 1-based index instead (0-based index + 1)
"ε¦ ×ばつ # Repeat "ε¦ ̈" that many times as string
¦ # Remove the leading "ε"
.V # Evaluate and execute it as 05AB1E code:
ε # Map over each inner list:
¦ # Remove the first item
̈ # As well as the last item
It'll always output an additional trailing empty list, because Δ
will loop an additional time to determine if the list isn't changing anymore.
The determination of the depth of the multi-dimensional matrix (DdΔ€`}\N>
) is taken from this answer of mine.
Minor note: "ε¦ ×ばつ
cannot be ...ε¦ ×ばつ
or ...ε ×ばつ
, because the ¦ ̈
/ ̈¦
will be interpret as dictionary words "ink"
/"keys"
respectively: try it online.
Retina 0.8.2, 188 bytes
;{:G`
(\[)(\d+|\[((\[)|(?<-4>])|[^][])+(?(4)^)]),(?=(\d+|\[((\[)|(?<-7>])|[^][])+(?(7)^)]),)|(?<=,(\d+|(?(10)^)\[((])|(?<-10>\[)|[^][])+])),(\d+|\[((\[)|(?<-13>])|[^][])+(?(13)^)])(?=])
1ドル
Try it online! Link includes test cases. Explanation:
;{:G`
Output the contents of the buffer before every iteration, but don't double-output the last iteration.
(\[)(\d+|\[((\[)|(?<-4>])|[^][])+(?(4)^)]),(?=(\d+|\[((\[)|(?<-7>])|[^][])+(?(7)^)]),)|
Match the first element in the array or one of its inner subarrays, as long as that array has at least three elements. The leading [
is captured as that's slightly golfier than a lookbehind. The first element is matched normally and the second element is matched with a lookahead as if it's a subarray then we'll want to match the first element in the subarray. .NET balancing groups are used to ensure that whole subarrays are matched. Alternatively:
(?<=,(\d+|(?(10)^)\[((])|(?<-10>\[)|[^][])+])),(\d+|\[((\[)|(?<-13>])|[^][])+(?(13)^)])(?=])
Match the last element in the array or one of its inner subarrays, as long as that array has at least three elements. The penultimate element is matched with a lookbehind which is subtly different as it matches from right to left so ]
s increase the balancing group depth and [
s decrease it. Here a lookahead is slightly golfier than capturing the trailing ]
.
1ドル
Keep the leading [
before the first element if that's what we matched.
Charcoal, 66 bytes
W¬=θυ«≔∨υθθ⟦⭆1θ⟧≔IIθυ≔⟦υ⟧ηFη«F‹2Lκ«≔⟦⟧ζWκ⊞ζ⊟κF⮌✂ζ1±1⊞κμ»Fκ¿+⟦⟧λ⊞ηλ
Try it online! Link is to verbose version of code. Explanation:
W¬=θυ«
Repeat until the array stops changing.
≔∨υθθ
Except on the first run, use the new array in place of the original.
⟦⭆1θ⟧
Pretty-print the array on its own line.
≔IIθυ
Deep clone the array. (Unfortunately adding 0
or multiplying by 1
doesn't work because those operators only vectorise once.)
≔⟦υ⟧ηFη«
Start processing the array and all of its subarrays.
F‹2Lκ«
If this array has at least three elements, then...
≔⟦⟧ζWκ⊞ζ⊟κ
... remove the elements of the array into a temporary array, and...
F⮌✂ζ1±1⊞κμ
... push all of the elements of the temporary array except the first and last back to the array.
»Fκ¿+⟦⟧λ⊞ηλ
Push all subarrays of the current array to the list of arrays to be processed.
Python 2, 68 bytes
def f(L):L>f<map(f,L)<L.pop(0)+L.pop()
L=input()
while[f(L)]:print L
A program taking input from STDIN and output to STDOUT. Doesn't output the initial array. Terminates with error.