1
\$\begingroup\$

Given a list of integers, such as {1, 4, 2, 8, 10}, TI-Basic's ΔList will determine the difference between every overlapping pair of items. In the given example, it would be {3, -2, 6, 2}.

Task:

Take a list of integers as input, in any reasonable way. All overlapping pairs {x, y} should become y - x, and the resulting list returned. It should always be one item shorter. You can assume the list will never be shorter than two items.

Test cases:

{1, 2}                  {1}
{1, 2, 4}               {1, 2}
{2, 1}                  {-1}
{2, 2, 2}               {0, 0}
{-1, 0, 0, -1}          {1, 0, -1}
{0, 1, 2, 1, 1, 0}      {1, 1, -1, 0, -1}

Other:

This is . Shortest answer (in bytes) per language wins!

\$\endgroup\$
6
  • 1
    \$\begingroup\$ Is the {2, 2, 2} -> {0, 0, 0} testcase correct? Shouldn't the output be {0, 0}? \$\endgroup\$ – Delfad0r Apr 22 at 15:17
  • \$\begingroup\$ @Delfad0r Fixed \$\endgroup\$ – Redwolf Programs Apr 22 at 15:18
  • 1
    \$\begingroup\$ sub-problem of this challenge. \$\endgroup\$ – Giuseppe Apr 22 at 15:21
  • \$\begingroup\$ Instead of overlapping pairs {x, y} becoming {y - x}, can they become {x - y}? e.g. {1, 4, 2, 8, 10} -> {-3, 2, -6, -2} \$\endgroup\$ – Aaron Miller Apr 22 at 15:44
  • 5
    \$\begingroup\$ I've closed this as a duplicate of Different Way Forward, as this is essentially the same task, but to only do one step \$\endgroup\$ – caird coinheringaahing Apr 22 at 15:53

11 Answers 11

6
\$\begingroup\$

V (vim), 33 26 25 bytes

qqjYkPJr-0C<c-r>=<c-r>"
<Esc>j@qq@qdd

Try it online!

Input as a newline separated list of integers.

-7 bytes from kops.

-1 more byte from kops.

Explanation

qqjYpkk:s/\n/-
qq             start macro q
  jYp          go down and duplicate the second line from the cursor
     kk        move back to the original position
       :s/\n/- replace newline with -
               Current Line: n1-n2

