Create a checkerboard matrix

26

2

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.

Stewie Griffin

Posted 2017-06-15T17:32:16.590

Reputation: 43 471

Is a list of strings OK? – xnor – 2017-06-15T17:52:45.980

Yes, that's OK. – Stewie Griffin – 2017-06-15T17:53:49.607

1Related. – beaker – 2017-06-15T18:51:31.137

2Your examples show spaces between numbers on the same row, is that required, so as to look more like a square? – BradC – 2017-06-15T19:09:09.797

@BradC it's not required. The first approach here is valid.

– Stewie Griffin – 2017-06-15T20:19:29.803

Can we take the input in unary if our language doesn't have the concept of a number? (e.g. #### for input 4) – Conor O'Brien – 2017-06-15T20:26:27.773

https://oeis.org/A100241 has all of the binary representations of this. Dunno how I'd even begin to use it though lol. – Magic Octopus Urn – 2017-06-16T20:40:27.217

Answers

12

Jelly, 4 bytes

52 seconds!

+€ḶḂ

Try it online!

Leaky Nun

Posted 2017-06-15T17:32:16.590

Reputation: 45 011

7"52 seconds!" like I'm not used to it... – Erik the Outgolfer – 2017-06-15T17:35:43.947

5Do ya'll have, like, a beeper, you wear 24/7 for new PPCG challenges? – Magic Octopus Urn – 2017-06-16T20:43:24.160

@carusocomputing Those who have a faster internet connection are usually the lucky ones that will win. – Erik the Outgolfer – 2017-06-17T14:16:00.927

9

MATL, 5 bytes

:otYT

Try it at MATL online!

Explanation

Consider input 4 as an example.

:    % Implicit input, n. Push range [1 2 ... n]
     %   STACK: [1 2 3 4]
o    % Parity, element-wise
     %   STACK: [1 0 1 0]
t    % Duplicate
     %   STACK: [1 0 1 0], [1 0 1 0]
YT   % Toeplitz matrix with two inputs. Implicit display
     %   STACK: [1 0 1 0;
     %           0 1 0 1;
     %           1 0 1 0;
     5           0 1 0 1]

Luis Mendo

Posted 2017-06-15T17:32:16.590

Reputation: 87 464

7

Japt, 6 bytes

ÆÇ+X v

Test it online! (Uses -Q flag for easier visualisation)

Explanation

 Æ   Ç   +X v
UoX{UoZ{Z+X v}}  // Ungolfed
                 // Implicit: U = input number
UoX{          }  // Create the range [0...U), and map each item X to
    UoZ{     }   //   create the range [0...U), and map each item Z to
        Z+X      //     Z + X
            v    //     is divisible by 2.
                 // Implicit: output result of last expression

An interesting thing to note is that v is not a "divisible by 2" built-in. Instead, it's a "divisible by X" built-in. However, unlike most golfing languages, Japt's functions do not have fixed arity (they can accept any number of right-arguments). When given 0 right-arguments, v assumes you wanted 2, and so acts exactly like it was given 2 instead of nothing.

ETHproductions

Posted 2017-06-15T17:32:16.590

Reputation: 47 880

7

V, 16, 15 bytes

Ài10À­ñÙxñÎÀlD

Try it online!

Hexdump:

00000000: c069 3130 1bc0 adf1 d978 f1ce c06c 44    .i10.....x...lD

James

Posted 2017-06-15T17:32:16.590

Reputation: 54 537

7

Haskell, 50 41 39 38 bytes

Thanks to nimi and xnor for helping to shave off a total of 9 10 bytes

f n=r[r"10",r"01"]where r=take n.cycle

Alternately, for one byte more:

(!)=(.cycle).take
f n=n![n!"10",n!"01"]

or:

r=flip take.cycle
f n=r[r"10"n,r"01"n]n

Probably suboptimal, but a clean, straightforward approach.

Julian Wolf

Posted 2017-06-15T17:32:16.590

Reputation: 1 139

concat.repeat is cycle: n!l=take n$cycle l. If you go pointfree it saves one more byte: (!)=(.cycle).take. – nimi – 2017-06-15T18:26:41.203

Lovely! I knew there was a builtin for that, but couldn't remember the name for the life of me – Julian Wolf – 2017-06-15T18:31:10.077

I was going to suggest f n|r<-take n.cycle=r[r"10",r"01"] or similar. but Haskell seems to infer the wrong type for r? It works with explicit typing f n|r<-take n.cycle::[a]->[a]=r[r"10",r"01"]. – xnor – 2017-06-15T18:59:54.350

