| Bytes | Lang | Time | Link |
|---|---|---|---|
| 090 | Tcl | 170615T233622Z | sergiol |
| 052 | Janet | 250325T101547Z | xigoi |
| 092 | SAKO | 250324T151748Z | Acrimori |
| 057 | AWK | 250303T214944Z | xrs |
| 039 | Bash + rs | 170615T183723Z | Digital |
| 247 | Bespoke | 250216T131627Z | Josiah W |
| 053 | Zsh | 250216T071725Z | roblogic |
| 032 | Arturo | 230301T220015Z | chunes |
| 006 | Uiua | 231113T055927Z | chunes |
| 005 | Thunno 2 | 230622T172822Z | The Thon |
| 029 | TIBasic | 230322T181324Z | Youserna |
| 089 | Go | 230325T100705Z | The Thon |
| 005 | Vyxal | 230302T095610Z | The Thon |
| 008 | Pip S | 230322T202635Z | DLosc |
| 004 | Japt m | 230321T121034Z | Shaggy |
| 005 | Nekomata | 230302T001036Z | alephalp |
| 007 | Pyt | 230302T125020Z | Kip the |
| nan | 230302T094648Z | The Thon | |
| 005 | MATL | 170615T213405Z | Luis Men |
| 006 | Husk | 201022T121810Z | LegionMa |
| 059 | C gcc | 170908T202832Z | ceilingc |
| 043 | Haskell | 170908T032502Z | Wheat Wi |
| 049 | Perl 5 | 170908T024229Z | Xcali |
| 063 | C | 170615T195719Z | Steadybo |
| 142 | SwiProlog | 170620T083033Z | Tessella |
| 016 | q/kdb+ | 170616T173653Z | mkst |
| 066 | Python | 170616T071653Z | Daffy |
| 041 | J | 170617T013708Z | Tessella |
| 021 | braingasm | 170616T213504Z | daniero |
| 088 | C# .NET Core | 170616T202936Z | Charlie |
| 081 | Common Lisp | 170616T190525Z | user6516 |
| 077 | Java OpenJDK 8 | 170615T192806Z | PunPun10 |
| 037 | R | 170616T110213Z | user2390 |
| 007 | 05AB1E | 170616T062952Z | kalsower |
| 082 | Bash & coreutils | 170616T094135Z | Wossname |
| 028 | Mathematica | 170616T075807Z | Greg Mar |
| 030 | Retina | 170615T184538Z | Neil |
| 145 | C# | 170616T072122Z | Erlantz |
| 024 | Octave | 170616T051148Z | alephalp |
| 023 | Mathematica | 170616T052104Z | alephalp |
| 009 | J | 170616T002106Z | miles |
| 087 | /// | 170615T230534Z | boboquac |
| 5146 | JavaScript ES6 | 170615T183659Z | Downgoat |
| 036 | Clojure | 170615T211442Z | NikoNyrh |
| 038 | Haskell | 170615T175912Z | Julian W |
| 009 | Pyth | 170615T201508Z | KarlKast |
| 015 | Brachylog | 170615T195211Z | Fatalize |
| 054 | Python 2 | 170615T183638Z | totallyh |
| 043 | Python | 170615T192408Z | xnor |
| 015 | Actually | 170615T192217Z | Leaky Nu |
| 038 | Cheddar | 170615T191652Z | Leaky Nu |
| 008 | Charcoal | 170615T183855Z | Neil |
| 038 | Julia | 170615T182138Z | Julian W |
| 017 | CJam | 170615T181550Z | Erik the |
| 015 | V | 170615T174325Z | DJMcMayh |
| 056 | PHP | 170615T175138Z | Jör |
| 019 | Brachylog | 170615T180751Z | Leaky Nu |
| 019 | QBIC | 170615T175923Z | steenber |
| 007 | MATL | 170615T174740Z | Stewie G |
| 025 | Mathematica | 170615T175534Z | Martin E |
| 028 | Mathematica | 170615T174439Z | ZaMoC |
| 008 | APL Dyalog | 170615T174251Z | user4180 |
| 006 | Japt | 170615T173657Z | ETHprodu |
| 004 | Jelly | 170615T173308Z | Leaky Nu |
Tcl, 90 bytes
proc C n {time {time {puts -nonewline [expr [incr i]%2]} $n
puts ""
if $n%2<1 incr\ i} $n}
Janet, 52 bytes
|(seq[i :down[$ 0]](seq[j :down[$ 0]](%(+ i j 1)2)))
SAKO, 92 bytes
PODPROGRAM:F(N)
CALKOWITE:N,I
*)DRUKUJ(0):MOD(I,2)
SPACJI2×0*MOD(I,N)
POWTORZ:I=1(1)N*2
WROC
Outputs a list of lists.
Full programme version, 94 bytes
CALKOWITE:N,I
CZYTAJ:N
*1)DRUKUJ(0):MOD(I,2)
SPACJI2×0*MOD(I,N)
POWTORZ:I=1(1)N*2
STOP1
KONIEC
Bash + rs, 39
- 3 bytes saved thanks to @roblogic
eval echo \$[~{1..$1}+{1..$1}\&1]|rs $1
Bespoke, 247 bytes
daily I am torn
swaying between both of my given options
jumping to zero,then one,as usual
skipping over each in repeated cycles
anything I decide,I suddenly override
in this limited box,put in a restricted matrix
so am I evermore confined in here
For each X and Y value from n down to 1, I print !((x + y) % 2).
Arturo, 39 36 32 bytes
$[n][map n'x->map n'y->%x+y+1 2]
-3 thanks to alephalpha's simpler method
Prettified and rearranged for clarity:
$[n][ ; a function taking an argument n
map n 'x -> ; map over 1..n, assign current elt to x
map n 'y -> ; map over 1..n again, assign current elt to y
(x+y+1)%2 ; what it looks like
] ; end function
Thunno 2, 5 bytes
RɗDȷ=
Port of my Vyxal answer.
Explanation
# implicit input
R # one range
ɗ # mod 2
D # duplicate
ȷ # outer product:
= # equality
# implicit output
TI-Basic, 30 29 bytes
identity(Ans→[A]
Fill(1,[A]
cumSum([A]
2fPart(.5(Ans+Ansᵀ+[A]
Takes input in Ans.
-1 byte thanks to MarcMush
Go, 89 bytes
import."fmt"
func f(n int){
for i:=1;i<=n;i++{
for j:=i;j<i+n;j++{Print(j%2)}
Println()}}
Prints a multi-line string.
Explanation
import ."fmt" // Boilerplate
func f(n int) { // Function f taking an integer n:
for i := 1; i <= n; i++ { // Loop through each i in [1..n]:
for j := i; j < i+n; j++ { // Loop through each j in [i..i+n]:
Print(j%2)} // Print j mod 2 without a newline
Println()}} // Print a newline
Vyxal, 5 bytes
ʁ∷:v=
-3 thanks to @lyxal
-1 thanks to @emanresuA
Explanation
ʁ∷:v= implicit input
ʁ range(input)
∷ mod 2
: duplicate
v= equals, vectorised over the top list
(generates a nested list)
Old:
ɾ∷ẋ⁽†ẇ implicit input
ɾ range(1, input+1)
∷ mod 2
ẋ repeated input times
ẇ apply to every 2nd item:
⁽† vectorised not
ʁ:∷:†$"$İ implicit input
ʁ: range, duplicate
∷: mod 2, duplicate
† vectorised not
$" swap and pair
$İ swap and index in
Pip -S, 8 bytes
%U$+GMCa
Other list-formatting flags such as -l and -p work too. Without a flag, the list of lists is printed without any separators (e.g. 1001 for input 2), which seemed like stretching the permissive output format a bit too far.
Explanation
a ; Command-line argument
MC ; Map to a square coordinate grid of that size:
$+G ; Sum the two coordinates
U ; Increment
% ; Mod 2
Nekomata, 5 bytes
ᵒ+→2%
ᵒ+→2%
ᵒ+ Create an (input x input) matrix where the element at (i,j) is i+j
→ Increment
2% Mod 2
Pyt, 7 bytes
ř2%Đɐ=Ɩ
ř implicit input; řangify
2% modulo 2
Đ Đuplicate
ɐ= for ɐll possible pairs, check for equality
Ɩ coerce booleans to integers; implicit print
Prints list of lists
Thunno, \$ 12 \log_{256}(96) \approx \$ 9.88 bytes
RD2%D1_ZPsAH
Attempt This Online! or see it in a nicer format
Explanation
RD2%D1_ZPsAH # Implicit input Example: n=3
RD # Duplicate range(input) STACK: [0, 1, 2], [0, 1, 2]
2%D # Duplicate ^ mod 2 STACK: [0, 1, 2], [0, 1, 0], [0, 1, 0]
1_ # 1 - top copy of ^ STACK: [0, 1, 2], [0, 1, 0], [1, 0, 1]
ZP # Pair top two items STACK: [0, 1, 2], [[0, 1, 0], [1, 0, 1]]
sAH # 1-based modular indexing STACK: [[1, 0, 1], [0, 1, 0], [1, 0, 1]]
# Implicit output
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]
Haskell, 43 bytes
h=1:0:h
l=h:(0:h):l
s x=take x$map(take x)l
This creates the infinite checkerboard matrix and saves it to l. Then our function s chops off a square.
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);}
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 withr()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.
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...
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]
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
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.
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;}
I can't believe this is the shortest way to initialize a 2D array of integers, there must be another way...
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:/
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;}
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;
}
R, 38 37 bytes
n=scan();(matrix(1:n,n,n,T)+1:n-1)%%2
-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.
05AB1E, 9 7 bytes
-2 bytes thanks to Emigna
LDÈD_‚è
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
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.
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.
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.
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]
Mathematica, 23 bytes
ToeplitzMatrix@#~Mod~2&
///, 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?
VandDare 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 theks to the/r/S/blockSs are just used to pad instances whereks come after/s so that they don't get moved elsewhere, and theSs are then removed#s are then turned intor\nsThe string of
ks is turned into an alternating1010...stringThe
r\ns are turned into1010...\nsEvery pair of
1010...\n1010\nis turned into1010...\01010...;\nEither
0;or1;are trimmed off (because the01010...string is too long by 1)
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))
This outputs as an array of arrays. JavaScript ranges are pretty unweildy but I use [...Array(n)] which generates an array of size n
Clojure, 36 bytes
#(take %(partition % 1(cycle[1 0])))
Yay, right tool for the job.
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.
Brachylog, 15 bytes
^₂⟦₁%₂ᵐ;?ḍ₎pᵐ.\
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]]
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)]
Python, 43 bytes
lambda n:[('10'*n)[i:i+n]for i in range(n)]
Anonymous function that outputs a list like ['1010', '0101', '1010', '0101'].
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");
V, 16, 15 bytes
Ài10ÀñÙxñÎÀlD
Hexdump:
00000000: c069 3130 1bc0 adf1 d978 f1ce c06c 44 .i10.....x...lD
PHP, 56 bytes
Output as string
for(;$i<$argn**2;)echo++$i%2^$n&1,$i%$argn?"":"
".!++$n;
PHP, 66 bytes
Output as 2 D array
for(;$i<$argn**2;$i%$argn?:++$n)$r[+$n][]=++$i%2^$n&1;print_r($r);
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.
MATL, 7 bytes
:t!+2\~
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.
Mathematica, 25 bytes
1-Plus~Array~{#,#}~Mod~2&
Mathematica, 28 bytes
Table[Mod[i+j+1,2],{i,#},{j,#}]&
APL (Dyalog), 8 bytes
~2|⍳∘.+⍳
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.
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.
