16
\$\begingroup\$

1d version

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]]]]
\$\endgroup\$
7
  • \$\begingroup\$ Can we assume anything about the inputs, such as positive, non-zero, etc.? (re. suggested edit) \$\endgroup\$
    – hyper-neutrino
    Jan 28 at 4:38
  • \$\begingroup\$ Can we output a final empty list if the input has even length? \$\endgroup\$
    – att
    Jan 28 at 6:47
  • \$\begingroup\$ @att Sure, that's fine \$\endgroup\$
    – emanresu A
    Jan 28 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\$ Jan 28 at 8:57
  • \$\begingroup\$ Can we skip outputting the initial array, and only output subsequent steps? \$\endgroup\$ Jan 28 at 18:04

15 Answers 15

9
\$\begingroup\$

Wolfram Language (Mathematica), 47 44 bytes

#//.l_:>(Print@l;#0/@#[[2;;-2]]~Check~#&@l)&

Try it online!

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&

Try it online!

Returns the lists in reverse order. I don't expect this is allowed, but it's an interesting variation.

\$\endgroup\$
2
  • \$\begingroup\$ Why not print to stdout instead of returning the result, so that you can Echo each step? \$\endgroup\$
    – alephalpha
    Jan 28 at 8:23
  • \$\begingroup\$ @alephalpha Besides my not liking to use Echo, no particular reason. \$\endgroup\$
    – att
    Jan 28 at 21:07
7
\$\begingroup\$

APL(Dyalog Unicode), 17 bytes SBCS

{h↓⍵↓⍨-h←×⍴⎕←⍵}⍣≡

Try it on APLgolf!

A dfn submission which prints each iteration. Input is a multidimensional array.

-14 from ovs.

\$\endgroup\$
2
  • \$\begingroup\$ If you pass a vector left argument to , you can drop along multiple axes: {h↓⍵↓⍨-h←×⍴⎕←⍵}⍣≡ \$\endgroup\$
    – ovs
    Jan 28 at 9:13
  • \$\begingroup\$ I suppose axis specification never really is golfy \$\endgroup\$
    – Razetime
    Jan 28 at 9:18
6
\$\begingroup\$

Ruby, 57 bytes

f=->x{x*0==0?x:x[1..-2].map(&f)}
g=->h{p h
h[2]&&g[f[h]]}

Try it online!

\$\endgroup\$
6
\$\begingroup\$

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
\$\endgroup\$
5
\$\begingroup\$

APL (Dyalog Unicode), 18 bytes

Anonymous tacit prefix function.

{⍵(⊢↓↓⍨∘-)×⍴⎕←⍵}⍣≡

Try it online!

{}⍣≡  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

\$\endgroup\$
5
\$\begingroup\$

Python + NumPy, 74 bytes --> 64 bytes (@DLosc)

def x(a): 
 if len(a):print(a);x(a[(slice(1,-1),)*len(a.shape)])

Try it online!

Thank's @DLosc for removing the return. Nice spot.

\$\endgroup\$
3
  • 2
    \$\begingroup\$ Welcome to Code Golf! Nice answer. \$\endgroup\$ Jan 28 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
    Jan 28 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
    Jan 29 at 6:07
4
\$\begingroup\$

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]]]])))

\$\endgroup\$
4
\$\begingroup\$

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

Try it online!

-7 bytes + fixed thanks to DLosc

\$\endgroup\$
1
4
\$\begingroup\$

Wolfram Language, 34 32 bytes

#~ArrayPad~-1&~FixedPointList~#&

Try it online!

  • ArrayPad[array,-1] negatively pad 1 element on every side regardless the dimension of array.

  • FixedPointList[f, x] applies function f on x repeatedly, until a fixed-point of f 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]&
\$\endgroup\$
6
  • 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 with Echo instead of Print). \$\endgroup\$
    – att
    Jan 30 at 6:55
  • \$\begingroup\$ @att Thanks! I'm fine with Echo, except Echo@#~ArrayPad~-1&~FixedPoint~#& is longer than current version. \$\endgroup\$
    – Silvia
    Jan 30 at 7:48
  • 1
    \$\begingroup\$ Using //. will almost always be shorter than FixedPoint for golfing. \$\endgroup\$
    – att
    Jan 30 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
    Jan 30 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
    Jan 30 at 8:36
3
\$\begingroup\$

BQN, 20 bytesSBCS

×∘≠¨⊸/×∘≢⊸(-∘⊣↓↓)⍟⊒˜

Run online!

...⍟⊒˜ 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÷˜≠)

Run online!

\$\endgroup\$
3
\$\begingroup\$

Brachylog, 12 bytes

ẉ&{ċbk↰ᵐ|ℕ}↰

Outputs one array per line. Includes an empty array for even-length inputs. Try it online!

Explanation

ẉ&{ċbk↰ᵐ|ℕ}↰
ẉ             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
      ↰ᵐ        Map the current predicate recursively to each remaining element
        |       Or, if that definition fails, try this one:
         ℕ      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
\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

Charcoal, 66 bytes

W¬⁼θυ«≔∨υθθ⟦⭆¹θ⟧≔IIθυ≔⟦υ⟧ηFη«F‹²Lκ«≔⟦⟧ζWκ⊞ζ⊟κF⮌✂ζ¹±¹⊞κμ»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.

⟦⭆¹θ⟧

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‹²Lκ«

If this array has at least three elements, then...

≔⟦⟧ζWκ⊞ζ⊟κ

... remove the elements of the array into a temporary array, and...

F⮌✂ζ¹±¹⊞κμ

... 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.

\$\endgroup\$
1
\$\begingroup\$

Python 2, 68 bytes

def f(L):L>f<map(f,L)<L.pop(0)+L.pop()
L=input()
while[f(L)]:print L

Try it online!

A program taking input from STDIN and output to STDOUT. Doesn't output the initial array. Terminates with error.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.