C<c-r>=-(<c-r>") 
C                cut current line, enter insert mode
 <c-r>=          evaluate the following
       -(<c-r>") negate n1-n2

<Esc>j@qq@qdd
<Esc>         exit insert mode
     j        go 1 step down to the next values
      @qq     recursively call macro q and end macro
         @q   call macro q
           dd delete last extra line
\$\endgroup\$
3
  • 1
    \$\begingroup\$ -7 bytes \$\endgroup\$ – kops Apr 22 at 20:48
  • \$\begingroup\$ @kops thanks. I should use Jr in my other answers as well. \$\endgroup\$ – Razetime Apr 23 at 3:26
  • 1
    \$\begingroup\$ Found one more byte \$\endgroup\$ – kops Apr 23 at 5:38
4
\$\begingroup\$

Trivial answers

05AB1E, 1 byte

¥

Try it online!

R, 4 bytes

diff

Try it online!

Jelly, 1 byte

I

Try it online!

Feel free to add your answers here.

\$\endgroup\$
5
  • 2
    \$\begingroup\$ What is a 'trivial answer'? 5 bytes = 5 commands for APL, or 4 bytes = 4 functions for Jelly seems quite a lot... Are there special 'brownie points' for outgolfing a 'trivial answer'? \$\endgroup\$ – Dominic van Essen Apr 22 at 15:44
  • \$\begingroup\$ @DominicvanEssen Hmm, you're right. Maybe I should remove the APL and Jelly (and maybe the Husk) answers, at least for now? \$\endgroup\$ – user Apr 22 at 15:48
  • \$\begingroup\$ They are pretty simple, and that's mostly why they are there. You can probably find most of these as part of many, many answers. \$\endgroup\$ – Razetime Apr 22 at 15:49
  • 3
    \$\begingroup\$ A "trivial answers" CW answer should consist of just builtin answers. Not only does this not contain it, but it doesn't even have the literal Jelly builtin for this \$\endgroup\$ – caird coinheringaahing Apr 22 at 15:49
  • \$\begingroup\$ I've removed them now \$\endgroup\$ – user Apr 22 at 15:56
4
\$\begingroup\$

Retina 0.8.2, 82 bytes

\d+
$*
¶.*
$&-$%`
^.*¶

%(`^((-1+)|(1*)-)-(1*)$
$2$3$4
(1*)-+\1
-
--|-$

1`1*$
$.&

Try it online! Takes input on separate lines but test suite converts from and to comma delimited for convenience. Explanation:

\d+
$*

Convert to unary.

¶.*
$&-$%`

Create expressions for the differences of successive terms. $%` grabs the previous term without having to match it.

^.*¶

Delete the first term.

%(`

Perform the rest of the script individually on each subtraction.

^((-1+)|(1*)-)-(1*)$
$2$3$4

If the two values have different signs then negate the second and add it to the first.

(1*)-+\1
-

Otherwise subtract the two values.

--|-$

The subtraction can result in a double negative or a trailing subtraction of zero which needs to be removed.

1`1*$
$.&

Convert to decimal.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ This question's been closed for nearly four hours (nice answer though!) \$\endgroup\$ – Redwolf Programs Apr 22 at 19:54
3
\$\begingroup\$

JavaScript (ES6), 32 bytes

a=>a.slice(1).map((v,i)=>v-a[i])

Try it online!


JavaScript (ES6), 33 bytes

a=>a.map(v=>-p+(p=v),p=a.shift())

Try it online!

JavaScript (ES10), 34 bytes

a=>a.flatMap((v,i)=>i?v-a[i-1]:[])

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ I don't think it can get much shorter than this... \$\endgroup\$ – Recursive Co. Apr 22 at 15:32
3
\$\begingroup\$

Python 3, 38 bytes

lambda s:[y-x for x,y in zip(s,s[1:])]

Try it online!

-8 bytes thanks to @cairdcoinheringaahing

\$\endgroup\$
1
2
\$\begingroup\$

Charcoal, 10 bytes

IEΦθκ⁻ι§θκ

Try it online! Link is to verbose version of code. Explanation:

   θ        Input array
  Φ         Filtered where
    κ       Current index is not zero
 E          Map over remaining elements
      ι     Current element
     ⁻      Subtract
        θ   Input array (still with zeroth element)
       §    Indexed by
         κ  Current index
I           Cast to string
            Implicitly print
\$\endgroup\$
2
\$\begingroup\$

APL 5 bytes

2-⍨/⎕

    ⎕ ⍝ input
 -⍨   ⍝ reversed subtraction
2  /  ⍝ between every 2 elements like [1,2,3] -> [1 f 2, 2 f 3]

try it online

\$\endgroup\$
1
\$\begingroup\$

Haskell, 17 bytes

zipWith(-)=<<tail

Try it online!

\$\endgroup\$
1
\$\begingroup\$

R, 25 bytes

head((x=scan())[-1]-x,-1)

Try it online!

The R 'trivial' answer is diff (difference).

So this is my attempt at the golfiest solution without diff...

\$\endgroup\$
1
  • \$\begingroup\$ There are lots of longer ones without diff, including my favorite, but I haven't been able to find a shorter one. \$\endgroup\$ – Giuseppe Apr 22 at 18:12
1
\$\begingroup\$

Jelly, 2 bytes

Try it online!

Full program, as it relies on Jelly's outputting format.

This applies I (forward differences) to each overlapping pair [x, y] (Ɲ), yielding [y - x] for each. Due to Jelly's printing, as a full program, it omits the brackets around single element lists, so the output looks like a flat list.

Jelly, 3 bytes

Non-builtin approaches. All are functions

_ƝN

Try it online!

_@Ɲ

Try it online!

IƝF

Try it online!

How they work

They all use the Ɲ quick, which takes a link before it (a command, usually an atom) and a list, and applies the link to all overlapping pairs in the list.

_ is subtraction in Jelly, and @ reverses the order of the arguments to the previous link.

The first one calculates x - y for each overlapping pair, then negates each.

The second calculates x -@ y for each overlapping pair, which is equivalent to y - x

The third applies I (forward differences) to each pair, yielding [y - x]. We then flatten this into a single array.

\$\endgroup\$
4
  • \$\begingroup\$ Hm... Just when I was wondering what did and what did not qualify as a 'trivial' answer... \$\endgroup\$ – Dominic van Essen Apr 22 at 15:47
  • \$\begingroup\$ @DominicvanEssen I didn't edit this into the CW answer because I think they should be for builtin-only answers, which this one clearly isn't. If it were edited to remove the APL and Jelly answers, I might \$\endgroup\$ – caird coinheringaahing Apr 22 at 15:48
  • \$\begingroup\$ (only the first one - I - was there when I commented: I assume that counts as a built-in, right? The others all look perfectly valid to me... well done [unless there's a 2-byter that you're missing...]) \$\endgroup\$ – Dominic van Essen Apr 22 at 15:54
  • \$\begingroup\$ @DominicvanEssen Yeah, I don't think you can get more "builtin" than 1 byte in Jelly :P \$\endgroup\$ – caird coinheringaahing Apr 22 at 15:56
0
\$\begingroup\$

Wolfram Language (Mathematica), 11 bytes

Differences

Try it online!

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.