Skip to main content
28 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
  • 22.1k
14 votes

Longest Consecutive Sequence

Jelly, 6 bytes f‘$ƬL’ Try it online! ...
ovs's user avatar
  • 59.5k
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
  • 6,015
13 votes

Is it a cartesian product?

Python, 76 bytes Thanks @att for -1. lambda L,S=sorted:S(K:=[a+b for a in L for b in L])==S((x*3)[::3]for x in K) Attempt This Online! Python, 77 bytes Thanks @...
Albert.Lang's user avatar
  • 2,884
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

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,800
11 votes

Weave two lists, cycling if necessary

R, 61 60 57 54 50 bytes Edit: -1 byte thanks to @Giuseppe, -3 bytes thanks to int 21h -- Glory to Ukraine -- and again -4 bytes thanks to @Giuseppe. ...
pajonk's user avatar
  • 16.7k
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.2k
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,515
10 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
  • 897
10 votes

Is it a cartesian product?

Jelly, 9 bytes ŒṬ€SẸƇÆrE Try it online! Builds a 2D matrix where each element represents how many times its coordinates appear in the original array, and then ...
Bubbler's user avatar
  • 77k
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

"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
  • 9,408
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,898
8 votes

Which skill to train?

APL(Dyalog Unicode), 14 bytes SBCS ⊃∘⍒(1+÷)*1+⍋∘⍋ Try it on APLgolf! ...
att's user avatar
  • 20.6k
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
  • 16.7k
8 votes

Search the deepest depths of an array

JCram, 13 bytes (SBCS) -1 thanks to @mukundan314 -1 thanks to @tsh A JCrammed version, as suggested by Kamila Szewczyk. νⓅιk⑦③⍋⍁lζ⁷⊗Ⓙ JavaScript (ES13), 60 bytes -...
Arnauld's user avatar
  • 193k
8 votes

Reconstruct a list from its prefixes

Jelly, 4 bytes §ṢŻI A monadic Link that accepts a list of lists of positive integers and yields a list of positive integers. Try it online! How? ...
Jonathan Allan's user avatar
8 votes

Weave two lists, cycling if necessary

Vyxal, 9 bytes @÷›∴vẎƒYṪ Try it Online! ...
emanresu A's user avatar
  • 38.8k
8 votes

Is it a cartesian product?

Wolfram Language (Mathematica), 25 bytes MatrixRank@BinCounts@#<2& Try it online! Form a matrix whose entries correspond to the number of times a pair is ...
att's user avatar
  • 20.6k
7 votes

Longest Consecutive Sequence

Python 3, 45 bytes f=lambda l:len(l)and-~f({*l}&{x-1for x in l}) Try it online! Without using sets: Python, 48 bytes ...
xnor's user avatar
  • 145k
7 votes

Longest Consecutive Sequence

Haskell, 32 bytes f[]=0 f l=1+f[n|n<-l,elem(n+1)l] Try it online! Repeatedly filters l by only keeping elements ...
xnor's user avatar
  • 145k
7 votes

Longest Consecutive Sequence

Vyxal, 35 bitsv2, 4.375 bytes ÞǔĠṠG Try it Online! How? Port of my Jelly answer. ...
Jonathan Allan's user avatar
7 votes

What's my score?

Python, 92 bytes -2 bytes, thanks to UndoneStudios -7 bytes, thanks to SuperStormer ...
bsoelch's user avatar
  • 6,015

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