Hot answers tagged

20

Python 2, 24 bytes Takes two lists \$ a \$ and \$ b \$, where \$ b \$ has the missing number. lambda a,b:sum(a)-sum(b) Try it online! Given that \$ a \$ and \$ b \$ differ by a single number, we can trivially obtain the answer by computing the difference of sums.


19

Python 3, 33 bytes lambda a:sum(sorted(a)[-2::-2])/2 Try it online! This is my first answer :D


19

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


18

Python 2, 37 bytes Outputs a flattened \$ n \times n \$ latin square. lambda n:((range(n)*-~n)[1:]*n)[::~n] Try it online!


18

><>, 4 3 bytes i#o Try it online! Lol, ><> ties with beats Jelly & 05AB1E. Terminates with an error. -1 thanks to @Manny Queen. How does it work? The instruction pointer (IP) is currently moving right i Take input as a character # Reflect - The IP starts moving left i Take input again o Output that # Reflect - The ...


17

Jelly, 5 bytes 4ṗ>2Ġ Try it online! Neil saved a byte. Thanks! 4ṗ n-th Cartesian power of [1,2,3,4] >2 Replace 1,2,3,4 with 0,0,1,1 Ġ Group indices by values For n=2, 4ṗ>2 generates a list of 16 pairs: 1: [0, 0] 2: [0, 0] 3: [0, 1] 4: [0, 1] 5: [0, 0] 6: [0, 0] 7: [0, 1] 8: [0, 1] 9: [1, 0] 10: [1, 0] 11: [1, 1] 12: [1, ...


17

Python 3, 47 bytes f=lambda x,*a:f(*a,x[1:],x[:-1])if sum(x)else x Try it online! It's simple breadth-first search, implemented recursively. The if...else is slightly bothering me; it feels like there is an improvement somewhere...


16

AWK -vn=, 9 a[$1]++<n This is so competitive with the golfing-language answers, I feel like I might have misunderstood the question - did I? The limit n is passed as a variable at the command line with -vn=<value>. The array is read from STDIN. Try it online!


16

Python 2, 23 bytes lambda l:map(l.count,l) Try it online!


15

convey, 47 bytes Returns 0 for neither, 1 for one-based, 2 for zero-based and 3 for unknown. {2+~`0 ?;\2:!2[ v^<]` ?;\?+2 v^< v*+} !>>>=?^ 2 Try it online! Run for 1 2 2 3 with index 2 and value 2: On the left we split the input lines with ;\ ^< into the three paths. The first one pushes two zeroes after length of path + index time steps ...


15

JavaScript, 106 101 bytes s=>[...s,'!',s].reverse().join``.match(`(.)((?!\\1).)[^!]{${s.indexOf` `-1}}((?!\\1|\\2).)(\\2|\\3)`) Try it online! (Returns inverted truthy/falsy.) -5 by using spread syntax. Focus on the boundaries of the tiling. Suppose the tiling is not checkered. Then there is an odd cycle in the adjacency graph. Draw the odd cycle by ...


15

Java, 27 bytes java.util.Collections::swap 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

Haskell, 22 bytes (.zip).(.).map.uncurry Try it online! Not particularly interesting, just a reimplementation of zipWith converted to pointfree form: \f x y -> map (uncurry f) (zip x y) \f x -> map (uncurry f) . zip x \f x -> (map (uncurry f) .) zip x \f -> (map (uncurry f) .) . zip \f -> (. zip) (map (uncurry f) .) \f -> (. zip) ((.) (...


14

Python 2, 40 bytes f=lambda s,*l:s[3:]and f(s[1:],l,*l)or l Try it online! Doesn't bother telling car from cdr. Instead, makes a big nested tuple where every path ends at the right value, assuming it has the right length and ends in a. For example, f("cdddar", 5) = ((((5,), 5), (5,), 5), ((5,), 5), (5,), 5) is the same as f("caaaar", 5)...


14

Python 3.8 (pre-release), 57 bytes f=lambda S,p=1:S>[]and f(S[1:])+sum((p:=p^s%2)for s in S) Try it online! Similar to brute-force method below but should have better complexity. Python 3, 57 bytes f=lambda S:S>[]and~sum(S)%2+f(S[1:])+f(S[:-1])-f(S[1:-1]) Try it online! This is a brute force method based on inclusion-exclusion. Also works on ...


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

JavaScript (V8), 48 bytes a=>f=b=>b[2]?b[[x,...r]=f(b.slice(1)),1]<f?x:r:a Try it online! -2 bytes thanks to @Arnauld!


13

Trivial Built-in Answers This is the post for all of the languages where this is a built-in Vyxal, 1 byte ≈ Try it Online! Jelly, 1 byte E Try it online! 05AB1E (legacy), 1 byte Ë Try it online! Wolfram Language (Mathematica), 5 bytes Equal Try it online! SameQ also works. Brachylog, 1 byte = Try it online! Husk, 1 byte E Try it online! Factor, 3 bytes ...


13

Python 3, 100 99 88 bytes lambda a:2in[len({i,l}^{k,j})for x,y in zip(a,a[1:])for i,j,k,l in zip(x,x[1:],y,y[1:])] Try it online! Return True if it isn't checkered tiling, False otherwise -1 byte by inversing True/False for return value -11 thanks to @xnor How it works: The pattern is True if any of the following sub-pattern appears in the pattern : AA ...


13

J, 7 bytes -:/:@/: Try it online! Ninja'd by hyper-neutrino :( Takes zero-based input. Applying Grade Up twice to a vector gives a "ranking" of each element, so that 0 is the smallest, 1 is the next smallest, ... up to n-1, breaking ties by giving a smaller number to the one appearing first. A vector is a permutation if and only if the ranking is ...


13

Risky, 7 bytes 7 bytes and a palindrome 1_??!0:0!??_1 Try it online! Explanation 1 sort _ ? input ? filter by ! factorial 0 0 : = 0 range ! length ? input ? find first number such that _ 1 1


13

Jelly, 8 bytes Ä2œPF_@\ Try it online! Based on the older approach below. Ä generates cumulative sums, and 2œPF (ew) throws away the second one. Then \ scanning by _@ flipped subtraction gives: [a, a+b+c-(a), a+b+c+d-(a+b+c-(a)), a+b+c+d+e-(a+b+c+d-(a+b+c-(a))), ...] = [a, b+c, a+d, b+c+e, ...] I feel like there has to be ...


13

Python 3 with numpy, 90 87 86 84 bytes from numpy import* lambda n:2*((4<<(a:=(y:=c_[1-n:n])-y.T)//4)+a%4-2&a-2*y)-1<<a<y%2 Takes \$\left\lceil \frac{n}{2}\right\rceil\$. -3 by removing x. -1 with a shorter expression for \$-x - y\$. -2 with a shorter combining method. How it works Broadcasting is used in several places to produce the ...


13

Vyxal, 3 bytes smy Try it Online! Look ma, no Unicode! s # Sort m # Mirror, appending the reverse y # Push every second item, and the rest.


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 ...


12

Brainfuck, 43 bytes ,>,[->,<],<[->->-<<]>[[-]>>++<<]>[[-]>+<]>. Arguments are taken in this order: value_at_index, index, array values. Outputs are: 0-unknown, 1-zero indexed, 2-one indexed, 3-neither. How it works , read value_at_index into first cell >, read index into second ...


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