51

Python, 32 bytes lambda n:eval('['*n+'n'+']*n'*n) Try it online! Makes a string like "[[[n]*n]*n]*n" with n multiplcations, and evaluates it as Python code. Since the evaluation happens within the function scope, the variable name n evaluates to the function input.


44

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 a+b to notate that a is friends with b, a*b that a knows b and a?b that b should be suggested to a or not. The first line simply says that X*Y is true if either X+Y, Y+X or X == Y is true. This implements the ...


31

J, 4 bytes $~#~ Try it online! Explanation $~#~ Input: integer n #~ Create n copies of n $~ Shape n into an array with dimensions n copies of n


19

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 test cases. Explanation " % For each element in the input (implicit) @ % Push current element ? % If non-zero (this consumes the current element) @ % Push ...


17

JavaScript (ES6), 53 47 bytes f=o=>o.big?o.replace(/./g,' '):o.map?o.map(f):0 Test cases f=o=>o.big?o.replace(/./g,' '):o.map?o.map(f):0 test = [ "", 7, 123.456, "X", " ", "Yo!", [], [-1.2E3], ["Hey"], ["H","e","y"], ["R",2,"D",2], ["C","3","P",0], ["THX",[1138]], ["T","H","X",[1138]], [[["H"],"e",...


15

PHP, 138 133 129 bytes PHP beats Mathematica - a rare occurence. for(;$s=fgets(STDIN);$s>G?print$$a[$b]?$s<L:$s>L&&@array_intersect_key($$a,$$b):$$a[$b]=$$b[$a]=1)[,$a,$b]=explode(" ",trim($s)); prints 1 for truthy, empty string for falsy. Run with -nr or test it online. needs PHP 7.1 for the list assignment; user names are case sensitive ...


13

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. Example : [1,0,2] will return [2,0,2] which is to be interpreted as [2,0,2] = [2]. a->{int s=0;for(int v:a)a[v>0?s++:--s]=v;} Try it online! Previous versions: Java (JDK 10), 60 bytes l-&...


12

APL (Dyalog APL), 4 bytes ⍴⍨⍴⊢ Try it online!


12

Mathematica, 22 20 bytes (t=Table)@@t[#,#+1]& (* or *) Table@@Table[#,#+1]&


12

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 @call echo(%%%1.%2%% Prints 1 for friends, a blank line for strangers. SUGGEST.CMD @call set k=0%%%1.%2%% @set k=1&if %k%==0 for /f "tokens=2 delims=.=" %%f in ('set %1.')do @call set k=%%k%%%%%%f.%2%% @echo(%k:~1,1% Prints 1 or a blank ...


12

PowerShell, 46 41 40 bytes $args|%{$x,$a=&({1,$_+$a},{$a})[!$_]};$a Try it online! Takes input via splatting, e.g., $z=@(12,3,0,101,11,1,0,0,14,0,28); .\implement-stack.ps1 @z, which on TIO manifests as separate arguments. $args|%{$x,$a=&({1,$_+$a},{$a})[!$_]};$a # Full program $args # Take input via ...


12

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 than an iterated finite state machine. Regular expressions are a tool for building finite state machines, and sed can iterate. It's a match made in heaven. Takes input from stdin, like so: ...


12

Python 3, 400 393 390 bytes L=len S,*K=' ┴┼│123456789' def T(x): try:return[str(x+0)] except: z=[*map(T,x)];q=max(map(L,z)) for p in z:p+=[S*L(p[0])]*(q-L(p)) b=[S.join(a)for a in zip(*z)];t=b[0];l=L(t);s=0;e=L(z);r=[S]*l if e<2:return['│'.center(l),*b] for i in range(l): if t[i]in K:s+=1;r[i]='┬┌┐'[(s<e)-(s>1)] elif 0<s<e:r[...


11

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 recursively, until fully wound, and then backtracks through the list. r keeps track of how many numbers to discard before printing something. Increases if current item is 0, decreases otherwise. If 0, we ...


10

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 saving 4 bytes.


10

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

Haskell, 28 bytes foldl(#)[] (_:s)#0=s s#n=n:s Try it online!


9

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 explanation will be using N=5. Part 1: Create the bottom layer on every stack The reason why this is needs a separate piece of code is because every stack needs to be used: the first 4 need a 5 to be put ...


9

APL (155) ∆←{O←⍺⍺⋄'+'=O←⎕CR'O':R[⍋R←⍺,⍵]⋄'-'=O:⍺{⍵≡⍬:⍺⋄(⍺/⍨(⍳⍴⍺)≠⍺⍳⊃⍵)∇1↓⍵}⍵⋄(⍬≡⍴⍵)∧K←'×'=O:⍵/⍺⋄K:⍺/⍵⋄'÷'=O:∪⍺⌿⍨⍵≤+/⍺∘.=⍺⋄'⊂'=O:⍵{(∪⍺)≢∪⍵:0⋄1+⍺∇⍵-∆⍺}⍺⋄⍺[⍋⍺]≡⍵[⍋⍵]}⋄⎕ This defines an operator ∆ 'bag', which defines bag operations for given functions. I.e. +∆ would be addition. It then reads a line from the keyboard and evaluates it as an APL expression. ...


9

R, 26 This is the obvious answer but perhaps there is something cleverer? n=scan();array(n,rep(n,n))


9

Brain-Flak, 40 36 bytes ([]){{}{({}<>)<>}([]){{}<>}{}([])}<> Try it online! Thanks to @Nitrodon for -4 bytes. Since Brain-Flak already uses stacks, this is a good puzzle for Brain-Flak. ([]){ while items on stack {} pop stack count { if top element is non-zero ({}<>)<> push it on the ...


9

Python 2, 59 57 51 bytes s=[] for x in input():s=(s+[x],s[:-1])[x<1] print s Try it online!


9

Jelly, 6 bytes ṣ0Ṗ;¥/ Try it online! How it works ṣ0Ṗ;¥/ Main link. Argument: A (array) ṣ0 Split A at zeroes. ¥/ Left-reduce the resulting 2D array by this dyadic chain: Ṗ Pop; discard the last element of the left argument. ; Concatenate the result with the right argument.


8

Haskell Haskell is pretty good at defining obvious things from scratch. Almost all definitions are simple translations of math axioms. Doesn't support negative numbers, behaves badly on errors (e.g. 1-2 or 1/0). data MyNum = Zero | S MyNum toNative :: MyNum -> Integer toNative Zero = 0 toNative (S n) = (toNative n) + 1 fromNative :: Integer -> MyNum ...


8

Python 2, 77 S={'0':0} for w in input().split('\n'):S[w]+=1;S[' '+w]=0;print w[:-1]+`S[w]` Like Sp3000's answer, but with a dictionary. The dict S stores the current number for each nesting level '0', ' 0', ' 0' and so on. For each line in the input, increment the corresponding nesting level, and reset the nesting level one higher to 0.


8

JavaScript (ES6),  44  40 bytes f=(n,k=i=n)=>i--?f(n,Array(n).fill(k)):k Demo f=(n,k=i=n)=>i--?f(n,Array(n).fill(k)):k console.log(JSON.stringify(f(1))) console.log(JSON.stringify(f(2))) console.log(JSON.stringify(f(3))) console.log(JSON.stringify(f(4)))


8

05AB1E (legacy), 6 5 bytes -1 thanks to Kevin Cruijssen F¹.D) Try it online! F # For 0 .. input ¹.D) # Push <input> copies of the result of the last step as an array


8

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. Uses iterate and !! instead of a recursive help function. Instead of constructing list delimiters "by hand", uses filter(>'"').show to format a list of strings, then stripping away the extra " ...


8

Haskell, 93 92 84 83 82 bytes data B=B[B]Int|L k!B[l@(B[x,y]a),r]n|k<n=B[k!l,r]n|k>n=B[l,k!r]n|1>0=B[x,B[y,r]k]a Thanks to @BMO, @alephalpha and @Laikoni for a byte each and @nimi for eight bytes! Try it online!


8

Wolfram Language (Mathematica), 28 bytes #//.{a___,b_,0,c___}:>{a,c}& Try it online!


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