Hot answers tagged

27 votes

Is it a valid chemical?

Python 3, 61 60 59 58 51 bytes lambda x:len(x)-2<~sum(x)%2*all(x)*sum(x)/2>=max(x) Try it online! -1 thanks to @Kevin Cruijssen -7 thanks to @xnor Uses ...
Command Master's user avatar
26 votes

Reconstruct a list from its prefixes

Python 3, 51 bytes lambda l,p=0:[-p+(p:=x)for x in sorted(map(sum,l))] Try it online! Take the sums of the input lists, sort them in increasing order, take ...
xnor's user avatar
  • 145k
24 votes

Find the largest sum such that no two elements are touching

Ruby, 46 38 bytes ->l{a=b=0;l.map{a,b=b,[a+_1,b].max};b} Attempt This Online! Runs in O(n). How? We need 2 accumulators. The first 2 steps will initialize them ...
G B's user avatar
  • 21.6k
17 votes

Is it traversable?

JavaScript (ES6), 39 bytes Expects (stamina)(list). Returns false for truthy and true for falsy. ...
Arnauld's user avatar
  • 191k
14 votes

Longest Consecutive Sequence

Jelly, 6 bytes f‘$ƬL’ Try it online! ...
ovs's user avatar
  • 59k
13 votes

Adjacent Items Sorting

05AB1E, 14 13 11 12 bytes Wsvεèyδ‚yš}˜ Try it online! If it shouldn't output "0" for 0 it can be fixed with +1 byte by appending ...
Command Master's user avatar
13 votes

Longest Consecutive Sequence

Jelly, 5 bytes ṬŒg§Ṁ A monadic Link that accepts a list of positive integers and yields the length of the longest consecutive sequence. Try it online! How? ...
Jonathan Allan's user avatar
13 votes

Complement an infinite list

Python (no imports), 52 bytes -3 bytes, thanks to xnor def f(s,x=0): for y in s:yield from range(x+1,x:=y) Attempt This Online! input/output as generator
bsoelch's user avatar
  • 5,945
12 votes

Reconstruct an array by successive insertions

05AB1E, 11 bytes η@O<ø{.γн}¦ Try it online! Takes a list of numbers, 0 to 14, with o being 0, and ...
Command Master's user avatar
11 votes

How long to carry sort?

R, 41 40 35 bytes Edit: -1 byte thanks to @Giuseppe and -5 bytes thanks to @Dominic van Essen. \(x)max(order(y<-c(x,Inf))-seq(!y)) Attempt This Online! The idea ...
pajonk's user avatar
  • 15.8k
10 votes

Is it traversable?

Excel (ms365), 75, 64 bytes -11 bytes thanks to @Dominic =REDUCE(B1,A2:A5-A1:A4,LAMBDA(a,b,IF(b>a,-1,a-(-b>a)*(a>0))))>=0
JvdV's user avatar
  • 3,613
10 votes

The Jaccard Index

APL(Dyalog Unicode), 5 bytes SBCS ∩÷⍥≢∪ Try it on APLgolf! Input A f B
att's user avatar
  • 20.1k
10 votes

Diagonalize a vector

K (ngn/k), 7 bytes {x*=#x} My first K post. I tried real hard to make a Bubbler train work here but failed. Try it online!
south's user avatar
  • 1,670
10 votes

Find the largest sum such that no two elements are touching

Haskell, 38 35 bytes g(x:y:r)=max(g$y:r)$x+g r g x=sum x Attempt This Online! g x=sum x is a handy way to combine the two base ...
Laikoni's user avatar
  • 26.1k
10 votes

Compute how many are no larger than each item in an array

Python, \$O(n \frac{\log n}{\log \log n})\$ Uses the approach from the paper "Optimal Algorithms for List Indexing and Subset Rank". ...
Command Master's user avatar
10 votes

What and where is the max?

Julia 1.0, 7 bytes findmax Try it online! Outputs (value, CartesianIndex(row_index, col_index)) (1-indexed)
MarcMush's user avatar
  • 6,475
9 votes

Counting fading

Python 2 or 3, 69 bytes f=lambda c:c>[]and-~f([s for s in c if sum(1==abs(e-s)for e in c)>3]) A recursive function that accepts a list of positions on the ...
Jonathan Allan's user avatar
9 votes

Counting fading

MATLAB with Image Processing Toolbox, 45 29 bytes @(x)max(max(bwdist(~x,'ci'))) Anonymous function that inputs a matrix and outputs a number. Try it online! (with ...
Luis Mendo's user avatar
  • 104k
9 votes

Transpose a multidimensional array

Python 3, 51 bytes f=lambda a,b:b<2and[*a]or[f(x,b-1)for x in zip(*a)] Try it online! Takes as input the list and then the dimension. If we look at the list of ...
Command Master's user avatar
9 votes

Just another traffic jam!

Jelly, 3 bytes ŒgZ A full program that accepts a pair of the two strings as the first argument and prints the resulting single lane. Try it online! How? ...
Jonathan Allan's user avatar
9 votes

What and where is the max?

Python + Numpy, 48 bytes lambda x:(x.max(),divmod(x.argmax(),x.shape[1])) Attempt This Online!
Stef's user avatar
  • 879
8 votes

Counting fading

MATLAB with Image Processing toolbox, 59 66 bytes +7 bytes from @Luis Mendo I={@(~)0,@(a)1+G(imerode(a,strel('dis',1)))} G=@(a)I{2-~nnz(a)}(a) Writing recursive ...
97.100.97.109's user avatar
8 votes

Compute cumulative means efficiently

APL(Dyalog Unicode), 19 20 bytes SBCS Added 1 byte to fix handling of trailing integers. (⌈¯2-/⌊,⊢/)⊢⍤/+\÷⍳⍤≢ Try it on APLgolf! ...
ovs's user avatar
  • 59k
8 votes

Diagonalize a vector

Haskell, 39 bytes f[]=[];f(a:b)=(a:map(0*)b):map(0:)(f b) Recursively generates the matrix by appending a value at the top left corner of a smaller matrix.
Magma's user avatar
  • 251
8 votes

Find the largest sum such that no two elements are touching

R, 80 46 43 bytes Edit: hugely golfed with inspiration from G B's answer: upvote that! \(x)max(Reduce(\(a,i)c(max(a),a[1]+i),x,0)) Attempt This Online! Runs in ...
Dominic van Essen's user avatar
8 votes

Output the longest list

Python, 74 74 bytes f=lambda i,n,m=0:1<len(r:={x(n)for x in i if x(m)})and f(i,n,m+1)or sum(r) Attempt This Online! Previous version had a bug Potentially ...
AnttiP's user avatar
  • 7,848
8 votes

Which skill to train?

APL(Dyalog Unicode), 14 bytes SBCS ⊃∘⍒(1+÷)*1+⍋∘⍋ Try it on APLgolf! ...
att's user avatar
  • 20.1k
8 votes

"Perfect" an array

Vyxal 3, 16 bytes L:ɾẋΩᵛC₌}ƛ?ÞṅL}g Simple bruteforce, probably can be golfed more. Try it Online! ...
math scat's user avatar
  • 8,843
8 votes

Remove falsy rows and columns

R, 35 bytes \(x,`?`=\(d)apply(x,d,all))x[?1,?2] Attempt This Online! Alternative solution if the input contained NAs instead ...
pajonk's user avatar
  • 15.8k
7 votes

Guessing on straws

Wolfram Language (Mathematica), 22 bytes ConnectedGraphQ@*Graph Try it online!
ZaMoC's user avatar
  • 24.8k

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