Podcast #128: We chat with Kent C Dodds about why he loves React and discuss what life was like in the dark days before Git. Listen now.

Hot answers tagged

12

Python 2, 76 bytes f=lambda n,k=1000:n>19and(f(n/k,k/10)+`k-k%4*5`)*(n>=k)+f(n%k,k/10)or`n`[:n] Try it online! Test suite from Chas Brown. A fully recursive solution that splits on place values k=1000, k=100, and k=10. The separator for the place values is computed as k-k%4*5, which maps 1000 -> 1000 100 -> 100 10 -> 0 By stopping ...


7

Haskell, 82 73 bytes r=read.reverse.show f 1=0 f a=1+(minimum$f(a-1):[f$r a|r a<a,mod a 10>0]) Try it online! Simplest recursion method. -9 bytes thanks to Christian Sievers


6

Wren, 126 124 bytes As always Wren is the longest. I hate Wren's verbosity. Fn.new{|a|a.map{|i|i>0?"":"-"+(i.abs.toString[-1..-(i.abs.toString.count)])}.map{|i|Num.fromString(i)}.reduce{|x,y|x<y?x:y}} Try it online! Explanation Fn.new{ // ...


6

Python 2, 61 60 59 bytes lambda n:[a*(1+.03*n)**i*75for a in 4,8for i in range(n/2)] Try it online! -1 byte, thanks to Joseph Sible


5

