Skip to main content
added 53 characters in body
Source Link
DLosc
  • 39.2k
  • 5
  • 83
  • 141

Brachylog, 13 9 bytes

-4 bytes thanks to a clever trickclever trick from Unrelated String

⊇.+0&s.∨Ė

Try it online! (Note that negative numbers in the input must be indicated with underscores rather than minus signs.)

Explanation

It would have been nice to use this 6-byte solution:

s.+0∨Ė
s.       A contiguous sublist of the input is the output
  +0     and its sum is zero
    ∨   Or, if it is impossible to satisfy those conditions...
     Ė   the output is the empty list

Unfortunately, the order in which the s predicate tries sublists is ordered by start index, not by length. However, the predicate, which gives not-necessarily-contiguous sublists, is ordered by length. So we can start by getting the longest sublist and then afterwards check that it's contiguous:

⊇.+0&s.∨Ė
⊇.          A sublist of the input is the output
  +0        and its sum is zero
    &       and furthermore
     s.     the output is a contiguous sublist of the input
       ∨   Or, if it is impossible to satisfy those conditions...
        Ė   the output is the empty list

Brachylog, 13 9 bytes

-4 bytes thanks to a clever trick from Unrelated String

⊇.+0&s.∨Ė

Try it online! (Note that negative numbers in the input must be indicated with underscores rather than minus signs.)

Explanation

It would have been nice to use this 6-byte solution:

s.+0∨Ė
s.       A contiguous sublist of the input is the output
  +0     and its sum is zero
    ∨   Or, if it is impossible to satisfy those conditions...
     Ė   the output is the empty list

Unfortunately, the order in which the s predicate tries sublists is ordered by start index, not by length. However, the predicate, which gives not-necessarily-contiguous sublists, is ordered by length. So we can start by getting the longest sublist and then afterwards check that it's contiguous:

⊇.+0&s.∨Ė
⊇.          A sublist of the input is the output
  +0        and its sum is zero
    &       and furthermore
     s.     the output is a contiguous sublist of the input
       ∨   Or, if it is impossible to satisfy those conditions...
        Ė   the output is the empty list

Brachylog, 13 9 bytes

-4 bytes thanks to a clever trick from Unrelated String

⊇.+0&s.∨Ė

Try it online! (Note that negative numbers in the input must be indicated with underscores rather than minus signs.)

Explanation

It would have been nice to use this 6-byte solution:

s.+0∨Ė
s.       A contiguous sublist of the input is the output
  +0     and its sum is zero
    ∨   Or, if it is impossible to satisfy those conditions...
     Ė   the output is the empty list

Unfortunately, the order in which the s predicate tries sublists is ordered by start index, not by length. However, the predicate, which gives not-necessarily-contiguous sublists, is ordered by length. So we can start by getting the longest sublist and then afterwards check that it's contiguous:

⊇.+0&s.∨Ė
⊇.          A sublist of the input is the output
  +0        and its sum is zero
    &       and furthermore
     s.     the output is a contiguous sublist of the input
       ∨   Or, if it is impossible to satisfy those conditions...
        Ė   the output is the empty list
Golfed 4 bytes
Source Link
DLosc
  • 39.2k
  • 5
  • 83
  • 141

Brachylog, 1313 9 bytes

-4 bytes thanks to a clever trick from Unrelated String

;0⟨s+⟩ᶠlᵒ{t|}⊇.+0&s.∨Ė

Try it online!Try it online! (Note that negative numbers in the input must be indicated with underscores rather than minus signs.)

Explanation

It would have been nice to use this 6-byte solution:

;0            Pair the input list with 0
  ⟨  ⟩         "Sandwich" construct:s.+0∨Ė
   s    .       A contiguous sublist of the input list is the output
    +     +0     and theits sum of the output is 0zero
        Or, if Findit allis possibleimpossible outputsto ofsatisfy thethose aboveconditions...
      lᵒĖ   the output is Sortthe byempty lengthlist

Unfortunately, the order in which the s predicate tries sublists is ordered by start index, not by length. However, the predicate, which gives not-necessarily-contiguous sublists, is ordered by length. So we can start by getting the longest sublist and then afterwards check that it's contiguous:

⊇.+0&s.∨Ė
⊇.        {t } A Takesublist of the lastinput one...
is the output
  +0      |  and ...its or,sum ifis thezero
 list of results was& empty, just return that list

It would have been nice to use this 6-byte solution:

s.+0∨Ė
s.  and furthermore
    A sublists. of the input is the output
  +0is a contiguous sublist of andthe itsinput
 sum is zero
orOr, if it is impossible to satisfy those conditions...
        Ė   the output is the empty list

But unfortunately, the order in which Brachylog tries sublists is ordered by start index, not by length. So I had to fall back to the generate-all-possibilities-and-sort-them strategy.

Brachylog, 13 bytes

;0⟨s+⟩ᶠlᵒ{t|}

Try it online! (Note that negative numbers in the input must be indicated with underscores rather than minus signs.)

Explanation

;0            Pair the input list with 0
  ⟨  ⟩         "Sandwich" construct:
   s           A contiguous sublist of the input list is the output
    +          and the sum of the output is 0
          Find all possible outputs of the above
      lᵒ      Sort by length
        {t }  Take the last one...
          |   ... or, if the list of results was empty, just return that list

It would have been nice to use this 6-byte solution:

s.+0∨Ė
s.       A sublist of the input is the output
  +0     and its sum is zero
or, if it is impossible to satisfy those conditions...
     Ė   the output is the empty list

But unfortunately, the order in which Brachylog tries sublists is ordered by start index, not by length. So I had to fall back to the generate-all-possibilities-and-sort-them strategy.

Brachylog, 13 9 bytes

-4 bytes thanks to a clever trick from Unrelated String

⊇.+0&s.∨Ė

Try it online! (Note that negative numbers in the input must be indicated with underscores rather than minus signs.)

Explanation

It would have been nice to use this 6-byte solution:

s.+0∨Ė
s.       A contiguous sublist of the input is the output
  +0     and its sum is zero
       Or, if it is impossible to satisfy those conditions...
     Ė   the output is the empty list

Unfortunately, the order in which the s predicate tries sublists is ordered by start index, not by length. However, the predicate, which gives not-necessarily-contiguous sublists, is ordered by length. So we can start by getting the longest sublist and then afterwards check that it's contiguous:

⊇.+0&s.∨Ė
⊇.          A sublist of the input is the output
  +0        and its sum is zero
    &       and furthermore
     s.     the output is a contiguous sublist of the input
Or, if it is impossible to satisfy those conditions...
        Ė   the output is the empty list
Source Link
DLosc
  • 39.2k
  • 5
  • 83
  • 141

Brachylog, 13 bytes

;0⟨s+⟩ᶠlᵒ{t|}

Try it online! (Note that negative numbers in the input must be indicated with underscores rather than minus signs.)

Explanation

;0            Pair the input list with 0
  ⟨  ⟩         "Sandwich" construct:
   s           A contiguous sublist of the input list is the output
    +          and the sum of the output is 0
     ᶠ        Find all possible outputs of the above
      lᵒ      Sort by length
        {t }  Take the last one...
          |   ... or, if the list of results was empty, just return that list

It would have been nice to use this 6-byte solution:

s.+0∨Ė
s.       A sublist of the input is the output
  +0     and its sum is zero
    ∨   or, if it is impossible to satisfy those conditions...
     Ė   the output is the empty list

But unfortunately, the order in which Brachylog tries sublists is ordered by start index, not by length. So I had to fall back to the generate-all-possibilities-and-sort-them strategy.