14
\$\begingroup\$

Given an integer N from 1-9, you must print an NxN grid of NxN boxes that print alternating 1s and Ns, with each box having an alternating starting integer.

Examples

Input: 1
Output:

1


Input: 2
Output:

12  21
21  12

21  12
12  21


Input: 3
Output:

131  313  131
313  131  313
131  313  131

313  131  313
131  313  131
313  131  313

131  313  131
313  131  313
131  313  131


Input: 4
Output:

1414  4141  1414  4141
4141  1414  4141  1414
1414  4141  1414  4141
4141  1414  4141  1414

4141  1414  4141  1414
1414  4141  1414  4141
4141  1414  4141  1414
1414  4141  1414  4141

1414  4141  1414  4141
4141  1414  4141  1414
1414  4141  1414  4141
4141  1414  4141  1414

4141  1414  4141  1414
1414  4141  1414  4141
4141  1414  4141  1414
1414  4141  1414  4141

An array (of NxN arrays) or text in this format is acceptable.

\$\endgroup\$
8
  • \$\begingroup\$ Can we output an \$N^2 \times N^2\$ matrix? Must there be separators between the blocks? \$\endgroup\$ Commented Jan 27, 2022 at 21:35
  • \$\begingroup\$ @cairdcoinheringaahing > An array of numbers or text in this format is acceptable. \$\endgroup\$
    – Fmbalbuena
    Commented Jan 27, 2022 at 21:37
  • 3
    \$\begingroup\$ @Fmbalbuena That doesn't answer the question \$\endgroup\$
    – emanresu A
    Commented Jan 27, 2022 at 21:40
  • 1
    \$\begingroup\$ @cairdcoinheringaahing Any data type that expresses separated boxes of numbers in this format acceptable \$\endgroup\$
    – drmosley
    Commented Jan 27, 2022 at 21:41
  • 1
    \$\begingroup\$ @drmosley So is [[1,2,2,1],[2,1,1,2],[1,2,2,1],[,2,1,1,2]] an acceptable output for \$N = 2\$? \$\endgroup\$ Commented Jan 27, 2022 at 21:43

14 Answers 14

10
\$\begingroup\$

Wolfram Language (Mathematica), 30 29 bytes

#^Array[Plus,#+0{,,,}]~Mod~2&

Try it online!

Returns a \$(N\times N)\times(N\times N)\$ array.

  Array[    ,#+0{,,,}]          N x N x N x N array, where values are
#^                                N^
        Plus                        (sum of indices
                      ~Mod~2         mod 2)
\$\endgroup\$
1
  • 1
    \$\begingroup\$ The power trick is a nice touch. \$\endgroup\$
    – Jonah
    Commented Jan 27, 2022 at 23:33
5
\$\begingroup\$

J, 33 23 19 bytes

]^2|i.+/2|+/^:2~@i.

Try it online!

Note: In TIO I added a formatting function to box each element, which is now a list of digits, to make the correctness more obvious. If you remove it, the results will still be correct but because of the way J prints by default it won't be immediately clear.

-10 thanks to power trick stolen from att's Wolfram answer

  • 2|...+/^:2~@i. Creates a 0-1 checkerboard in the n x n x n dimensions of the input
  • i.+ Creates n versions of that, adding 0, 1, ... n elementwise.
  • ]^ Raises input to those 0-1 matrices, creating matrices of 1 and n. This is really the end of the golf.
\$\endgroup\$
3
\$\begingroup\$

Vyxal, 16 bytes

₀Ẏ:v꘍:£vv‡¥꘍?‹*›

Try it Online!

A huge mess.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ This was one of my first thoughts too but I don't think it qualifies. Try it with 4, for example. \$\endgroup\$
    – Jonah
    Commented Jan 27, 2022 at 21:59
  • \$\begingroup\$ @Jonah Oops. (filler) \$\endgroup\$
    – emanresu A
    Commented Jan 27, 2022 at 22:00
  • \$\begingroup\$ @Jonah Fixed. (filler) \$\endgroup\$
    – emanresu A
    Commented Jan 27, 2022 at 22:03
  • \$\begingroup\$ According to this comment, you may still need some additional separation rather than the single block? Though tbh I am not entirely sure... \$\endgroup\$
    – Jonah
    Commented Jan 27, 2022 at 22:08
  • \$\begingroup\$ Looks correct now. \$\endgroup\$
    – Jonah
    Commented Jan 28, 2022 at 3:00
3
\$\begingroup\$

GeoGebra, 92 bytes

n=2
a=Zip(Zip(Mod(b-a,2),b,1…n),a,1…n)
b=(n-1)a+1
l=Zip(Zip(b+Mod(d,2)(n+1-2b),d,c),c,a)

Try It On GeoGebra!

n is the input, and l is the output.

The output is in the form of an array of depth 4:

  • Inside the outermost list (layer 1) is the rows of the supersquare (layer 2)
  • Inside the second layer is each individual matrix in the supersquare (layer 3)
  • Insde the third layer is each row of a particular matrix entry in the supersquare (layer 4)

The default input is n=2, but you can change this to whatever input you want.

Output for n=2 (layers labelled):

{{{{1, 2}, {2, 1}}, {{2, 1}, {1, 2}}}, {{{2, 1}, {1, 2}}, {{1, 2}, {2, 1}}}}
||||__4_|        |                  |                                      |
|||______3_______|                  |                                      |
||_______________2__________________|                                      |
|____________________________________1_____________________________________|
\$\endgroup\$
2
\$\begingroup\$

Pari/GP, 48 bytes