That is really weird, especially given that f n=r[r"10",r"01"] where r=take n.cycle seems to work fine (but gives the same byte count as the current solution) – Julian Wolf – 2017-06-15T19:07:31.977

1

@JulianWolf Haskell seems to have trouble inferring polymorphic types

– xnor – 2017-06-15T19:43:32.037

@JulianWolf: you can omit the space before the where, so your function from the comment is 38 bytes – nimi – 2017-06-15T20:57:24.807

@JulianWolf This is because of the monomorphism restriction. I'm not sure if it's legal, but this is disabled by default in GHCi, so you might be able specify "Haskell - GHCi" to get the bytes.

– zbw – 2017-06-16T01:42:10.740

1@zbw I thought this was the case but using NoMonomorphismRestriction didn't help. Nor did Rank2Types or RankNTypes. Do you know what's going on there? – xnor – 2017-06-16T04:59:03.807

Another solution at 39 bytes using only list comprehension. – ბიმო – 2017-07-22T01:50:24.600

5

APL (Dyalog), 8 bytes

~2|⍳∘.+⍳

Try it online!

Explanation

Let's call the argument n.

⍳∘.+⍳

This creates a matrix

1+1 1+2 1+2 .. 1+n
2+1 2+2 2+3 .. 2+n
...
n+1 n+2 n+3 .. n+n

Then 2| takes modulo 2 of the matrix (it vectorises) after which ~ takes the NOT of the result.

user41805

Posted 2017-06-15T17:32:16.590

Reputation: 16 320

4

Mathematica, 25 bytes

1-Plus~Array~{#,#}~Mod~2&

Martin Ender

Posted 2017-06-15T17:32:16.590

Reputation: 184 808

4

JavaScript ES6, 55 54 51 46 bytes

Saved 1 byte thanks to @Neil

Saved 2 bytes thanks to @Arnauld

n=>[...Array(n)].map((_,i,a)=>a.map(_=>++i&1))

Try it online!

This outputs as an array of arrays. JavaScript ranges are pretty unweildy but I use [...Array(n)] which generates an array of size n

Downgoat

Posted 2017-06-15T17:32:16.590

Reputation: 27 116

It's still a byte shorter to use the index parameters: n=>[...Array(n)].map((_,i,a)=>a.map((_,j)=>(i+j+1)%2)) – Neil – 2017-06-15T18:41:59.797

@Neil huh, I never thought to use the third parameter in map, thanks! – Downgoat – 2017-06-15T18:47:47.900

@Arnauld thanks! that inspired me to save 5 more bytes! – Downgoat – 2017-06-15T22:55:35.570

4

Retina, 33 30 bytes

.+
$*
1
$_¶
11
10
T`10`01`¶.+¶

Try it online! Explanation: The first stage converts the input to unary using 1s (conveniently!) while the second stage turns the value into a square. The third stage inverts alternate bits on each row while the last stage inverts bits on alternate rows. Edit: Saved 3 bytes thanks to @MartinEnder.

Neil

Posted 2017-06-15T17:32:16.590

Reputation: 95 035

$\1$'is just$_`. – Martin Ender – 2017-06-16T05:27:56.743

@MartinEnder Ah, I'm unfamiliar with $_, thanks! – Neil – 2017-06-16T07:45:52.543

3

MATL, 7 bytes

:t!+2\~

Try it online!

Explanation:

         % Implicit input (n)
:        % Range from 1-n, [1,2,3]
 t       % Duplicate, [1,2,3], [1,2,3]
  !      % Transpose, [1,2,3], [1;2;3]
   +     % Add        [2,3,4; 3,4,5; 4,5,6]
    2    % Push 2     [2,3,4; 3,4,5; 4,5,6], 2
     \   % Modulus    [0,1,0; 1,0,1; 0,1,0]
      ~  % Negate     [1,0,1; 0,1,0; 1,0,1]

Note: I started solving this in MATL after I posted the challenge.

Stewie Griffin

Posted 2017-06-15T17:32:16.590

Reputation: 43 471

Equivalent, and shorter: :&+o~ – Luis Mendo – 2017-06-15T21:25:07.933

1Still learning :-) I'll update tomorrow. I liked your other approach too :-) – Stewie Griffin – 2017-06-15T21:44:16.810

1This is what I came up with, too. And hey, you only use the pure MATL instruction set, not those pesky Y-modified instructions @LuisMendo uses. – Sanchises – 2017-06-16T21:04:14.840

@Sanchises Pesky, huh? :-P

– Luis Mendo – 2017-06-16T21:09:39.303

3

Java (OpenJDK 8), 80 77 bytes

-3 bytes thanks to Kevin Cruijssen

