Skip to main content
Share Your Experience: Take the 2024 Developer Survey
57 votes

N-dimensional N^N array filled with N

Python, 32 bytes lambda n:eval('['*n+'n'+']*n'*n) Try it online! Makes a string like "[[[n]*n]*n]*n" with ...
xnor's user avatar
  • 145k
44 votes
Accepted

Should we be friends?

SWI-Prolog, 62 47 41 bytes X*Y:-X+Y;Y+X;X==Y. X?Y:-not(X*Y),X*Z,Y*Z. Prolog isn't too often useful, but when it is it's just beautiful. We'll use ...
orlp's user avatar
  • 39.1k
34 votes

N-dimensional N^N array filled with N

J, 4 bytes $~#~ Try it online! Explanation ...
miles's user avatar
  • 16.9k
19 votes

Implementing a stack

MATL, 6 bytes "@?@}x Input is a row vector of numbers. The final stack is shown upside down, with the most recent element below. Try it online! Or verify all ...
Luis Mendo's user avatar
  • 105k
17 votes

Neutralize data

JavaScript (ES6), 53 47 bytes f=o=>o.big?o.replace(/./g,' '):o.map?o.map(f):0 Test cases ...
Arnauld's user avatar
  • 193k
16 votes

Follow a linked list

Haskell, 17 bytes f x=iterate(x!!)0 0-indexed Try it online!
MennoK's user avatar
  • 271
16 votes

Follow a linked list

convey, 36 29 bytes v<<<<,{ \~./." >"`=>! >!0<"} Try it online! { gets the input, that ...
xash's user avatar
  • 11.3k
14 votes

Should we be friends?

PHP, 138 133 129 bytes PHP beats Mathematica - a rare occurence. ...
Titus's user avatar
  • 14.7k
13 votes

N-dimensional N^N array filled with N

APL (Dyalog APL), 4 bytes ⍴⍨⍴⊢ Try it online!
Dennis's user avatar
  • 210k
13 votes

N-dimensional N^N array filled with N

Mathematica, 22 20 bytes (t=Table)@@t[#,#+1]& (* or *) Table@@Table[#,#+1]&
JungHwan Min's user avatar
  • 13.8k
13 votes

Implementing a stack

Java (JDK 10), 42 bytes Since "[the] output is flexible [...], the only thing that matters is the order", this changes the input array into a 0-terminated array. ...
Olivier Grégoire's user avatar
12 votes

Should we be friends?

CMD (Batch), 50 + 20 + 135 = 205 bytes FRIEND.CMD @for %%f in (%1.%1 %1.%2 %2.%2 %2.%1)do @set %%f=1 KNOW.CMD ...
Neil's user avatar
  • 171k
12 votes

Implementing a stack

PowerShell, 46 41 40 bytes $args|%{$x,$a=&({1,$_+$a},{$a})[!$_]};$a Try it online! Takes input via splatting, e.g., ...
AdmBorkBork's user avatar
  • 43.4k
12 votes

Implementing a stack

Sed, 17 Bytes :;s/[0-9]\+,0//;t -3 bytes thanks to @OMᗺ, -1 thanks to @eggyal Because you're guaranteed to never pop an empty list, you don't need anything more ...
Tacroy's user avatar
  • 221
12 votes

Drawing a tree from an array

Python 3, 400 393 390 bytes ...
Beefster's user avatar
  • 9,951
11 votes

Implementing a stack

C (gcc), 62 60 56 55 bytes -2 -6 bytes thanks to l4m2 -1 byte thanks to ceilingcat. Uses the permitted notion of -1 terminated arrays. f() calls itself ...
gastropner's user avatar
  • 4,373
11 votes

CGAC2022 Day 7: Fen The Wicked

Python 3.8, 45 bytes lambda l,i=0:[sum(l[i&(i:=i+1):i])for _ in l] Try it online! The start index for the sum at one-indexed position ...
xnor's user avatar
  • 145k
10 votes

Is it a max-heap?

JavaScript (ES6), 34 30 bytes a=>!a.some((e,i)=>e>a[i-1>>1]) Edit: Fixing my code for the spec clarification cost 1 byte, so thanks to @edc65 for ...
Neil's user avatar
  • 171k
10 votes

N-dimensional N^N array filled with N

JavaScript (ES6),  44  40 bytes f=(n,k=i=n)=>i--?f(n,Array(n).fill(k)):k Demo ...
Arnauld's user avatar
  • 193k
10 votes

N-dimensional N^N array filled with N

Octave, 35 33 25 23 20 bytes @(N)ones(N+!(1:N))*N Try it online! @(N)ones(N*ones(1,N))*N @(N)repmat(N,N*ones(1,N)) Thanks to @...
rahnema1's user avatar
  • 5,665
10 votes

N-dimensional N^N array filled with N

R, 26 This is the obvious answer but perhaps there is something cleverer? n=scan();array(n,rep(n,n))
Flounderer's user avatar
10 votes

Implementing a stack

R, 45 bytes o={};for(e in scan())o="if"(e,c(e,o),o[-1]);o Try it online! -4 byte thanks to @Giuseppe
digEmAll's user avatar
  • 5,029
10 votes

Implementing a stack

Haskell, 28 bytes foldl(#)[] (_:s)#0=s s#n=n:s Try it online!
nimi's user avatar
  • 35.8k
10 votes

Implementing a stack

Jelly, 6 bytes ṣ0Ṗ;¥/ Try it online! How it works ...
Dennis's user avatar
  • 210k
10 votes

Follow a linked list

Husk, 4 bytes ¡!¹← Try it online! 1-indexed, returns an infinite list. Explanation ...
Razetime's user avatar
  • 27.2k
10 votes

Keep elements in sequence that have a letter repeated at least 3 times

Python 3, 51 50 bytes lambda l:{s for s in l for c in s if s.count(c)>2} Try it online!
The Thonnu's user avatar
  • 18.1k
10 votes

Implement a bag without replacement

JavaScript (ES6), 50 bytes A function that draws one piece at a time. m=f=_=>m^(m|=1<<(i=Math.random()*7))?-~i:f(m%=127) Try it online! Commented ...
Arnauld's user avatar
  • 193k
9 votes

Stack Exchanging

Pyth 96 94 bytes Mt*Q+++bGdHM|%+y_GHQQg1 2++Qd1g2 3g2 1g3 1++Qd2Vr3QgNtN++QdN;g1QVStQVStQI<NHgnNHnNtH)++nN0dnNH Try it here How does it work? This ...
K Zhang's user avatar
  • 7,116
9 votes

Implement bag operations

APL (155) ...
marinus's user avatar
  • 31.2k
9 votes

N-dimensional N^N array filled with N

05AB1E (legacy), 6 5 bytes -1 thanks to Kevin Cruijssen F¹.D) Try it online! ...
Riley's user avatar
  • 11.7k

Only top scored, non community-wiki answers of a minimum length are eligible