C++, 140 159 147 145 bytes Edit: new solution without standard library, using a recursive function and pointer magic (constant 20 experimentally determined and not the same on other compilers) Edit 2: -2 bytes thanks to ceilingcat int*s,S,*G,x;int F(int Z,int*p=&x){int W=1,a=(*p)++,r=0;for(;r=r*10+a%10,a/=10;);G?W=r,p<=G?G=&W,S++,p=s:0:p=s=G=&...


5

Perl 5, 51 bytes: sub f{($x,$y)=map{/-?/;$&.reverse$'}@_;$x>$y?$x:$y} Try it online! ...or if using a function from a core module is allowed (max in List::Util) it's 35 bytes: 35 bytes: sub f{max map{/-?/;$&.reverse$'}@_} Try it online!


4

Gaia, 44 43 bytes :(!> ℍZ:₸Z¤1>∧'0פṙ$×:dẏ×,↑ℍṙ× @3eZ,↑¦↓3eṙ× Longer than my original answer, but it works now. fixed my code again but now it's twice as sad Try it online! @3eZ,↑¦↓3eṙ× Main function. @3eZ Input divmod 1000, pushes input div 1000 then input mod 1000 , Pair those two value into a list ↑¦ Run the ...


4

Zsh, 63 bytes for x;a+=(${(M)x#-}${(j::)${(Oas::)x#-}}) <<<$a[1+$[a[1]<a[2]]] Try it online! ${x#-} removes the leading - if it exists. Adding the (M) flag causes the - to be substituted instead of what remains. Then this construct reverses the remaining string: ${(j::)${(Oas::)var}}. For each element, we append to an array, then use a ...


4

05AB1E, 32 29 25 bytes žDL.Xʒ•BÌË"•4в2ôsÇ87%èøOQ -7 bytes thanks to @Grimy. Try it online or verify all test cases. Explanation: žD # Push builtin 4096 (the maximum Roman number is 3999) L # Create a list in the range [1,4096] .X # Convert each integer to a Roman number ʒ # Filter this list ...


4

Python 3, 89 86 bytes Disclaimer: This answer does not work for the second example #4 due to floating point accuracy constraints in Python. Feel free to disqualify. Update: This answer now works for all examples thanks to @JonathanAllan f=float r=lambda a:-f(a[:1:-1])if eval(a)<0else f(a[::-1]) m=lambda a,b:max(r(a),r(b)) Try it online!


4

JavaScript (ES6), 100 bytes Takes input as (a)(b). a=>b=>Math.max((g=n=>(s=n>0||-1)*[...(s*n).toFixed(9).replace(/\.0+$/,0)].reverse().join``)(a),g(b)) Try it online!


4

05AB1E, 6 7 6 bytes +1 byte to fix cases when only one of the inputs is negative. -1 byte by Grimmy í'-δ†Z Try it online! Test all inputs The input is taken as a list of strings [x, y]. Explanation: í Reverse each input '- Push a minus sign δ† Apply "filter out the minus sign to the front" to each input Z Take the maximum


4

Python 3.8, 69 bytes lambda*v:max(map(lambda x:(-1)**(z:=eval(x)<0)*float(x[z:][::-1]),v)) An unnamed function accepting 2 (actually, any number of) strings, which yields the maximal reversed version as a floating point number. Try it online!


4

Wolfram Language (Mathematica), 36 bytes {#,2#}&[300(1+.03#)^Range[0,#/2-1]]& Try it online!


3

My first try on Code Golf, please point out some optimizations! Python 3.7, 465 449 364 363 284 Bytes Edit: Thanks for the improvements! Jo King managed to squeeze this logic from 449 to just 364 bytes, I took a single Byte off it so I felt justified to update it. :D Description of the old function, which remains can still be found in the code: To ...


3

Jelly, 16 bytes Recursion might well end up being less bytes. ṚḌ;‘))Fṭ 1Ç¡ċ€ċ0 A monadic Link accepting a positive integer which yields a non-negative integer Try it online! Or try a faster, 17 byte version How? ṚḌ;‘))Fṭ - Helper Link: next(achievable lists) ) - for each (list so far): ) - for each (value, V, in that list): Ṛ - ...


3

APL (Dyalog Unicode), 5 bytes ⍳⊥⊢÷! Try it online! Using the mixed base trick found in my answer of another challenge. Uses ⎕IO←0. How it works ⍳⊥⊢÷! Right argument: n, the number of terms ⊢÷! v: 1÷(n-1)! ⍳ B: The array of 0 .. n-1 ⊥ Expand v to length-n array V, then mixed base conversion of V in base B Base | Digit | Value -------...


3

RUBY, 24 bytes 18 bytes n=0;10.times{puts n+=1} thanks to @A_ 10.times{|n|p n+1}


3

PHP, 137 136 143 bytes function f($n){return$n>99?$n>999?f(substr($n,0,-3)).(1e3).f(substr($n,-3)):$n[0].'100'.f($n[1].$n[2]):($n>20&&$n%10?(0^$n/10).'0'.$n[1]:+$n);} Try it online!


3

Charcoal, 34 32 31 29 bytes ⪫E↨Nφ⪫E↨ι¹⁰⁰⪫↨λχ…0›Πλ﹪λχ100Iφ Try it online! Link is to verbose version of code. Edit: Saved 2 bytes thanks to @Grimmy. Saved a further byte by finding a better way to choose between 0 or the empty string from a boolean. Saved a further 2 bytes because Base(l, 10) conveniently returns an empty array when l is zero. Explanation: ...


3

Python 3+inflect, 193 bytes import inflect,re exec("for i in re.sub('(and)|\W',' ',%sinput())).split():\n for j in range(0,6**8):\n if i in %sj):\n print(j,end='')\n break"%(('inflect.engine().number_to_words(',)*2)) Try it online! Very naive solution which uses Python's inflect library. Here is the unfurled code: import inflect,re for i in re.sub('(...


3

JavaScript, 69 bytes Recursively splits and joins based on place values stored in k. Inspired by xnor's Python answer. f=(n,k=1e3)=>n<20?+n||'':(n<k?'':f(n/k|0,k/10)+[k-10&&k])+f(n%k,k/10) Try it online!


3

Red, 74 bytes func[a][forall a[a/1:(sign? a/1)* do reverse to""absolute a/1]max a/1 a/2] Try it online! Takes the input as a list of two numbers. The 5th test case (0.0000001 0) doesn't work in TIO, but works fine in the Red console:


3

JavaScript (Node.js), 64 bytes a=>b=>(R=([h,...t])=>h?h+1<0?h+R(t):R(t)+h:t)(a)>+R(b)?R(a):R(b) Try it online! Input / output as strings.


3

05AB1E, 15 bytes ₆v;F.03*>NmyтP, Port of @TFeld's Python 2 answer, so make sure to upvote him. Try it online or verify all test cases. Explanation: ₆ # Push builtin integer 36 v # Loop `y` over both digits: ; # Halve the (implicit) input-integer F # Inner loop `N` in the range [0, input/2):...


3

Jelly, 16 bytes ×.03‘*HḶ$×300;Ḥ$ Try it online! -2 bytes thanks to Nick Kennedy Explanation ×.03‘*HḶ$×300;Ḥ$ Main Link ×.03 Multiply the number of bottles by 3% ‘ Increment (add 100%) * (Vectorized) exponent to the power of: HḶ$ [0, 1, ..., n-1] ×300 Multiply each factor by 300 ...


3

Julia 1.0, 42 38 bytes f(n,z=[75*(1+.03n).^(0:n÷2-1)])=4z,8z -4 bytes using ideas from John and Joseph Sible. Try it online!


3

Haskell, 47 bytes f n=[75*a*(1+0.03*n)**i|a<-[4,8],i<-[0..n/2-1]] Try it online!


3

Python 2, 55 bytes n=input()/2 for c in 4,8:exec"print c*75;c*=1+.06*n;"*n Try it online! The *75 trick is taken from other answers.


2

JavaScript, 95 93 92 bytes f=n=>n>999?f(n/1e3|0)+[1e3]+f(n%1e3):n>99?(n/100|0)+'100'+f(n%100):n>20&&(x=n%10)?n-x+''+x:n Try it online! -2 bytes (@Shaggy): store n%10 in a variable -1 byte (@Arnauld): replace '1000' with [1e3]


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