j->{String s="1";for(int i=1;i<j*j;s+=i++/j+i%j&1)s+=1>i%j?"\n":"";return s;}

Try it online!

Oh look, a semi-reasonable length java answer, with lots of fun operators.

lambda which takes an int and returns a String. Works by using the row number and column number using / and % to determine which value it should be, mod 2;

Ungolfed:

j->{
    String s="1";
    for(int i=1; i<j*j; s+= i++/j + i%j&1 )
        s+= 1>i%j ? "\n" : "";
    return s;
}

PunPun1000

Posted 2017-06-15T17:32:16.590

Reputation: 973

You can remove the space to save a byte. The challenge states the output format is flexible. Oh, and you can save two more bytes by changing (i++/j+i%j)%2 to i++/j+i%j&1 so you won't need those parenthesis. Which make the total 1 byte shorter than my nested for-loop solution (n->{String r="";for(int i=0,j;i++<n;r+="\n")for(j=0;j<n;r+=j+++i&1);return r;}), so +1 from me. :) – Kevin Cruijssen – 2017-06-16T07:26:16.360

@KevinCruijssen Yeah I was still waiting on a response on the space. I didn't think about & having higher precedence than % and &1 == %2 – PunPun1000 – 2017-06-16T12:02:15.967

3

Brachylog, 15 bytes

^₂⟦₁%₂ᵐ;?ḍ₎pᵐ.\

Try it online!

Explanation

Example Input: 4

