Hot answers tagged

19

Python 3, 27 bytes lambda x:min(x,key=x.count) Try it online!


15

Brachylog, 2 bytes sp Try it online! Explanation Input variable = "Hello World!", Output variable = "d!rl" (?)s Take a substring of the Input variable p(.) It is a permutation of the Output variable


14

R, 24 bytes a=scan();a[a!=median(a)] Try it online!


11

Bash + coreutils + w3m, 51 45 42 bytes w3m oeis.org/b${1:1}.txt|sed "s/^$2 //p;d" Thanks to @EamonOlive for golfing off 3 bytes! Example run $ bash oeis.sh A172141 7 980 How it works w3m is a text-based web browser, which displays both HTML and plain text in readable format. Unlike curl, it follows redirects by default (this is required, since oeis.org/...


11

APL (Dyalog Unicode), 20 bytes Anonymous infix lambda taking a and b as left and right argument. Requires 0-based indexing. {0@((⍳⍴⍵)∘.+⍸⍵⍷⍺)⊢⍺} Try it online! {…} dfn; arguments are ⍺ and ⍵:  ⊢⍺ on a,  0@(…) put zeros at the following positions   ⍵⍷⍺ indicate (Boolean matrix) locations b begins (top left corner) in a   ⍸ indices where (list of 2-element ...


9

JavaScript (ES6), 488 341 bytes s=>[s.replace(/(.)\1+/g,'$1+'),...[...Array(60)].map((_,i)=>`(${(i+4).toString(2).slice(1)})+`),...[...Array(1536)].map((_,i)=>`${i>>10?(i>>8&1)+(i&2?'+':''):''}(${i&1}${i&4?i>>4&1:i&16?'+':''}${i&8?''+(i>>7&1)+(i&64?i>>5&1:i&32?'+':''):''})+${...


9

JavaScript (ES6),  32  27 bytes Saved 5 bytes thanks to @xnor a=>a.sort()[0]+a.pop()-a[1] Try it online! How? a.sort() sorts the input array in lexicographical order. We know for sure that this is going to put the unique element either at the first or the last position, but we don't know which one: [ x, ..., y, ..., x ].sort() -> [ y, x, ..., x ] or [...


9

x86-16 machine code, 14 bytes Binary: 00000000: 498d 7c01 f3a6 e305 4ea6 7401 4ec3 I.|.....N.t.N. Listing: 49 DEC CX ; only do length-1 compares 8D 7C 01 LEA DI, [SI+1] ; DI pointer to next value F3 A6 REPE CMPSB ; while( [SI++] == [DI++] ); E3 05 JCXZ DONE ; if end of array, result is ...


8

Python 2, 75 68 63 bytes 5 bytes thanks to Dennis. And I have outgolfed Dennis. Credits to Byeonggon Lee for the core of the algorithm. o=t=[] for i in input():o+=~i%2*(t+[i]);t=i%2*([i]+t) print o+t Ideone it! Old version: 75 bytes


8

C#, 1.052 × 1010 The idea is to do the hash function in reverse to get all possible un-hashed strings from the given number. Integer overflow of the 32-bit hash value is also emulated in reverse by incrementing the given hash value by 2^32 on each iteration. UPDATE: I've made several micro-optimisations that significantly improved the performance of my ...


8

Japt, 6 bytes Works with strings or integers. æøV kV Test it Explanation Implicit input of array U and string/integer V. æ Get the first element (subarray) in U that ... øV Contains V. kV Remove V and implicitly return the resulting single-element array.


8

Haskell, 33 bytes x!((a,b):c)|x==a=b|x==b=a|1<2=x!c Try it online! Defines a binary operator !, which takes as its left argument a value x of type τ and as its right argument a list of tuples (τ, τ). The definition pattern matches on the head (a,b) and tail c of the given list; if x==a then b is returned; if x==b then a is returned, and otherwise we go ...


8

Haskell, 32 bytes f(x:y)|[e]<-filter(/=x)y=e|1<3=x Try it online! Explanation The code, expanded with variables renamed to be more descriptive. f (first:rest) | [unique] <- filter (/=first) rest = unique | 1 < 3 = first (first:rest) is a pattern match on a list that destructures it into its first element (first) and the list without the ...


8

MATLAB/Octave with Image Processing Toolbox/Package, 78 bytes @(A,B)A.*~conv2(reshape(all(im2col(A,size(B))==B(:),1),size(A)-size(B)+1),B>0) Try it online! Explanation Consider the example A = [1 2 3 4 3 2 3 5 4] B = [3 2] im2col(A,size(B)) takes sliding blocks of A with the size of B and arranges them as columns: 1 4 2 3 3 2 4 ...


7

Scala One "useful" characteristic of Java's String.hashCode method is that if you have a string z with z.hashCode() == 0, then you also have that (z + x).hashCode() == x.hashCode() for any other string x. So even if we can find a relatively small number of strings with a given hashCode, we can just prefix it with a bunch of zero-hash strings, and ...


7

JavaScript (E6) 97.5 (39*5*0.5) (O(n), linear search, the array can be scrambled again and again) The function accepts (and ignores) a third parameter F=(a,x)=>a.map((e,i)=>e-x?0:j=i,j=-1)|j or F=(a,x)=>a.some((e,i)=>(j=i,e==x))?j:-1 or F=(a,x)=>a.every((e,i)=>(j=i,e-x))?-1:j Test in FireFox/FireBug console F([5, 2, 3, 4, -1, 6, 90, ...


7

JavaScript (ES6), 77 bytes (s,t)=>t&&[...t.slice(0,s.length)].sort()+''==[...s].sort()|f(s,t.slice(1)) Returns 1 or 0. Snippet f= (s,t)=>t&&[...t.slice(0,s.length)].sort()+''==[...s].sort()|f(s,t.slice(1)) console.log(f('d!rl','Hello World!')) //1 console.log(f('Pog','Programming Puzzles & Code Golf')) //0 ...


7

05AB1E, 10 8 bytes Saved 2 bytes thanks to Leo ʒæså}éR` Try it online! Explanation ʒ # filter list, keep only members for which the following is true så # input is in the æ # powerset of the current word } # end filter é # sort by length R # reverse ` # push separately (shortest on top) I would ...


7

APL (Dyalog Extended), 5 bytes ⍸1=¯⍸ Try it online! Regular Dyalog APL 18.0 has ⍸⍣¯1, but it doesn't work here because it requires the input array to be sorted, unlike Extended's ¯⍸ which allows unsorted input arrays. How it works ⍸1=¯⍸ ⍝ Input: a vector N of positive integers ⍝ (Example: 4 4 6 4) ¯⍸ ⍝ Whence; generate a vector V where V[i] is ...


7

05AB1E, 2 bytes ʒ¢ Try it online! Since 1 is the only truthy integer in 05AB1E, we can just filter (ʒ) on the ¢ount. There is also the Counter-Mode builtin, which returns the least frequent element in a list at the same bytecount: .m Try it online!


6

C, 3.2 × 1010 preimages #include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, char *argv[]) { char output[29] = { 0 }, preimages[64][8], zeros[512][8] = { "/*$+06(" }; unsigned i, j, k, l, t; t = atoi(argv[1]); for(i = 7; i --> 0; t /= 31) preimages[0][i] = zeros[0][i] + (t % 31); ...


6

APL, 21 20 bytes {∊⌽¨⍵⊂⍨e⍲¯1↓0,e←2|⍵} Try it || All test cases Explanation: 2|⍵ Select all the odd numbers e← Save that to e 0, Append a 0 ¯1↓ Delete the last element e⍲ NAND it with the original list of odd numbers ⍵⊂⍨ Partition the list: (even)(...


6

05AB1E, 3 bytes œåZ Try it online! -1 byte thanks to Emigna. Explanation: œåZ 2 inputs œ permutations of the first input å Is each of the in the second input? Z Take the maximum of the resulting boolean list


6

Python 2, 67 66 bytes Takes input as two strings, substring first. a=sorted lambda s,S:a(s)in[a(S[n:n+len(s)])for n in range(len(S))]


6

PHP, 111 bytes $y=array_map(str_split,preg_grep("#".chunk_split($_GET[1],1,".*")."#",$_GET[0]));sort($y);echo join($y[0]??[]); Try it online!


6

MATL, 30 29 bytes xtog!s2$S"1G!'.*'Yc!@gwXXn?@. Try it online! Explanation x % Implicitly take first input (string with three letters). Delete. % Gets copied into clipboard G, level 1 t % Implicitly take second input (cell array of strings defining the % words). Duplicate o % Convert to numeric array of code ...


6

Brachylog (v2), 10 bytes {{s\s\}ᵈ}ᶜ Try it online! I like how clear and straightforward this program is in Brachylog; unfortunately, it's not that short byte-wise because the metapredicate syntax takes up three bytes and has to be used twice in this program. Explanation {{s\s\}ᵈ}ᶜ s Contiguous subset of rows \s\ Contiguous subset of ...


6

Python 3, 243 239 bytes -4 bytes thanks to @JonathanFrech! def f(a,t,k=1): while g(a,t,[0]*len(a),[],k):k+=1 def g(a,t,c,p,k):n=len(a);k,i=k//n,k%n;k,j=k//n,k%n;exec(["c[i]=0","c[i]=a[i]","x=min(a[j]-c[j],c[i]);c[i]-=x;c[j]+=x"][k%3]);p+=k%3,i,j;return g(a,t,c,p,k//3)if k>2else{t}-{*c}or print(p) Try it online! Input: a list of bucket capacities a, ...


6

Jelly, 3 bytes ḟÆṃ Explanation: ḟ (probably) removes all elements that are not the most common element (returned by Æṃ). I don't know why isn't the result a single-element list (perhaps it's a feature I didn't know about?), but that makes this even better. Try it online!


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