Hot answers tagged

19

Python 2, 33 30 bytes -3 bytes thanks to @xnor lambda n,v:eval('[v,'*n+']'*n) Try it online!


15

BQN, 3 bytesSBCS Perfect challenge for BQN's occurence count builtin. +⟜⊒ Run online! The modifier ⟜ composes two functions. If there is a single argument (as is the case here), the right function is called on that argument, and then the left function is called with that result and the original argument: (f⟜g x) ≡ (x f g x) The builtin ⊒ takes a vector and ...


15

Python 3.8, 48 46 bytes 2-byte savings jointly contributed by Jonathan Allan and dingledooper lambda L,j=1:[n-(j:=j-1)-L.index(n)for n in L] Try it online! Explanation The original 48-byte solution is easier to explain: lambda L:[n+i-L.index(n)for i,n in enumerate(L)] Consider each number \$n\$ in the list together with its index \$i\$. Suppose that we ...


15

Brachylog, 3 bytes p↰ᵐ Try it online! Explanation This takes the first list as the input variable, and the second list as the output variable. The interpreter then prints true. if it is possible to satisfy this predicate with these variables, and false. otherwise. p The output is a permutation of the input ↰ᵐ Recursively call this predicate on each ...


14

Python 3, 47 bytes lambda l:(sorted({*l},key=l.count)*2)[-len(l):] Try it online!


14