^₂               Square:                            16
  ⟦₁             1-indexed Range:                   [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
    %₂ᵐ          Map Mod 2:                         [1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0]
       ;?ḍ₎      Input-Chotomize:                   [[1,0,1,0],[0,1,0,1],[1,0,1,0],[0,1,0,1]]
           pᵐ.   Map permute such that..
             .\  ..the output is its own transpose: [[1,0,1,0],[0,1,0,1],[1,0,1,0],[0,1,0,1]]

Fatalize

Posted 2017-06-15T17:32:16.590

Reputation: 32 976

3

Clojure, 36 bytes

#(take %(partition % 1(cycle[1 0])))

Yay, right tool for the job.

NikoNyrh

Posted 2017-06-15T17:32:16.590

Reputation: 2 361

3

05AB1E, 9 7 bytes

-2 bytes thanks to Emigna

LDÈD_‚è

Try it online!

Explanation

LDÈD_‚sè» Argument n
LD        Push list [1 .. n], duplicate
  ÈD      Map is_uneven, duplicate
    _     Negate boolean (0 -> 1, 1 -> 0)
     ‚    List of top two elements of stack
      è   For each i in [1 .. n], get element at i in above created list
          In 05AB1E the element at index 2 in [0, 1] is 0 again

kalsowerus

Posted 2017-06-15T17:32:16.590

Reputation: 1 894

You can cut the » as list-of-lists output is okay and you can also remove s. – Emigna – 2017-06-16T07:27:45.697

@Emigna Yep, thanks! – kalsowerus – 2017-06-16T10:43:06.373

Explanation is a bit irrelevant. – Erik the Outgolfer – 2017-06-17T14:14:31.243

2

Charcoal, 8 bytes

UON10¶01

Try it online! Explanation: This roughly translates to the following verbose code (unfortunately the deverbosifier is currently appending an unnecessary separator):

Oblong(InputNumber(), "10\n01");

Neil

Posted 2017-06-15T17:32:16.590

Reputation: 95 035

2

C, 69 67 63 bytes

Thanks to @Kevin Cruijssen for saving two bytes and @ceilingcat for saving four bytes!

i,j;f(n){for(i=n;i--;puts(""))for(j=n;j;)printf("%d",j--+i&1);}

Try it online!

Steadybox

Posted 2017-06-15T17:32:16.590

Reputation: 15 798

You can remove the space in printf("%d ", since that's another valid method of output. – Conor O'Brien – 2017-06-15T21:09:43.073

@ConorO'Brien Yeah, thanks. – Steadybox – 2017-06-15T21:15:48.730

You can save two bytes by changing (j+++i)%2 to j+++i&1 to remove those parenthesis. – Kevin Cruijssen – 2017-06-16T07:31:33.950

@ceilingcat Thanks! – Steadybox – 2017-09-08T00:02:53.767

2

Pyth, 9 bytes

VQm%+hdN2

Try this!

another 9 byte solution:

mm%+hdk2Q

Try it!

KarlKastor

Posted 2017-06-15T17:32:16.590

Reputation: 2 352

2

J, 9 bytes

<0&=$&1 0

Try it online!

miles

Posted 2017-06-15T17:32:16.590

Reputation: 15 654

2

Octave, 24 bytes

@(n)~mod((a=(1:n))+a',2)

Try it online!


Or the same length:

@(n)mod(toeplitz(1:n),2)

Try it online!

alephalpha

Posted 2017-06-15T17:32:16.590

Reputation: 23 988

2

Mathematica, 23 bytes

ToeplitzMatrix@#~Mod~2&

alephalpha

Posted 2017-06-15T17:32:16.590

Reputation: 23 988

2

R, 38 37 bytes

n=scan();(matrix(1:n,n,n,T)+1:n-1)%%2

Try it online!

-1 byte thanks to Giuseppe

Takes advantage of R's recycling rules, firstly when creating the matrix, and secondly when adding 0:(n-1) to that matrix.

user2390246

Posted 2017-06-15T17:32:16.590

Reputation: 1 391

You can drop a byte by getting rid of the t and instead constructing the matrix with byrow=T, i.e., (matrix(1:n,n,n,T)+1:n-1)%%2 – Giuseppe – 2017-06-16T14:12:00.200

1outer(1:n,1:n-1,"+")%%2 is quite a few bytes shorter :) – JAD – 2017-06-23T12:01:15.257

2

Swi-Prolog, 142 bytes.

t(0,1).
t(1,0).
r([],_).
r([H|T],H):-t(H,I),r(T,I).
f([],_,_).
f([H|T],N,B):-length(H,N),r(H,B),t(B,D),f(T,N,D).
c(N,C):-length(C,N),f(C,N,1).

Try online - http://swish.swi-prolog.org/p/BuabBPrw.pl

It outputs a nested list, so the rules say:

  • t() is a toggle, it makes the 0 -> 1 and 1 -> 0.
  • r() succeeds for an individual row, which is a recursive check down a row that it is alternate ones and zeros only.
  • f() recursively checks all the rows, that they are the right length, that they are valid rows with r() and that each row starts with a differing 0/1.
  • c(N,C) says that C is a valid checkerboard of size N if the number of rows (nested lists) is N, and the helper f succeeds.

Test Cases: enter image description here

TessellatingHeckler

Posted 2017-06-15T17:32:16.590

Reputation: 2 412

1

PHP, 56 bytes

Output as string

for(;$i<$argn**2;)echo++$i%2^$n&1,$i%$argn?"":"
".!++$n;

Try it online!

PHP, 66 bytes

Output as 2 D array

for(;$i<$argn**2;$i%$argn?:++$n)$r[+$n][]=++$i%2^$n&1;print_r($r);

Try it online!

Jörg Hülsermann

Posted 2017-06-15T17:32:16.590

Reputation: 13 026

1

QBIC, 19 bytes

[:|?[b|?(a+c+1)%2';

Explanation

[:|         FOR a = 1 to b (b is read from cmd line)
?           PRINT - linsert a linebreak in the output
[b|         FOR c = 1 to b
?(a+c+1)%2  PRINT a=c=1 modulo 2 (giving us the 1's and 0's
';            PRINT is followed b a literal semi-colon, suppressing newlines and 
              tabs. Printing numbers in QBasic adds one space automatically.

steenbergh

Posted 2017-06-15T17:32:16.590

Reputation: 7 772

1

Brachylog, 19 bytes

⟦₁Rg;Rz{z{++₁%₂}ᵐ}ᵐ

Try it online!

Leaky Nun

Posted 2017-06-15T17:32:16.590

Reputation: 45 011

1

CJam, 17 bytes

{_[__AAb*<_:!]*<}

Try it online!

Returns a list (TIO link has formatted output).

Erik the Outgolfer

Posted 2017-06-15T17:32:16.590

Reputation: 38 134

out-golfed – Esolanging Fruit – 2017-06-15T21:10:04.707

@Challenger5 Sorry you can't outgolf with deleted answer. – Erik the Outgolfer – 2017-06-16T08:19:04.900

1

Bash + rs, 42

eval echo \$[~{1..$1}+{1..$1}\&1]|rs $1 $1

Try it online.

Digital Trauma

Posted 2017-06-15T17:32:16.590

Reputation: 64 644

1

Cheddar, 38 bytes

n->(|>n).map(i->(|>n).map(j->i+j+1&1))

Try it online!

Leaky Nun

Posted 2017-06-15T17:32:16.590

Reputation: 45 011

1

///, 87 bytes + input

/V/\\\///D/VV//*/k#D#k/k#D&k/k&DVk/k\D/SD/#/r
DSkk/10DSk/1D&/V#rV#0r;VV0;VVV1;V\D/r/S/&[unary input in asterisks]

Try it online! (input for 4)

Unary input in 1s, 95 bytes + input

/V/\\\///D/VV//&1/k#&D&|D/#k/k#D&k/k&DVk/k\D/SD/#/r
DSkk/10DSk/1D&/V#rV#0r;VV0;VVV1;V\D/r/S/&&[unary input in ones]|

Try it online! (input for 8)

How does this work?

  • V and D are to golf \/ and // respectively.

  • /*/k#/ and /&1/k#&//&|// separate the input into the equivalent of 'k#'*len(input())

  • /#k//k#//&k/k&//\/k/k\// move all the ks to the /r/S/ block

  • Ss are just used to pad instances where ks come after /s so that they don't get moved elsewhere, and the Ss are then removed

  • #s are then turned into r\ns

  • The string of ks is turned into an alternating 1010... string

  • The r\ns are turned into 1010...\ns

  • Every pair of 1010...\n1010\n is turned into 1010...\01010...;\n

  • Either 0; or 1; are trimmed off (because the 01010... string is too long by 1)

boboquack

Posted 2017-06-15T17:32:16.590

Reputation: 2 017

1

Python, 73 63 62 66 bytes

Saved 4 bytes thanks to officialaimm

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

Daffy

Posted 2017-06-15T17:32:16.590

Reputation: 985

1Declaring R=range(n) and using R saves 3 bytes. 1 unwanted space between ) and for can be removed as well. – officialaimm – 2017-06-16T07:40:43.380

1@officialaimm Sweet, thanks for the tip! I'm new a golfing. I'm thankful for any tips :) – Daffy – 2017-06-16T19:50:39.977

1

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 – 2017-06-17T01:37:51.933

1@officialaimm Noted. Still better than my original submission. Thanks a bunch. :) – Daffy – 2017-06-17T01:43:09.640

1

Mathematica, 28 bytes

Cos[+##/2Pi]^2&~Array~{#,#}&

Pure function taking a positive integer as input and returning a 2D array. Uses the periodic function cos²(πx/2) to generate the 1s and 0s.

For a little more fun, how about the 32-byte solution

Sign@Zeta[1-+##]^2&~Array~{#,#}&

which uses the locations of the trivial zeros of the Riemann zeta function.

Greg Martin

Posted 2017-06-15T17:32:16.590

Reputation: 13 940

1

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

streetster

Posted 2017-06-15T17:32:16.590

Reputation: 3 635

1

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.

daniero

Posted 2017-06-15T17:32:16.590

Reputation: 17 193

1

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

TessellatingHeckler

Posted 2017-06-15T17:32:16.590

Reputation: 2 412

1

Haskell, 43 bytes

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.

Post Rock Garf Hunter

Posted 2017-06-15T17:32:16.590

Reputation: 55 382

0

Mathematica, 28 bytes

Table[Mod[i+j+1,2],{i,#},{j,#}]&

J42161217

Posted 2017-06-15T17:32:16.590

Reputation: 15 931

0

Julia, 38 bytes

n->map(x->x%2,(1:n).+transpose(0:n-1))

The usual approach

Julian Wolf

Posted 2017-06-15T17:32:16.590

Reputation: 1 139

0

Python 2, 72 70 61 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!

totallyhuman

Posted 2017-06-15T17:32:16.590

Reputation: 15 378

62 bytes – Leaky Nun – 2017-06-15T19:06:19.463

54 bytes – Leaky Nun – 2017-06-15T19:11:06.943

0

Actually, 15 bytes

r;╗⌠╜+u⌠2@%⌡M⌡M

Try it online!

Leaky Nun

Posted 2017-06-15T17:32:16.590

Reputation: 45 011

0

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

xnor

Posted 2017-06-15T17:32:16.590

Reputation: 115 687

0

tcl, 82

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.

sergiol

Posted 2017-06-15T17:32:16.590

Reputation: 3 055

0

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][1]

Try it online!

Erlantz Calvo

Posted 2017-06-15T17:32:16.590

Reputation: 131

0

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.

Wossname

Posted 2017-06-15T17:32:16.590

Reputation: 221

0

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:/

user65167

Posted 2017-06-15T17:32:16.590

Reputation:

0

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

Charlie

Posted 2017-06-15T17:32:16.590

Reputation: 11 448

0

Perl 5, 49 bytes

$_=10x(($n=<>)/2).1x($n%2);say&&y/01/10/while$n--

Try it online!

Xcali

Posted 2017-06-15T17:32:16.590

Reputation: 7 671

0

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!

ceilingcat

Posted 2017-06-15T17:32:16.590

Reputation: 5 503