Hot answers tagged

31

Python, 24 bytes lambda l:sum(l)>max(l)*2 Try it online! Checks if a+b+c > max(a,b,c)*2. If, say, c is the biggest one, this is equivalent to a+b+c>2*c, or a+b>c, which is want we want for the triangle inequality.


26

CP-1610 machine code (Intellivision), 12 DECLEs1 = 15 bytes A routine taking \$(a,b,c)\$ into R0, R1 and R2 respectively and setting the carry if \$(a,b,c)\$ is not a triangle, or clearing it otherwise. 083 | MOVR R0, R3 0CB | ADDR R1, R3 15A | CMPR R3, R2 02F | ADCR R7 0D3 | ...


14

J, 22 bytes %&".e.=/#&,%/&(1".\.]) Try it online! quick explanation for now: take the input as strings %/&(1".\.]) creates a function table %/ whose axes are the integer ". lists formed by the 1-outfixes \. (remove 1 digit at a time) of both args, and whose cells are the quotients of those numbers =/ forms a corresponding function table ...


13

Python, 31 bytes lambda l:sum(n%2for n in l)|2<3 Try it online! We use k|2<3 to check that k is either 0 or 2. This works because the bit operation |2 sets the bit for place-value 2, so to get a result that's 2 or less, there must be no other bits set.


11

Python 3, score = big(?) from math import exp, log, log1p def f(b, n): e = n * (n - 1) / 2 m = 0 c = 1 s = 0 t = 1 << b for k in range(b): s += c m += exp(e * (log1p(-s / t) if 2 * s < t else log((t - s) / t))) c = c * (b - k) // (k + 1) return m Try it online! The Hamming distance \$D_{x, y}\$ ...


11

C (gcc), 46 \$\cdots\$ 43 42 bytes Saved 2 bytes thanks to 79037662!!! f(a,b,c){a=a^b?a^c?b^c?a*b*c:a:b:b^c?c:1;} Try it online!


11

Python 3, 86 bytes lambda a,b:g(a)&g(b) g=lambda s:{(int(s[:i]+s[i+1:])/int(s),x)for i,x in enumerate(s)} Try it online! -8 bytes thanks to ovs Making use of the fact that the boolean value for a0/b0==a/b is equivalent to a0/a==b0/b. The helper function g generates all ratios a0/a and keeps track of the removed digit. Then it does the same for b0/b. ...


10

R, 57 bytes function(v,`!`=sum,N=!1^v)c(A<-!v/N,P<-!v/2^c(N:1,N),P-A) Try it online! A clever collaboration with digEmAll and RobinRyder; uses the observation by Grimmy and Kevin's explanation from the comments, which I replicate below: For anyone else wondering why the example [a,b,c,d] -> [a,b,c,c,d,d,d,d] doesn't match the explanation of ...


10

05AB1E, 3 bytes ¢ÏP Try it online! ¢ # count occurences of each number Ï # keep only those where the count is 1 P # product (1 if the list is empty)


9

J, 21 19 18 bytes { ::0[:+//.@(*/)/> Try it online! -1 byte thanks to FrownyFrog J has a nice geometric idiom for multiplying polynomials, which we represent as lists of coefficients, with explicit zeroes where needed: +//.@(*/) Let's see how this works using the example: 1 3 5 +//.@(*/) 5 3 2 First it creates a multiplication table */: 5 3 2 ...


9

JavaScript (ES6), 38 bytes (a,b,c)=>a-b?b-c?c-a?a*b*c:b:a:b-c?c:1 Try it online! JavaScript (ES6), 53 bytes Takes input as [a,b,c]. A=>[[a,b,c]=A,1,a-b?a-c?a:b:c,a*b*c][new Set(A).size] Try it online! Commented A => // A[] = input [ // lookup table: [a, b, c] = A, // set size = 0 --> impossible, so ...


9

Java 8, 52 49 38 26 bytes (a,b,c)->a+b>c&a+c>b&b+c>a Port of @xnor's formula turns out to be shorter after all, by taking the input as three loose integers instead of a List/array. -12 bytes thanks to @xnor for reminding me that the first 6-bytes formula I used in my 05AB1E answer is actually shorter in Java: \$(a+b>c)\land(a+c>b)...


8

J, 17 bytes Translation of this. -:@+/@|.(,,-)+/%# Anonymous tacit prefix function. Returns list of Progressive Mean™, Standard Mean, Trend. Try it online! This consists of three parts: (2÷⍨+)⌿∘⌽ (,,-) +⌿÷≢ +/ % # is the sum divided by the number of elements …⌿@|. reverses the list and then reduces from right to left using the function:  -:@+ add next ...


8

Mathematica, 47 45 43 29 bytes SBCS If[NumberQ[c=#~Log~#2],c,-1]& You can try it online! Thanks to @J42161217 for saving me a total of 16 bytes!!


8

JavaScript (Node.js),  285 ... 248  247 - 5 = 242 bytes Fixed a bug and saved a couple of bytes by borrowing @Grimmy's method for the final count of the real roots Expects the coefficients from highest to lowest. This is an implementation of Sturm's theorem, and therefore works for any degree, which might be a bit overkill for this challenge. p=...


7

MATL, 18 15 bytes Y:iWB1G"Y+]2GQ) Input is a cell array of numerical vectors with the polynomial coefficients, followed by an integer (k). Try it online! Or verify all test cases. Explanation Polynomial multiplication is convolution of their coefficients. And convolution is the key to success Y: % Implicit input: cell array of numeric vectors. ...


7

Python 3.8 (pre-release), 70 69 bytes -1 byte thanks to Kyle Gullion returns (mean, p_mean, trend) lambda x:(m:=sum(x)/len(x),[p:=x[0],*[p:=(p+n)/2for n in x]][-1],p-m) Try it online! Alternate version with more of a reducer implementation (89 bytes): lambda x:(m:=sum(x)/(l:=len(x)),[x:=[sum(x[0:2])/2]+x[2:]for n in range(l-1)][-1],x[0]-m) Although ...


7

APL (Dyalog), 13 bytes -1 bytes thanks to ngn {⊃⍸⍵≤+\÷⍳⌈*⍵} Try it online! A dfn solution that takes a right argument. Explanation: { } ⍝ dfn ⊃ ⍝ Take the first of ⍸ ⍝ The indexes of the truthy values of ⍵≤ ⍝ The right argument is smaller than or equal to \+ ⍝ The cumulative sum ÷ ...


7

Python 3, 35 bytes f=lambda x,n=1:x>0and-~f(x-1/n,n+1) Try it online!


7

Ruby, 19 bytes ->*a{2*a.max<a.sum} You can try it online! Uses the fact that this Ruby answer said it didn't want to implement the port of xnor's answer and at the same time taught me enough syntax to guess how the max of an array is calculated :)


6

05AB1E, 13 bytes ÅAIÅ»+;}θ‚Âƪ Outputs as a triplet [mean, progressive-mean, trend]. Try it online or verify all test cases. Explanation: ÅA # Calculate the arithmetic mean of the (implicit) input-list I # Push the input-list again Å» # Left-reduce it by: + # Add the two values together ; # And halve it }θ # After the ...


6

Scratch 2.0/3.0, 35 blocks/263 bytes SB Syntax: when gf clicked set[i v]to(2 set[L v]to(length of[I v set[T v]to(item(1)of[I v set[P v]to(T repeat((L)-(1 change[T v]by(item(i)of[I v change[P v]by(item(i)of[I v set[P v]to((P)/(2 change[i v]by(1 end set[T v]to((T)/(L say(join(join(join(join(P)[,])(T))[,])((P)-(T It's accidentally a polyglot. I accidentally ...


6

JavaScript (ES6),  32  29 bytes Saved 3 bytes thanks to @Shaggy f=(n,k=0)=>n>0?f(n-1/++k,k):k Try it online! Non-recursive version, 35 bytes n=>eval("for(k=0;(n-=1/++k)>0;);k") Try it online! Approximation (25 bytes, not fully working) This gives the correct result for all but the first test case. n=>Math.exp(n-.5772)+.5|0 ...


6

Perl 6, 27 bytes {+([\+](1 X/1..*)...*>=$_)} Try it online! Explanation: { } # Anonymous code block taking one argument +( ) # Return the length of [\+]( ) # The cumulative sum 1 X/ # Of the reciprocal of all of 1..* # 1 to ...


6

J, 10 bytes */@-.}./.~ Try it online! }./.~ group by value and remove one from each group -. set subtract from input */ product


6

R, 40 37 bytes -3 bytes by taking input with scan(). prod(unique(x<-scan())^2,1/x)^!!sd(x) Try it online! sd(x) computes the standard deviation (which is 0 iff all entries are equal), so !!sd(x) is TRUE if some entries are different, and FALSE if all are equal. if all entries are different, then unique(x)==x, so we are down to prod(x)^1 if 2 entries ...


6

APL (Dyalog), 10 bytes Thanks to Adám and Bubbler in chat for helping golf this ×/∪*1=⊢∘≢⌸ Try it online! Explanation: ×/ ⍝ Reduce by multiplication ∪ ⍝ The unique values of the input * ⍝ To the power of 1= ⍝ Whether one is equal to ≢ ⍝ The length of ⊢∘ ⌸ ⍝ The indexes of each ...


6

05AB1E, 6 5 4 bytes O;‹ß -1 byte by porting @xnor's algorithm. -1 byte thanks to @Grimmy using a derived formula. Try it online or verify all test cases. Explanation: The formula this program uses to determine if three side-lengths \$a,b,c\$ form a triangle is: \$t=\frac{a+b+c}{2}\$ \$(a<t)\land(b<t)\land(c<t)\$ O # Take the sum of the (...


6

Ruby, 23 bytes ->*a{a.sort!.pop<a.sum} Try it online! A different approach, xnor's formula would be shorter but I'm satisfied with that.


6

PHP, 45 32 bytes fn($a)=>max($a)*2<array_sum($a); Try it online! Lambda function that takes an array [a, b, c] and outputs empty string if false or "1" if true, with xnor's formula. Note that in PHP the ; is only necessary when the function is attributed to a variable, so it's placed in the footer (provided code is valid as it is). EDIT: thanks to ...


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