We’re rewarding the question askers & reputations are being recalculated! Read more.

New answers tagged

2

Charcoal, 66 bytes FLθFLθFLθF¬⌈⟦⁼ικ⁼ιλ⁼κ뛧θι§θꛧθκ§θλ⟧«≔E⟦ικλ⟧§θνη¿∧№η⊘Ση¬№υη⊞υη»Iυ Try it online! Link is to verbose version of code. Explanation: FLθFLθFLθF¬⌈⟦ Loop through all triples of indices... ⁼ικ⁼ιλ⁼κλ ... checking whether the indices are distinct... ›§θι§θꛧθκ§θλ ... and that the triplet is in ascending order... ⟧«≔E⟦ικλ⟧§θνη ... if so ...


4

Python 3, 94, 89, 88, 85 bytes One byte saved thanks to frank. lambda l:{p for p in combinations(sorted(l),3)if sum(p)/2in p} from itertools import* Try it online!


1

Pyth, 13 bytes {f}csT2T.cSQ3 Try it online! Port of the solution thats been floating around. How it works {f}csT2T.cSQ3 .c 3 - All combinations length 3 of... SQ - The sorted input f - Filtered such that... csT2 - Half the sum of each combination... } T - Is an element of the original combination { ...


5

Japt, 13 11 bytes á3 k_r-ÃmÍâ Try it á3 k_r-ÃmÍâ // input as arrays á3 // all unique permutations k_ // save list that.. r-Ã // reduced by - from first element mÍ // ==mn => sort all â // remove duplicates Thanks to @Shaggy and @Embodiment of Ignorance for saving 2


2

K (oK), 38 bytes {?{x@<x}'({z=x+y}.)#x@/:{x~?x}#+!3##x} Try it online!


1

Java 10, 225 bytes import java.util.*;L->{var r=new TreeSet();for(int j,k,a,b,t[],i=L.size();i-->0;)for(j=i;j-->0;)if((k=L.indexOf((a=L.get(i))+(b=L.get(j))))>=0&k!=i&k!=j){Arrays.sort(t=new int[]{a,b,a+b});r.add(Arrays.toString(t));}return r;} Port of @Arnauld's JavaScript answer, just twice as long.. >.> Try it online. Explanation: ...


7

05AB1E, 11 10 9 10 bytes {æ3ùêʒxsOå +1 byte as bugfix for test cases like [1,0,-1] → [[-1,0,1]] (-1+1=0) and [-1,-2,-3] → [[-3,-2,-1]] (-1+-2=-3). Try it online or verify all test cases. Explanation: { # Sort the (implicit) input-list from lowest to highest value æ # Take the powerset of this sorted list 3ù # Only keep inner ...


3

JavaScript (Node.js),  118 116  115 bytes Returns a set. a=>new Set(a.flatMap((x,i)=>a.flatMap((y,j)=>j-i&&~(k=a.indexOf(x+y))&&k-i&&k-j?[x,y,x+y].sort((a,b)=>a-b)+'':[]))) Try it online! Commented a => // a[] = input array new Set( // create a set: ...


8

Ruby, 46 51 58 bytes ->l{l.permutation(3).select{|a,b,c|a+b==c}.map(&:sort)|[]} Try it online! Ok, not so straightforward, but now seems to work.


6

Jelly, 10 bytes Ṣœc3ḤiSƊƇQ Try it online! Ṣ Sort the input, œc3 find all length-3 subsequences, Ƈ filter to only the sets for which S the sum of all three elements i is an element of Ḥ Ɗ the set with all elements doubled, Q and uniquify.


4

Brachylog, 11 bytes {o⊇Ṫ.+/₂∈}ᵘ Try it online! Takes input through the input variable and outputs through the output variable. { }ᵘ Output every unique output from: . the output is ⊇ a subsequence Ṫ of length 3 { of the input o sorted + the sum of which /₂ ...


4

Perl 6, 65 64 bytes *.combinations(3)>>.sort.grep({@$_.sum==2*.any}).unique(:as(~*)) Try it online! *.combinations(3) # All the combinations of length 3 >>.sort # Sorted .grep({ }) # Filtered by {@$_.sum== # The sum is equal to ...


2

PowerShell, 56 55 54 47 bytes inspired by Arnauld's one accumulator code. !($args+1|?{$_;$t+=$_-1}|?{$t-ge$_;$t*=$t-ge0}) Try it online!


1

J, 28 bytes [:*/1>1&,*([0&>.<:@+)/\.@,&0 Try it online! Inspired by Arnauld's algorithm. 1 if a solution exists, 0 otherwise. How it works In order to retain intermediate values of t, I chose to use "Reduce on suffixes" /\. with the initial value appended. Given a function f, the result of f/\. looks like this: Input array: 0 0 2 2 ...


1

Jelly, 20 bytes ŒṖT€ẈỊẠƲƇðẈẋ`€F⁹a⁼ðƇ A monadic Link accepting a list of non-negative integers which yields a list. An empty list is falsey, a non-empty list is truthy. (The list is actually all ways which work). Try it online! Or see a test-suite. How? ŒṖT€ẈỊẠƲƇðẈẋ`€F⁹a⁼ðƇ - Link: list, S ŒṖ - all partitions (of S) Ƈ ...


1

J, 47 bytes [:*/2(>+.0>:[)/\[:(++(0>.0-+)*0<+-])/@|.\<:@,&1 Try it online! Credit to Arnauld for the high-level idea. I'll add explanation and golf more soon.


2

Python 2 via exit code, 52 bytes n=0 for x in input()+[1]:x*n<0>_;n+=x<1or-max(x-1,n) Try it online! Successful completion for True, error for False. -1 byte thanks to Arnauld.


1

Retina 0.8.2, 34 bytes ^ , \d+ $* \b ; +1`,;1|1;, ; ^\W*$ Try it online! Explanation: ^ , Prepend a , so that there's one , for each square. \d+ $* Convert the entries to unary. \b ; Surround each non-empty square with ;s. +1`,;1|1;, ; Each ; in turn starts eating away at the adjacent empty squares. ^\W*$ Check that there were no 1s left. After ...


2

Python 3, 96 87 85 bytes def f(a): c=p=0 for x in a: if p*x:return p|=x;c+=1 if c>=p>0:c=p=0 return p Try it online! -5 bytes thanks to 79037662. -6 bytes thanks to HyperNeutrino. None and positive are falsy, 0 is truthy.


5

JavaScript (ES6),  56  52 bytes Returns a Boolean value. a=>![...a,1].some(x=>t>0/x?1:(t+=x-1)*x<0?t=0:0,t=0) Try it online! Commented The accumulator \$t\$ is decremented whenever an empty slot is encountered. For each non-zero value \$x\$, we update \$t\$ to \$max(t+x-1,0)\$. The test fails if we reach a non-empty slot (or the ...


0

J, 103 chars Notice the result from the last test case is different from wiki and other languages. This is due to this pointer to 254-th zero byte at the boundary. Things get a lot easier, if this isn't treated as a special case. f =: 3 : 0 k =. I. (y,0)=0 s =. - 2 (-/) \ k (>: y i. 0), s (}:k) } y ) f2 =: 3 : 0 f each _254 <\ y ) Try ...


1

Clojure, 40 bytes (fn[a b](* 1e2(/(nth a b)(apply + a)))) Try it online! It's nice to know about the 1e2 trick. :-)


2

Pyth, 13 bytes c*100@hQeQshQ Try it online! First time using Pyth so theres probably some pretty big optimizations here, but I dont know where they are... 0-index, takes input as list, index


0

Perl 5, 92 bytes sub f{min map{$o=$_;map{$s="@_";$c=!/$o/*$s=~s,$o,$_,g;$s=~/(.) (.) \1/&&$1-$2?9e9:$c}@_}@_} Try it online! sub f{ #ungolfed: min #minimum of the yielded list of numbers (from core module List::Util) map{ #map: outer loop of f()'s input args $o=$_; ...


2

Jelly, (19?) 39 bytes The entire second line is dealing with the strict I/O spec currently in place, while the 19 byte first line is a dyadic Link performing the core of the challenge. Also if 0 were allowed where -1 is currently required save the o- for -2 bytes. r/p/ċɗ€ⱮT€Ḣ€ŒQa"$o- ỴḲ€VḢḢ‘œṖƲŒH€€1¦ç/K Try it online!


0

Pyth, 1 byte y Since Pyth has implicit Q (input variable) at the end of programs, this is basically just power set of the input. I don't think this violates any rules (although 'No libraries besides io' is a bit vague) Try it online!


1

Clojure, 170 Bytes (fn [a](reduce #(mapv +(concat %1(repeat 0))%2)(mapv (fn [[x y]](concat(repeat x 0)(range 1(+ 1 y))))(reduce #(conj %1(mapv +[(nth(last %1)0)0]%2))[(first a)](rest a))))) Try it online!


2

J, 20 bytes +/@(0>.>:@i.@+-])+/\ Try it online! I guess this is quite different from both Galen Ivanov's and Jonah's answers. A dyadic train that takes ns as left argument and ms as right. One trick was to avoid (...)"0 (apply to each item) in favor of ...@+. The conjunction u@v has the effect of u@:v"v. The rank-forcing effect is usually not ...


1

K (ngn/k), 30 bytes {+/0^(1+!'y)@'-n-\:!*|y+n:+\x} Try it online!


1

C, 204 196 195 192 185 bytes *c(m,n,q,x,p,z)int*m,*n;{int i,j,*a=malloc(4);for(i=p=z=0;m[i]+1;i++){q=n[i]+(p+=m[i]);a=realloc(a,8*((x=z)>q?z:(z=q)));bzero(a+x,(z-x)*8);for(j=p;j<q;j++)a[j]+=j-p+1;}a[z]--;return a;} De-golfed version: * count (m, n, size_of_array, pointer_into_array, previous_size_of_array, final_element_counted_to) //all but m and n ...


4

Jelly,  10  9 bytes Ä0ẋżR}F€S A dyadic Link accepting a list of integers on each side, \$M\$ on the left \$N\$ on the right, which yields a list of integers. Try it online! Or see the test-suite. How? Ä0ẋżR}F€S - Link: list of integers, M; list of integers, N Ä - cumulative sums (M) = [m1, m1+m2, m1+m2+m3, ...] 0ẋ - zero ...


2

Ruby, 92 88 84 bytes ->x{n,l=0,[];x.map{|a,b|0.upto(b-1+n+=a){|i|l[i]||=0};1.upto(b){|i|l[-i]+=1+b-i}};l} Input is a list of pairs, in the form [[m1, n1], [m2, m2], ...]. The approach is pretty much the algorithm as described. Golfy tricks I've used include: l[i]||=0 setting the value for L to 0 if it isn't already set That's pretty much it. Thanks ...


4

Java 8, 148 147 bytes N->M->{int l=M.length,s=M[l-1],p=0,L[],i=0,j;for(int n:N)s+=n;for(L=new int[s];i<l;i++)for(p+=N[i],j=s;j-->0;)L[j]-=j<p|j>=p+M[i]?0:~j+p;return L;} -1 byte thanks to @ceilingcat. Takes both integer-arrays as separated inputs. Try it online. Explanation: N->M->{ // Method with integer-array as two ...


4

J, 27 bytes [:+/[(>:@i.@[,~0$~])"0+/\@] Try it online! Takes two separate lists for m an n - the list for n is the left argument of the function, the list for m - the right one. K (ngn/k), 42 bytes {+/{x,'((|/#'x)-#'x)#'0}((+\x)#'0),'1+!'y} Try it online! Takes two separate lists for m an n It's too long currently, I'll try to golf it.


2

05AB1E, 13 bytes ηOÅ0s€L‚ø€˜0ζO Takes two separated lists. The list of positions \$(m_0, m_1, m_2, ...)\$ as first input and list of values \$(n_0, n_1, n_2, ...)\$ as second input. Try it online or verify all test cases. Explanation: η # Get all prefixes of the (implicit) input-list of positions # i.e. [0,1,5] → [[0],[0,1],[0,1,5]] O ...


3

APL (Dyalog Unicode), 19 bytes 1⊥0⌈(↑(⍳+)¨-⊢)∘(+\) Try it online! Uses the algorithm described in my J answer. How it works 1⊥0⌈(↑(⍳+)¨-⊢)∘(+\) left = n's, right = m's ∘(+\) m2 = cumulative sum of m's (⍳+)¨ Nested array of 1..(each element of n+m2) -⊢ Subtract each elem of m2 from each row (↑ ) ...


0

Java, 642 Bytes This I think is kind of awful, but doing it with Java who knows what I was expecting. If someone could help me make this better I would really appreciate that, a bunch of bytes are taken up dealing with the fact that combiner can get two different length lists and need to add them, and also with the fact that there is a type needed for the ...


2

PHP, 93 bytes for(;$n=$argv[++$i+1];)for($p+=$argv[$i++];$$i<$p+$n;)$l[$$i]+=$$i++<$p?0:$$i-$p;print_r($l); Try it online! Input is a flat list of m0,n0,m1,n1,... passed by command arguments ($argv) and output is string representation of L.


1

Bash, 77 bytes for N;{ b=($N) for((i=0,p+=b;i<p+b[1];a[i]+=++i>p?i-p:0)){ :;} } echo ${a[@]} Try it online! I couldn't port my first Zsh answer directly, since ${a[@]/#%/0} doesn't work in Bash. So instead of fixing the empty elements at the end, I set all the elements with a[i]+=0 along the way. In the end, this strategy works out better for Zsh ...


2

Wolfram Language (Mathematica), 62 60 56 53 50 bytes Plus@@PadRight[a=0;Ramp[Range[(a+=#)+#2]-a]&@@@#]& Try it online! -3 with guarantee that input is non-empty. Takes a list of pairs as the argument.


3

Zsh, 59 56 54 bytes -3 bytes by changing to my Bash strategy, -2 bytes by switching back to my original strategy, now that the rules specify the list is non-empty. for m n;for ((i=0,p+=m;i<n;a[p+i]+=++i)): <<<${a/#%/0} Try it online: [Original strat handling empty case] [Adapted Bash strat] [Current] Setting a[5]=1 causes a[1] through a[4] to ...


4

Jelly, 12 11 bytes +ɼ0x;R}ʋ/€S Try it online! A full program that takes a list of lists of integers as its argument and returns a list of integers. Can be adapted to work as a link by resetting the register to zero after each call (as implemented in the footer on TIO). Saved a byte now list can be non-empty.


3

Haskell, 90 87 bytes (foldl(%)[].).zipWith(\o p->(0<$[1..o])++[1..p]).scanl1(+) (a:b)%(c:d)=a+c:b%d b%d=b++d Takes the input as two separate lists [m0,m1,...] and [n0,n1,...]. Try it online! A variant (function % same as above), also 87 bytes: (foldl(\l(o,p)->((0<$[1..o])++[1..p])%l)[].).zip.scanl1(+)


2

Python 3, 144 119 118 111 110 106 bytes def f(m,n): p=k=0;l=[] for i,j in zip(n,m):p+=j;l+=[0]*(p+i-len(l));exec("l[~k]+=i-k;k+=1;"*i) return l Try it online! Thanks to: -@mypetlion for saving me 25 bytes


1

K (ngn/k), 10 bytes {x=\:x:&x} Try it online! -19 thanks to ngn ... keeping my submission below haha K (ngn/k), 29 bytes {,/d#',:'-':+\[d:x,0]>\:!+/x} Try it online! edit: breaks for 1-element input case, needs work edit1: now fixed. +4 bytes. boo


0

Jelly, 7 bytes ĖŒṙ⁼þ`G Try it online! Same approach as the J answer. G Grid format ⁼þ a table of whether or not pairs of elements are equal, from Œṙ the run-length decoded Ė enumeration of the input, ` compared with itself.


0

Forth (gforth), 61 bytes : f 8 * over + f@ 0e swap 0 do dup f@ f+ 8 + loop f/ 1e2 f* ; Try it online! It's way too painful to work with arrays in Forth... A function that takes three items (array length, array start address, index; rightmost being the top) from the main stack, and gives the answer on the FP stack. The array contains floating-point values....


Top 50 recent answers are included