n->matrix(n,,i,j,matrix(n,,k,l,n^((i+j+k+l)%2)))

Try it online!

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

05AB1E, 10 bytes

L4ãOÉmIFIô

The modulo-2 exponent trick is taken from @att's Mathematica answer, so make sure to upvote him as well!

Try it online or verify all test cases.

Explanation:

L           # Push a list in the range [1,(implicit) input]
 4ã         # Create all possible quartets of these values,
            # by using the cartesian power of 4
   O        # Sum each inner list
    É       # Check for each sum whether it's odd
     m      # Take the (implicit) input to the power of these 0s/1s,
            # so all 0s become 1 and all 1s become the input-digit
      IF    # Loop the input amount of times:
        Iô  #  Split the list into an input amount of parts
            # (after which the multi-dimensional matrix is output implicitly)
\$\endgroup\$
2
\$\begingroup\$

C (gcc), 114 111 bytes

k;j;i;f(n){for(i=0;n*n/++i;puts(i%n?"":"\n"))for(j=0;n/++j;putchar(32))for(k=0;n/++k;)printf("%d",i+j+k&1?:n);}

Try it online!

Saved 3 bytes thanks to ceilingcat!!!

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

Charcoal, 24 bytes

NθEθEθ⪫Eθ⭆θ∨¬﹪⁺⁺⁺ιλνπ²θ 

Try it online! Link is to verbose version of code. Explanation:

Nθ                          Input `n` as an integer
   θ                        Input `n`
  E                         Map over implicit range
     θ                      Input `n`
    E                       Map over implicit range
        θ                   Input `n`
       E                    Map over implicit range
          θ                 Input `n`
         ⭆                  Map over implicit range and join
               ⁺⁺⁺ιλνπ       Sum of all indices
             ¬﹪       ²      Is divisible by `2`
           ∨                Logical Or
                      θ     Input `n`
      ⪫                     Join with spaces
                            Implicitly print
\$\endgroup\$
1
\$\begingroup\$

JavaScript (Node.js), 56 bytes

f=(n,w=4,y)=>w?[...Array(n)].map(_=>f(n,w-1,y=!y)):y?n:1

Try it online!

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

Python, 89 bytes

f=lambda n,s=" \n\n",p=0:s.join(j[s>"":]or f(n,s[:-1],j>"1")for j in(n*str(10+n))[p:n+p])

Attempt This Online!

Returns a single string. Most lines have a trailing space.

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

Python 3, 181 bytes

def s(n):
    a=f'1{n}'*n
    b,c=' '.join(([a[:n],a[1:n+1]]*n)[:n]),' '.join(([a[1:n+1],a[:n]]*n)[:n])
    return '\n\n'.join((['\n'.join(([b,c]*n)[:n]),'\n'.join(([c,b]*n)[:n])]*n)[:n])
  1. Creates a single row for a single box
  2. Makes a complete row (N rows) of alternating boxes
  3. Return a string containing N rows of N-rows boxes
\$\endgroup\$
1
\$\begingroup\$

Labra, 69 bytes

()[]([])
()[<><{}>]<[{}<[]>]>
<[]><<>>[](<>[])<<>>
{}(())<<><<>><<>>>

I've been waiting for a month and a half for a question Labra could actually compete in.

Prints as a list: [[[[1, 2], [2, 1]], [[2, 1], etc...

For input = 3 (Which Labra receives as [3])
()[]([])                    [1,0]
()[<><{}>]                  Starting at n=1, n=[1,0][n]: [1,0,1,0,1,...]
          <[{}<[]>]>        Index with [1...input[1]]:   [0,1,0]
<[]><<>>                    Index into first line:       [1,0,1]
        [](<>[])            Put in list and add [0,1,0]: [[1,0,1],[0,1,0]]
                <<>>        Index with [0,1,0]:          [[1,0,1],[0,1,0],[1,0,1]]
{}(())                      Input (already a list) + 1:  [3,1]
      <          >          Index that with
       <>                   previous line:       [[1,0,1],[0,1,0],[1,0,1]]
         <<>>               indexed with itself: [[[0,1,0],[1,0,1],[0,1,0]],[[1,0,1],[0,1,0],...
             <<>>           twice:               [[[[1,0,1],[0,1,0],[1,0,1]],[[0,1,0],[1,0,1],[0,1,0]],...
                            giving:                      [[[[1,3,1],[3,1,3],[1,3,1]],[[3,1,3],[1,3,1],[3,1,3]],...
                            (implicit output)
\$\endgroup\$
1
\$\begingroup\$

PowerShell Core, 111 bytes

param($a)($s=1..($a*$a))|%{$b++
-join($s|%{"1$a"[($_+$b)%2]
.($g={if(!($_%$a)){$args;$b+=1-$a%2}})' '})
.$g ''}

Try it online!

-2 bytes thanks to mazzy!
-9 bytes thanks to mazzy again!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ nice. Try it online! ? \$\endgroup\$
    – mazzy
    Commented Jan 28, 2022 at 4:52
  • 1
    \$\begingroup\$ Try it online! ¯\_(ツ)_/¯ \$\endgroup\$
    – mazzy
    Commented Jan 29, 2022 at 8:37
0
\$\begingroup\$

PHP, 129 bytes

for(;$a++<$n=$argn;){for($b=0;$b++<$n;){for($c=0;$c++<$n;){for($d=0;$d++<$n;)echo($a+$b+$c+$d)%2?$n:1;echo" ";}echo"
";}echo"
";}

I feel like there's probably a way that this could be done better with a recursive function, but I can't wrap my head around it.

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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