# Create a checkerboard matrix

Take a positive integer n as input, and output a n-by-n checkerboard matrix consisting of 1 and 0.

The top left digit should always be 1.

Test cases:

n = 1
1

n = 2
1 0
0 1

n = 3
1 0 1
0 1 0
1 0 1

n = 4
1 0 1 0
0 1 0 1
1 0 1 0
0 1 0 1


Input and output formats are optional. Outputting the matrix as a list of lists is accepted.

• Is a list of strings OK? – xnor Jun 15 '17 at 17:52
• Yes, that's OK. – Stewie Griffin Jun 15 '17 at 17:53
• – beaker Jun 15 '17 at 18:51
• Your examples show spaces between numbers on the same row, is that required, so as to look more like a square? – BradC Jun 15 '17 at 19:09
• @BradC it's not required. The first approach here is valid. – Stewie Griffin Jun 15 '17 at 20:19

# braingasm, 21 bytes

;$[$[>+o:<]p[>+<]10.]


Explanation:

;                       Read a number from stdin.
$[ ] That many times...$[     ]               That many times...
>+o:<                  Increase the next cell and print its value modulo 2.
p[   ]       If the input was even...
>+<          Increase the next cell once more.
10.    Print a newline.


## J, 41 bytes

I know there's a J answer at 9, but I'm pleased to get anything working, even if it's not a tacit programming best possible..

ch=:monad define
2|(2$y)$(1+i.y),(i.y)
)


It takes a number y and generates two lists of numbers 1,2,3,..y and 0,1,2,3,..y-1 to make the offset first and second row, appends them into one long list, reshapes that into a y,y matrix (wrapping around when it runs out), and then does modulo 2 on all the elements to make them a 1 or 0.

   ch 1
1

ch 2
1 0
0 1

ch 3
1 0 1
0 1 0
1 0 1

ch 4
1 0 1 0
0 1 0 1
1 0 1 0
0 1 0 1

ch 5
1 0 1 0 1
0 1 0 1 0
1 0 1 0 1
0 1 0 1 0
1 0 1 0 1


# Python, 73 63 62 66 bytes

Saved 4 bytes thanks to officialaimm

r=range(input());print[''.join([(x+y+1)%2for x in r])for y in r]

• Declaring R=range(n) and using R saves 3 bytes. 1 unwanted space between ) and for can be removed as well. – officialaimm Jun 16 '17 at 7:40
• @officialaimm Sweet, thanks for the tip! I'm new a golfing. I'm thankful for any tips :) – Daffy Jun 16 '17 at 19:50
• Just noticed your code is just a snippet, not a function or full program which is not a healthy practice. You can use this instead, only 8 bytes more! – officialaimm Jun 17 '17 at 1:37
• @officialaimm Noted. Still better than my original submission. Thanks a bunch. :) – Daffy Jun 17 '17 at 1:43

# q/kdb+, 37 16 bytes

Solution:

{x#x#'(1 0;0 1)}


Example:

q){x#x#'(1 0;0 1)}1
1
q){x#x#'(1 0;0 1)}2
1 0
0 1
q){x#x#'(1 0;0 1)}3
1 0 1
0 1 0
1 0 1
q){x#x#'(1 0;0 1)}4
1 0 1 0
0 1 0 1
1 0 1 0
0 1 0 1


Explanation:

2nd version is much simpler, and thus shorter and faster (4x). Create a 2-item list containing 01... and 10... to the length of the input, then take 'x' number of items from this new list.

{              } / lambda function
(1 0;0 1)  / 2-item list of (0;1) and (1;0)
x#'           / take 'x' items from each list, if x=3 then (1 0 1;0 1 0)
x#              / take 'x' items from *this* list


Notes:

I've re-written this twice during this edit, went from 37->25->24->16 bytes. Now it's a little more competitive.

Edits:

• -21 bytes with complete re-write...

h=1:0:h
l=h:(0:h):l
s x=take x$map(take x)l  Try it online! This creates the infinite checkerboard matrix and saves it to l. Then our function s chops off a square. # Mathematica, 28 bytes Table[Mod[i+j+1,2],{i,#},{j,#}]&  ## Julia, 38 bytes n->map(x->x%2,(1:n).+transpose(0:n-1))  The usual approach # Actually, 15 bytes r;╗⌠╜+u⌠2@%⌡M⌡M  Try it online! # Python, 43 bytes lambda n:[('10'*n)[i:i+n]for i in range(n)]  Try it online! Anonymous function that outputs a list like ['1010', '0101', '1010', '0101']. # Python 2, 727061 54 bytes -7 bytes thanks to Leaky Nun. -1 byte thanks to Wheat Wizard. lambda n:[[i-~j&1for i in range(n)]for j in range(n)]  Try it online! C#, 145 Bytes int u=1;int n=int.Parse(b.Text);for(int i=0;i<n;i++){if(i%2==0){u=1;}else{u=0;}for(int j=0;j<n;j++){t.Text+=u.ToString();u=1-u;}t.Text+="\r\n";}[enter link description here]  Try it online! # Bash & coreutils, 82 bytes x=$1;yes 10|tr -d \\n|dd bs=1 count=$[x*x*2]|fold -w$[x*2-1]|grep -om$x "^.\{$x\}"


Takes one command-line integer as input. Apparently the spaces between are optional.

# Common Lisp, SBCL, 81 bytes

version 1:

(#1=dotimes(i(set'a(read)))(#1#(j a)(format t"~:[1~;0~] "(oddp(+ j i))))(terpri))


version 2:

(lambda(n)(#1=dotimes(i n)(#1#(j n)(format t"~:[1~;0~] "(oddp(+ j i))))(terpri)))


Two loops, and inside I print either 1 and space or zero and space based on parity of (+ j i). (terpri) for newline.

It's long, I know:/

# C# (.NET Core), 88 bytes

n=>{var r=new int[n,n];for(int i=0,j;i<n;i++)for(j=0;j<n;j++)r[i,j]=(i+j+1)%2;return r;}


Try it online!

I can't believe this is the shortest way to initialize a 2D array of integers, there must be another way...

time {set a "";time {set a $a[expr [incr i]%2]}$n;puts $a;if \!($n%2) incr\ i} $n  # demo Still a looser, but I think i can be golfed down a little bit further. # Perl 5, 49 bytes $_=10x(($n=<>)/2).1x($n%2);say&&y/01/10/while\$n--


Try it online!

# C (gcc), 60 59 bytes

i;f(n){for(i=n*n;i--;)printf(i%n?"%d":"%d\n",i%n+i/n+1&1);}


Try it online!