Python, 29 bytes lambda l:[x+max(l)for x in l] Try it online! Just adds the list maximum to each element, producing values greater than any in the original list. The max of the resulting list is double the original max, so we can invert by lowering each element by half the new max. decoder=lambda r:[x-max(r)//2 for x in r] You can encode with sum in place ...


13

Python 2, 57 bytes lambda l:[x for c in 1,2,1for x in set(l)if l.count(x)-c] Try it online! Iterates through the distinct elements of l 3 times, first outputting those that appear twice in l, then those that appear once, then twice again. -1 byte by att Python 3.8, 47 bytes def f(l):*map(l.remove,s:={*l}),;l+=[*s-{*l}]+l Try it online! -3 bytes by pxeger ...


11

Python 3, 49 bytes lambda x,y:g(x)==g(y) g=lambda a:sorted(map(g,a)) Try it online!


10

05AB1E, 3 bytes F‚Ù Try it online. Explanation: F # Loop the first input amount of times: ‚ # Pair the (implicit) second input-value with the current list Ù # And uniquify it (which only does something in the first iteration, # transforming the pair of values to a single wrapped value) # (after the loop, the result is output ...


10

J, 10 bytes /:~:*1#.e. Try it online! I had this first, hoping J might tie caird's Jelly, but I couldn't find any actual 9-byter. How it works /:~:*1#.e. Monadic train, input: X, a numeric vector 1#.e. Count occurrences of each number of X in X ~:* Multiply with nub sieve (1 if a number appears first, 0 otherwise) First ...


10

Python 2, 40 bytes f=lambda a:a>[]and max(len(a),*map(f,a)) Attempt This Online! Uses Python 2's global ordering: lists are all considered greater than ints. We can also use > instead of >=, because []>[] is False which is the same as 0, which would be the result anyway from the max(len(a),*map(f,a)). This also uses the var-args version of max ...


9

J, 9 bytes +i.@#-i.~ Try it online! Basically a port of DLosc's python answer, which fits naturally in the array paradigm. how Consider 1, 1, 1, 1, 10, 10, 20, 20, 20, 30, 40, 40, 40, 40 i.~ Index of each element's first appearance: 0 0 0 0 4 4 6 6 6 9 10 10 10 10 i.@#- Subtract that from 0 1 2 ... n: 0 1 2 3 0 1 0 1 2 0 0 1 2 3 + Add that to the ...


9

Jelly, 2 bytes ÆẸ Decoder: ÆE Try it online! A built-in. ÆE: Compute the array of exponents of z's prime factorization. Includes zero exponents. ÆẸ: Inverse of ÆE. In other words, ÆẸ maps \$[a,b,c,d,\dots]\$ to \$2^a 3^b 5^c 7^d \cdots\$. This trick is also used in the Gödel numbering.


9

Python, 57 54 53 bytes f=lambda x,a=0:[i*0==0and(a:=a+i)or f(i,a)for i in x] Attempt This Online! Thanks to @ovs for -3 bytes. Whython, 42 bytes f=lambda x,a=0:[(a:=a+i)?f(i,a)for i in x] Attempt This Online! Uses the ? operator to catch TypeError: unsupported operand type(s) for +: 'int' and 'list' and recurse in that case.


8

Python, 34 bytes f=lambda x,n:n and[x,f(x,n-1)][:n] Attempt This Online! Thanks to loopy walt for this one. Old answers: Python, 36 bytes f=lambda x,n:[x][n>1:]or[x,f(x,n-1)] Attempt This Online! Python, 36 35 bytes f=lambda x,n:n*[1]and[[x]+f(x,n-1)] Attempt This Online! Outputs as a singleton list, but that feels like cheating here. Python, 39 bytes f=...


8

Vyxal r, 4 bytes (⁰"U Try it Online! The joys of golfing language :) Explained (⁰"U ( # n times: ⁰ # push the value "U # pair and uniquify - a port of the 05ab1e answer


8

Python, 40 bytes f=lambda a,n=1:a and[a[:n]]+f(a[n:],n+1) Attempt This Online!


8

Wolfram Language (Mathematica), 35 bytes ReverseSort@Gather@#~Flatten~{2,1}& Try it online! # {1,1,2,3,3} Gather@ {{1,1},{2},{3,3}} ReverseSort@ {{3,3},{1,1},{2}} ~Flatten~{2 } {{3,1,2},{3,1}} ,1 ...


7

Ruby, 85 80 bytes f=->l{a,*l,b=l;q=l.size;a==0?b==q:(1..q).any?{|x|f[l[0,x]]&&f[[a-1,*l[x,q],b]]}} Try it online! Let me explain: The license is valid if: the first element is zero and the last element is the length of the list minus 2 or we can create 2 valid licenses by splitting the list in 2, removing the head from the first part and ...


7

Proton, 25 bytes n=>v=>((a=>[v,a])*n)([v]) Try it online! n=>v=>((a=>[v,a])*n)([v]) This language is stupid n=> Given n v=> and v (a=>[v,a]) pair v with the current accumulator *n n times ( )([v]) and call that on [...


7

R, 65 59 50 41 36 bytes function(x,`!`=order)x[!rev(x)][!!x] Try it online! How? First, a couple of tricks used: Re-assign the ! operator to become the order function, Notice that rank(x) is equivalent to order(order(x)), with tied ranks broken by first occurrence. So, ungolfed: twin_complement= function(x){ o=order(rev(x)) # get the order of ...


7

Jelly, 8 bytes ĠUFỤịỤịU Try it online! Port of Jonah's J answer: Ġ Group indices U Reverse each group F Flatten Ụ Grade up ịỤ Index into graded-up original list ịU Index into reversed list e.g. input: “XYXZYY” (needn't be integers!) Ġ: [[1,3],[2,5,6],[4]] U: [[3,1],[6,5,2],[4]] ...


7

Minipyth, 30 bytes (17.7 bytes if encoded) mhoithzsmqmscbnqgihcmmzsbbnlis Minipyth is a new minimalist language I've designed. It only uses lowercase letters, and it doesn't have things like variables, arity 2 functions, etc. Because Minipyth only uses lowercase letters, it could be re-encoded to be much shorter. I'm therefore also listing its information-...


7

Jelly, 7 bytes ẹⱮ_"JỤị Try it online! How it works ẹⱮ_"JỤị Monadic link. Input: X, a numeric vector ẹⱮ For each element n of X, get all indices of n in X _"J For each list of indices, subtract the original index If n appears twice, the first becomes [0, positive], and the second becomes [negative, 0]. ...


7

Factor, 94 62 bytes [ [ ] collect-by values [ length ] inv-sort-with round-robin ] Have a screenshot of running this in Factor's REPL because collect-by postdates the build on TIO. Explanation I finally found a use for round-robin! It's sort of like if flip (matrix transposition) worked for non-rectangular matrices and it also flattens the results. ...


7

MATL, 7 bytes t&=XRs+ Try it online! Or verify all test cases How it works Consider input [1, 1, 10, 10, 100, 100, 100, 100] as an example. t % Implicit input. Duplicate % STACK: [1 1 10 10 100 100 100 100], [1 1 10 10 100 100 100 100] &= % Matrix of equality comparisons % STACK: [1 1 10 10 100 100 100 100], [1 1 0 0 0 0 0 0 ...


7

Python 2, 68 bytes lambda L:eval(re.sub("[^\d]{3,}","],[",`[[0,L,0]]`))[1:-1] import re Textually replaces every stretch of more than 2 non digit characters (i.e. everything that is not ", ") with "],[". A little trickery at the ends is required to guarantee balanced brackets. Attempt This Online! Old Python 2, 73 ...


7

R, 76 75 bytes Or R>=4.1, 68 bytes by replacing the word function with a \. Edit: -1 byte thanks to @Dominic van Essen. function(x){y[]=cumsum(c(T,y<-sapply(x,is.list))|y);Map(unlist,split(x,y))} Try it online! Explanation (outdated a bit) function(x, # take x as input s=sapply(x,is.list) # which elements of x are lists themselves ) Map(...


7

JavaScript (ES6), 55 bytes f=a=>a.some(n=>++n.length!=a[0].length)?1:f(a.flat())+1 A quite simple approach: check if all elements are the same length, increment a counter if they are and repeat with the list flattened by one level, then return the counter. -3 bytes thanks to tsh. f=a=>a.some(n=>++n.length!=a[0].length)?1:f(a.flat())+1 ...


6

Wolfram Mathematica, 30 21 bytes Print[""@@#~Nest~##]& -9 bytes from @att! Try it online! Sample I/O: Print[""@@#~Nest~##]&@@{a,3} [a[a[a]]] Print[""@@#~Nest~##]&@@{17,3} [17[17[17]]] Print[""@@#~Nest~##]&@@{Pi,6} \$[\pi [\pi [\pi [\pi [\pi [\pi ]]]]]]\$


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