New answers tagged

0

Bash + Unix utilities, 77 74 bytes tee t|tr "`rs -T|sort -ur|xargs`" "`grep -q 9 t;seq $[8+$?] -1 0|xargs`"<t Try the test suite online! Input is a list of integers on stdin (space-separated digits). Output is in the same format, on stdout. This is an implementation of Surculose Sputum's nice method: List the unique digits that appear in the input, ...


2

J, 29 24 bytes (\:~@~.i.]){[:i.10-~9&e. Try it online! Thanks to Bubbler for catching some bugs. All are now fixed. Inspired by Surculose's sweet answer, be sure to upvote him.


2

Wolfram Language (Mathematica), 79 bytes Port of @SurculoseSputum answer #/.MapThread[Rule,{a=Union@#,Range[0,9][[-Tr[1^a]-(d=Boole[Max@a>8]);;-1-d]]}]& Try it online!


2

Erlang (escript), 143 bytes Port of Surculose Sputum's Python answer. u([])->[];u([H|T])->[H]++u([A||A<-T,A/=H]). i(X)->case X of true->1;_->0end. f(A)->[9-i(lists:member(9,A))-lists:sum([i(X>D)||X<-u(A)])||D<-A]. Try it online!


4

K (oK), 27 bytes {((9-9=|/x)-!#?x)(?x@>x)?x} Try it online! Takes the input as a list ot digits. Inspired by Kevin Cruijssen's 05AB1E and Surculose Sputum's Python solutions - please don't forget to upvote them!


0

This is a port of @Surculose Sputum's solution in APL2. U←((A⍳A)=⍳⍴A)/A←,A ((9∊A)⌽⌽⍳10)[U[⍒U]⍳A] It presupposes that A is a vector of numbers and that ⎕IO=0


3

W, 14 bytes Port of the 10-byte 05AB1E answer. ♥f<~|J▄5sO#o═╥ Uncompressed: oFU_:kkT-zrZ:a=-


3

Charcoal, 24 bytes ≔⌈Φχ¬№θIιη⪫⪪θ⎇⁼η⁹⌈θI⊕ηIη Try it online! Link is to verbose version of code. Uses a similar idea to @SurculoseSputum's answer. Explanation: ≔⌈Φχ¬№θIιη Find the highest digit not present in the input number. ⎇⁼η⁹⌈θI⊕η If that digit is less than 9 then increment it (which will give a digit in the input number) otherwise take the highest ...


15

Python 3, 58 52 bytes lambda n:[9-(9in n)-sum(x>d for x in{*n})for d in n] Try it online! Input: The number n, as a list of digits. Output: A similar number, as a list of digits. -6 bytes thanks to @xnor! How: Reverses the list of unique digits, then maps them to 9,8,7,... or 8,7,6,... depending on whether n contains the digit 9 or not. For example: ...


6

05AB1E, 20 16 10 bytes êRāTα:DIQ- -6 bytes after being inspired by @SurculoseSputum partially incorrect approach. I/O as a list of digits. Try it online or verify all test cases. Explanation: ê # Sort and uniquify the digits in the (implicit) input-list # i.e. [2,8,4,2] → [2,4,8] R # And reverse it # → [8,4,2] ...


8

JavaScript (ES6), 55 bytes I/O format: list of digits. a=>a.map(d=>d-=-!/9/.test(a)|!a.includes(d-=d>(a<'2'))) Try it online! Commented a => // a[] = input array of digits a.map(d => // for each digit d in a[]: d -= // update d: -!/9/.test(a) // if a[] doesn't include any 9, increment ...


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 with -apF'(?=\[)', 25 bytes Accepts input as: A [A-F,H-M] B [N-Z] $\||=$_ x"G "=~<>}{$\||=R Try it online! Verify all test cases. Note: the empty test case is missing from the verify all link because my test suite is bad and I should feel bad, but it works when tested in isolation.


2

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


1

PowerShell, 38 37 bytes ($args+'RG'|?{'G'-like"[$_ ]"})[0][0] Try it online! Takes strings as is, e.g. 'D(D,X-Z)'.


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 29 bytes 10(|-:@#$]#~@,"0|.)2&|0&,1+i. Try it online! J's reshape $ is so weird that it works in place of take {. when the left is positive singleton (regardless of what comes on the right). J, 30 bytes 10(|-:@#{.]#~@,"0|.)2&|0&,1+i. Try it online! Yet another case of repeat-bind (dyadic &) winning over other approaches. How ...


2

PowerShell, 38 bytes + to @Noodle9 $r=$args|?{'G'-match$_[1]} "R$r"[!!$r] Expects input via splatting. Try it online!


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!


2

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


2

Java 11, 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!


18

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

Wolfram Language (Mathematica), 124 bytes (r=StringReplace;s=ToString;i=-1;FixedPoint[(i++;r[r[#,s@i->"",1],s@i->StringCases[#,_~~_~~s@i][[1]]~StringTake~2])&,#,10])& Try it online!


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


0

Wolfram Language (Mathematica), 38 bytes Mod[2Last@ToCharacterCode@#,19]~Mod~4& Try it online! This borrows Uriel's formula, which happens to be the same length as dingledooper's, but I can't help but wonder if another formula would be shorter. I will investigate. For comparison, here's the string indexing solution (51 bytes): ("(|)D"~StringPosition~...


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

PowerShell, 69 58 bytes + to @Bubbler -11 bytes thanks to @mazzy $t='(/<[{}]>\)' $args|%{$a="$_$t"[-1-($t|% i*f $_)]+$a} $a Expects input via splatting. Try it online!


1

PowerShell, 16 bytes 2*$args[-1]%19%4 Try it online! PowerShell port of the modulo formula going around. Takes input via splatting


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

[Raku] (raku -p) (https://raku.org) 23 Bytes -> 19 Bytes $_=(4-.ords[*-1]*2%5)%4 $_=160%.ords[*-1]%7 #(with @dingledooper's formula)


1

Raku (raku -p), 66 61 58 56 Bytes my $a=-1;$_=S/(..)$a/{$0}/.subst(/$a/,$0,:g)until ++$a>9 First attempt. I am sure it can be improved


1

Haskell, 154 148 bytes n%c=show n==[c] n#(a:s@(b:c:_))|n%c=[a,b]|1>0=n#s _#_="" f n s|n>9||n#s==[]=s|1>0=f(n+1)$l++r>>= \c->last$[c]:[n#s|n%c]where(l,(_:r))=break(n%)s f 0 Try it online! Ungolfed: digitIsChar :: Int -> Char -> Bool digitIsChar n c = show n == [c] getSubStr :: Int -> String -> String getSubStr n (a:b:c:s) ...


0

Hy, 33 bytes (fn[s](%(%(*(ord(last s))2)19)4)) Try it online! It's a basic port of Uriel's answer. I might be able to shorten it by getting rid of a % but I need to play around with the syntax.


0

GolfScript, 11 bytes )3?11%2/(p; I'm convinced my method is the smallest possible, but this isn't the best possible one - just one of the earliest low-value ones. )3?11%2/(p; ) #Pop the last element of the string (as ascii value) 3? #Cube the number 11% #Modulo 11 2/ #Divide by 2 (round down) ( #Decrement by ...


0

T-SQL, 34 bytes SELECT CHARINDEX(RIGHT(@,1),'|)D')-1 Requires the variable @ to be declared and assigned the input. This code takes the right most character, and searches an indexed list of the possible mouthes. CHARINDEX returns a 1-based index. But, failing to find anything returns 0. So we leave the frown out of the search string (making the sad face ...


0

Canvas, 1 byte Exactly. A 1 byte built-in. ↔ Try it here!


Top 50 recent answers are included