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 ...
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 ...
34
votes
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 ...
17
votes
Neutralize data
JavaScript (ES6), 53 47 bytes
f=o=>o.big?o.replace(/./g,' '):o.map?o.map(f):0
Test cases
...
17
votes
Follow a linked list
convey, 36 29 bytes
v<<<<,{
\~./."
>"`=>!
>!0<"}
Try it online!
{ gets the input, that ...
16
votes
15
votes
13
votes
13
votes
N-dimensional N^N array filled with N
Mathematica, 22 20 bytes
(t=Table)@@t[#,#+1]&
(* or *)
Table@@Table[#,#+1]&
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. ...
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
...
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., ...
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 ...
12
votes
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 ...
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 ...
11
votes
Sorting with a deque
K (ngn/k), 17 bytes
{&/(x=|\x)|x=&\x}
Every element must be the minimal (or the maximal) element of its prefix.
Try it online!
APL(Dyalog Unicode), 13 10 ...
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
...
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 @...
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))
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
10
votes
10
votes
10
votes
Follow a linked list
Husk, 4 bytes
¡!¹←
Try it online!
1-indexed, returns an infinite list.
Explanation
...
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!
10
votes
Implement a bag without replacement
Vyxal, 5 bytes
Inspired by Kevin Cruijssen's 05AB1E answer
6 bytes without the bonus:
{7Þ℅⟑,
Try it Online!
Explanation
...
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
...
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!
...
9
votes
N-dimensional N^N array filled with N
Haskell, 52 bytes
f n=iterate(filter(>'"').show.(<$[1..n]))(show n)!!n
Try it online!
Inspired by @nimi's answer, but using more predefined functions.
...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
data-structures × 45code-golf × 39
string × 9
array × 9
decision-problem × 4
sorting × 4
number × 2
ascii-art × 2
code-challenge × 2
popularity-contest × 2
graph-theory × 2
stack × 2
generation × 2
tree-traversal × 2
math × 1
sequence × 1
integer × 1
geometry × 1
graphical-output × 1
random × 1
date × 1
matrix × 1
parsing × 1
fastest-code × 1
puzzle-solver × 1