Tag Info

53

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

32

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) @ % ... 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",], ["T","H","X",], [[["H"],"e",... 16 Haskell, 17 bytes f x=iterate(x!!)0 0-indexed Try it online! 16 convey, 36 29 bytes v<<<<,{ \~./." >"`=>! >!0<"} Try it online! { gets the input, that will get looped around in the upper two rows, while outputting downwards the length \, its indices /. and a copy of itself ". It will only continue after ~. (batch) if n elements reach ~., whereas n will be the length. We ... 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] = . 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)]*(q-L(p)) b=[S.join(a)for a in zip(*z)];t=b;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!

10

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.

10

Husk, 4 bytes ¡!¹← Try it online! 1-indexed, returns an infinite list. Explanation ¡!¹← ← take the first element as the start ¡ apply the following infinitely: !¹ index into the input

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

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

9

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 @LuisMendo saved 8 bytes @(N)ones(num2cell(!(1:N)+N){:})*N Try it online! Previous answer: @(N)repmat(N,num2cell(!(1:N)+N){:}) Try it online!

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

JavaScript (V8), 29 bytes Zero-indexed. Prints forever. (Where forever means until the call stack overflow.) f=a=>print(a.i=a[~~a.i])|f(a) Try it online! Commented f = a => // f is a recursive function taking the input array a[] print( // print: a.i = // update the current index, which is stored in the // ...

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

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