New answers tagged

1

Jelly, 13 bytes _Ḷ€ZŒHṚ;"¥/%⁵ A monadic Link accepting an integer which yields a list of lists of integers in \$[0,9]\$. Try it online! (footer just reformats the output list of lists) I feel there may be shorter. How? _Ḷ€ZŒHṚ;"¥/%⁵ - Link: integer, n € - for each (i) in (implicit range [1..n]) Ḷ - lowered range (i) -> [0.....


1

Perl 5 -n, 43 bytes @a=map$_%10x$_,$_&1^1..$_;say$_,pop@a for@a Try it online!


1

APL (Dyalog Unicode), 42 bytes 10|{⍵=1:1 1⍴1⋄2|⍵:⍵⍪∇⍵-1⋄(⍳∘≢,1+⊢,⊢/)∇⍵-1} Try it online! A fresh approach using recursion, though not very short. How it works 10|{⍵=1:1 1⍴1⋄2|⍵:⍵⍪∇⍵-1⋄(⍳∘≢,1+⊢,⊢/)∇⍵-1} ⍝ Input: n ⍵=1:1 1⍴1 ⍝ Base case: If n=1, give a 1x1 matrix of 1 2|⍵:⍵⍪∇⍵-1 ⍝ For odd n, prepend n copies of n on the top (⍳∘≢,1+⊢,⊢/)∇⍵-...


1

J, 30 bytes 10(|-:@#{.]#~@,"0|.)2&|0&,1+i. Try it online! Yet another case of repeat-bind (dyadic &) winning over other approaches. How it works 10(|-:@#{.]#~@,"0|.)2&|0&,1+i. NB. input=n 1+i. NB. 1..n 2&|0&, NB. prepend 0, but only if n is odd ( ] "0|.) NB. for ...


1

Ruby, 64 62 bytes ->n{c=1&~n;n,c=n-1,-~c,puts("#{n%10}"*n+"#{c%10}"*c)while c<n} Try it online! Based on @xnor's Python answer, thanks!


4

APL (Dyalog), 49 47 34 38 35 bytes 3 bytes saved thanks to @Bubbler! {10|(⌈⍵÷2)↑↑,/⍴⍨¨⍉↑((⍳⍵)-2|⍵)(⌽⍳⍵)} Try it online! ⍉↑ ⍝ concat each pair in ((⍳⍵) )(⌽⍳⍵) ⍝ 1..n and n..1 (into 2×n matrix) -2|⍵ ⍝ concats n-1..0 if n is odd ⍴⍨¨ ...


2

Erlang (escript), 109 bytes Seems huge in comparison with other answers. t(A,B)when A<B->"";t(A,B)->[string:copies([X rem 10+48],X)||X<-[A,B]]++" "++t(A-1,B+1). t(N)->t(N,1-N rem 2). Try it online!


1

J, 41 33 bytes -8 bytes thanks to Bubbler! 10(|-:@##"1~@{.],.|.)2&|@>:}.i.,] Try it online! K (oK), 41 38 bytes {(x%2)#{,/x#'10!x}'{(x-2!#x),'|x}1+!x} Try it online!


1

Java 8, 89 bytes n->{for(int c=~n&1;c<n;)System.out.println((n%10+"").repeat(n--)+(c%10+"").repeat(c++));} Port of @xnor's Python answer, so make sure to upvote him!! Try it online. Explanation: n->{ // Method with integer parameter and no return-type for(int c=~n&1; // Temp-integer `c`, ...


1

C (gcc), 85 82 bytes Saved 3 bytes thanks to newbie!!! i;c;f(n){for(c=-n%2;++c<n;--n,puts(""))for(i=0;i<n+c;)putchar((i++<n?n:c)%10+48);} Try it online! Port of xnor's Python answer so make sure to upvote him!!!


0

Icon, 76 bytes procedure f(n) c:=seq(1-n%2)&write(repl(n%10,n)||repl(c%10,c))&(n-:=1)=c end Try it online! Inspired by xnor's Python solution - don't forget to upvote it!


2

Charcoal, 24 bytes NθE…÷θ²θ⭆⟦⊕ι⁻|θ¹⊕ι⟧⭆λ﹪λχ Try it online! Link is to verbose version of code. Explanation: Nθ Input N. E…÷θ²θ Loop rows from N/2 to N. (Due to the increments in the code below, N/2 is excluded and N is included. I could have put the increments here for the same byte count.) ⭆⟦⊕ι⁻|θ¹⊕ι⟧ Each row contains two row-tiles, one for the row ...


0

Wolfram Language (Mathematica), 106 bytes (r=#+(y=Mod[#+1,2]);""<>{z@#,z[r-#]}&/@Range@r)[[-⌈r/2⌉;;-y-1]]& z@x_:=""<>ToString/@Table[x~Mod~10,x] Try it online!


15

Python 2, 59 bytes n=input() c=~n%2 while c<n:print`n%10`*n+`c%10`*c;n-=1;c+=1 Try it online! Prints like: 55555 44441 33322 It looks kind-of redundant to update n-=1;c+=1 where sum n+c remains unchanged. I feel like there's a better way, but I haven't seen it so far. Bounty is up for grabs! 60 bytes n=input() b=a=n/2 while n-b:b+=1;print`a%10`*a+`...


2

JavaScript (ES8),  72 71  68 bytes Returns a string. n=>(g=k=>k<n?(h=k=>''.padEnd(k,k%10))(k)+h(n--)+` `+g(k+1):'')(~n&1) Try it online!


2

05AB1E, 16 15 14 bytes Ýεθy×}2äí`RøJ» Try it online!


1

Ruby, 97 bytes ->s,n{t=(?.*s+$/)*s i=1 (t[i*-~s/s]=t[i/s-i%s*~s]=?X;i+=1)until 2>m=n-t.count(?X) m>0&&t[0]=?X t} Try it online! Llamda function. Returns a newline separated string with X and . can be shorter if a single line string is acceptable. Very simple: Make a newline separated string of s lines of s . Set i=1 and scan through all ...


1

Charcoal, 39 bytes NθNηG←↑⊖θ.UM✂KA⁰⊘η¹x↑‖M↗P↘⭆θ§.x›η⁺ι№KAx Try it online! Link is to verbose version of code. Explanation: NθNη Input S and N. G←↑⊖θ. Fill a triangle of size S-1 with .s. UM✂KA⁰⊘η¹x Change up to N/2 of those .s to xs. ↑‖M↗ Reflect to create the diagonal symmetry, but leaving the diagonal empty. P↘⭆θ§.x›η⁺ι№KAx Count the number of ...


1

PowerShell, 153 140 136 bytes param($l,$n)if($l--){0..$l|%{$y=$_ -join(0..$l|%{$n-=$a=($_-eq$y-and$n)+2*($_-gt$y-and$n-gt1) '.x'["$y;$_"-in($c+=,"$_;$y"*$a)-or$a]})}} Try it online! Unrolled: param($length,$n) if($length--){ 0..$length|%{ $y=$_ -join(0..$length|%{ $draw = 1*($_ -eq $y -and $n) + # draw 1 element on ...


2

Bash + Unix utilities, 152 bytes a=`dc<<<4dk$2*1+v1-2/0k1/p` for((q=$2-a*a-a,b=++a<$1?q/2:0;i<$1;++j>=$1?j=0,i++:0)){ printf $[i-j?i<a&j<a?1:i==a&j<b|j==a&i<b?1:0:i<q-2*b?1:0]\ ;}|rs $1 Try the test suite online! \$S\$ and \$N\$ are read from stdin, and the output is printed to stdout. The characters used are ...


1

Perl 5, 139 bytes sub f{($s,$n)=@_;$_='-'x$s x$s;$i=-1;while($n){$j=++$i%$s*$s+int$i/$s;$n<2&&$i-$j&&next;for$o($i,$j){$n-=s,^(.{$o})-,$1x,}}s/.{$s}/$&\n/gr} Try it online! sub fungolfed { ($s,$n) = @_; # input params s and n $_ = '-' x $s x $s; # $_ is the string of - and x $i=-1; ...


4

MATL, 21 20 bytes U:<~Zc`tnZ@)[]ett!-z Try it online! Characters are # and . The output is random, that is, it may be different every time the program is run. Running time is also random, but the program is guaranteed to finish in finite time. How it works General idea The code first generates a numeric vector with N ones and S^2-N zeros (U:<~), ...


4

JavaScript (ES6),  116 114  110 bytes Takes input as (s)(n). Returns a matrix of Boolean values. s=>n=>[...Array(s)].map((_,y,a)=>a.map((_,x)=>(p=Math.min(n&~1,s*s-s),x-y?(x<y?y*y-y+2*x:x*x-x+2*y)<p:x<n-p))) Try it online! How? The output matrix is divided into 2 parts: the cells located on the main diagonal (type A) ...


3

Wolfram Language (Mathematica), 113 bytes Searches random matrices [S,N] until matrix g is equal to Transpose(g) Outputs a binary matrix If[#<1,"",g=(P=Partition)[k=Join@@{1~Table~#2,Table[0,#^2-#2]},#];While[g!=Transpose@g,g=P[RandomSample@k,#]];g]& Try it online! Wolfram Language (Mathematica), 75 bytes Here is also a deterministic version that ...


7

Python 2, 116 113 112 101 bytes lambda s,n:[[[n>i<s-(s+n&1),[i*~-i+2*j,j*~-j+2*i][i<j]<n-s][j!=i]for j in range(s)]for i in range(s)] Try it online! Input: 2 integers s and n Output: A 2D list of True and False, representing x and . respectively. Explanation The strategy is to put as many xs on the diagonal as possible. If \$s\$ and \$...


0

ArnoldC, 498 bytes IT'S SHOWTIME HEY CHRISTMAS TREE n YOU SET US UP 0 GET YOUR ASS TO MARS n DO IT NOW I WANT TO ASK YOU A BUNCH OF QUESTIONS AND I WANT TO HAVE THEM ANSWERED IMMEDIATELY HEY CHRISTMAS TREE m YOU SET US UP 0 GET TO THE CHOPPER m HERE IS MY INVITATION n GET UP n GET UP 1 ENOUGH TALK GET TO THE CHOPPER m HERE IS MY INVITATION m GET DOWN 1 I ...


2

05AB1E, 48 47 bytes g≠iā<DδmUεXøINǝ}Xšεā<sUœε©2.ÆíÆ.±Xε®Nèè}«P}O}ć÷ Sometimes 05AB1E's lack of almost all matrix builtins is pretty annoying.. ;) Inspired by @Arnauld's JavaScript answer. Try it online or verify almost all test cases (removed the last two largest ones, since they time out on TIO). Explanation: First handle the edge case of a ...


1

APL (Dyalog Unicode), 10 bytesSBCS ⊢⌹∘.*⍨∘⍳∘≢ Try it online! A port of Graham's APL+WIN solution into a modern APL, which happens to work exactly the same (and have the same byte count) as my own J solution. How it works ⊢⌹∘.*⍨∘⍳∘≢ ⍝ Input: V, result of a polynomial evaluated at 0..m-1 ⍳∘≢ ⍝ Generate 0..m-1 ∘.*⍨∘ ⍝ Self outer product by * ...


1

Haskell, 77 bytes h%(a:t)=h-a:a%t h%_=[h] f(h:t)=h:foldr(%)[](f$zipWith((/).(-h+))t[1..]) f e=e Try it online!


0

Jelly, 14 bytes J’*þ`æ*-⁸æ×ær0 A monadic Link accepting a list of integers which yields a list of the exponents (floats and/or integers) with the lowest degree on the left of the same length as the input (with trailing zeros if need be). Try it online! How? J’*þ`æ*-⁸æ×ær0 - Link: list of integers, V J - range of length (V) ’ - ...


3

J, 10 bytes %.^/~@i.@# Try it online! Obligatory J answer on a matrix-related challenge. Takes input as a vector of extended integers (otherwise the answer may have small floating-point errors), and gives the polynomial's coefficients in lowest-first order, possibly with some extra zeroes at the end. How it works %.^/~@i.@# NB. Input: a vector V of ...


2

Charcoal, 68 62 bytes ≔⟦¹⟧ηFLθ«⊞υ⁰≔÷⁻§θιΣEυ×κXιλ∨ΠEι⊕κ¹ζUMυ⁺κ×ζ§ηλ⊞η⁰≔Eη⁻§η⊖λ×κιη»Iυ Try it online! Link is to verbose version of previous version of code that excludes trailing zeros, but apparently it isn't necessary to do that, thus saving 6 bytes. Outputs the terms in power order i.e. the constant term is printed first. Explanation: ≔⟦¹⟧η Start by ...


1

MATL, 12 bytes n:qGyz3$ZQYo The result is given with higher-order coefficients first, and may contain leading zeros. Try it online! Or verify all test cases Explanation Consider input [-3, -1, 5, 15, 29] as an example. n:q % Implicit input. Number of elements. Range. Subtract 1, element-wise % STACK: [0, 1, 2, 3, 4] G % Push input again ...


1

SageMath, 63 48 bytes lambda v:QQ[x].lagrange_polynomial(enumerate(v)) Try it online! Outputs the polynomial as $$a_k n^k + \cdots + a_3 n^3 + a_2 n^2 + a_1 n + a_0 $$


2

Python 3 + Numpy, 69 bytes lambda x:polyfit(range(len(x)),x,len(x)-1).round() from numpy import* Try it online! May have leading zeros.


5

R, 55 52 bytes -3 bytes thanks to Giuseppe. round(solve(outer(n<-seq(a=u<-scan())-1,n,"^"))%*%u) Try it online! Outputs \$(a_0, a_1,\ldots,)\$ with possible trailing zeros. Let \$u\$ be the output sequence, and \$X\$ be the \$m\times m\$ matrix such that \$X_{i,j}=i^j\$ (0-indexed), i.e. \$ X=\begin{pmatrix} 1&0&0&\ldots&0\\ 1&...


2

Pari/GP, 38 bytes a->Vecrev(polinterpolate([0..#a-1],a)) Try it online!


4

Wolfram Language (Mathematica), 50 49 37 bytes Returns a polynomial. Mathematica is so awesome x+1 can be used as a variable in this context. Apart is a weird built-in that, quoting from the docs, seems to attempt to rewrite an expression as a sum of terms with minimal denominators, and also happens to expand polynomials (that are returned in a weird ...


8

JavaScript (ES7),  193 ... 154  145 bytes Saved 9 bytes thanks to @Bubbler Returns \$(a_0,a_1,...,a_k)\$ with some possible trailing zeros. v=>v.map((_,i)=>(g=(i,m=v.map((n,y)=>v.map((_,x)=>x==i?n:y**x)))=>+m||m.reduce((s,[v],i)=>v*g(0,m.map(([,...r])=>r).filter(_=>i--))-s,0))(i)/g()) Try it online! (removed the ...


4

APL+WIN, 16 bytes Index origin = 0 Prompts for input as a vector and outputs coefficients from a0 to an-1 where n is the length of the vector. The order of the polynomial can be obtained by summing the number of coefficients up to the last none zero coefficient: 0⍕n⌹m∘.*m←⍳⍴n←,⎕ Try it online! Courtesy of Dyalog Classic


0

Desmos, 12 bytes f(x)=sign(x) or $$f\left(x\right)=\operatorname{sign}\left(x\right)$$ Try It On Desmos!


0

dc, 7 bytes ?dd*v/p Try it online! Input on stdin, output on stdout. (There may also be spurious output to stderr, which should be ignored, as usual.) This uses the formula $$\frac{\left\lfloor\sqrt{n^2}\right\rfloor}n$$ plus the fact that if you divide by 0 in dc, it leaves the 0 at the top of the stack. (If you try this out, note that negative ...


1

Python 3, 25 bytes lambda x:x and(1,-1)[x<0] Try it online! Uses a different approach from other python answers


1

Symja, 29 26 bytes f(x_):=If(x==0,0,x/Abs(x)) Try It Online! -3 bytes to due porting the idea behind the Io answer. Answer History 29 bytes f(x_):=If(x<0,-1,If(x>0,1,0)) For some reason, my edit history didn't save this old approach.


1

Io, 28 bytes Does a conditional checking over x/abs(x). method(x,if(x!=0,x/x abs,0)) Try it online!


-2

CommonJS (unsig,ugly,selfcnt) 48B m=n=>(n+1)%256;module.exports=a=>b=>[m(a),m(b)]; Readable version: submission = number => (n + 1) % 256; // Add 1, wrapping around. module.exports = number1 => (number2 => [submission(number1), submission(number2)]); // Apply submission to the input numbers. JavaScript (unsig,ugly,expr) 43B a=>b=>...


2

brainfuck, 69 bytes ,+[->+>+>+<<<],[->>->-<<<]++>>>[[-]<<<->.>>]<+<+>[[-]<<->.>]<+<[->.<] Try it online! My solution takes the first input and counts it upwards and prints the first two numbers unless they are the second input. So if the first input is A and ...


0

Ruby, 22 bytes ->*a{([*6..9]-a)[0,2]} Try it online! Same method as almost everybody.


1

Python 3.8, 31 29 bytes Saved 2 bytes thanks to xnor!!! lambda x,y:(k:=~x&1|~y&2,k+4) Try it online!


1

Bash + Core utilities, 29 bytes seq 4|egrep -v $1\|$2|head -2 Try it online! Input is passed in two arguments, and the output is printed to stdout. This prints the first two positive integers that aren't either of the two arguments.


Top 50 recent answers are included