# Output a string's cumulative slope

## Challenge

Given a string such as Hello World!, break it down into its character values: 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33.

Then calculate the difference between each consecutive pair of characters: 29, 7, 0, 3, -79, 55, 24, 3, -6, -8, -67.

Finally, sum them and print the final result: -39.

## Rules

• Standard loopholes apply
• Creative solutions encouraged
• Have fun
• This is marked as , the shortest answer in bytes wins but will not be selected.
• Dennis's observation shows that this task is phrased in a more complicated way than necessary. Commented Sep 26, 2016 at 2:47
• Can a language accept input as a character array even if it supports string types?
– Poke
Commented Sep 26, 2016 at 15:13
• @Poke sorry, has to be a string Commented Sep 27, 2016 at 20:19
• @GregMartin I actually did not realize that until later. The challenge should stay this way though. Commented Sep 27, 2016 at 20:19
• @DJMcMayhem Good to know, all other forms of output are hereby allowed. Commented Sep 27, 2016 at 20:58

# APL (Dyalog Extended), 9 bytes

⊢/-⍥⎕UCS⊃


Try it online!

#### Explanation

    ⎕UCS     Codepoints of input
⊃    Last item
-⍥         Minus
⊢/           First item


Old:

{                    }    Define a function
(⍴⍵)                    Length of argument
⎕UCS⍵              Codepoints of input
(    ⌷     )             Index in (1-indexed)
i.e. get the last item
-            Minus
1⌷          First item of
⎕UCS⍵     Codepoints of input

• Doesn't ⊢/-⍥⎕UCS⊃ do the trick?
Commented Mar 4, 2023 at 19:12
• No you give it a name and call it as a function with the string as argument. This is a template I use to exclude the assignment from the byte count.
Commented Mar 4, 2023 at 23:18
• @Adám thanks. I'm still a bit new to APL. Commented Mar 5, 2023 at 7:09
• @Adám I've looked through APL wiki and I'm still a bit unsure on how that's working. I've added an attempt at an explanation but it probably contains a lot of mistakes. Can you point out any corrections? Commented Mar 5, 2023 at 14:03
• It is a fork: ⊢/(-⍥⎕UCS )⊃ subtracting - the first ⊃ from the last ⊢/ but preprocessing ⍥ both arguments to - using ⎕UCS.
Commented Mar 5, 2023 at 16:55

# Trilangle, 19 bytes

i2|<_!..>iL.,<@-\S/


Try it in the online interpreter!

Unfolds to this:

     i
2 |
< _ !
. . > i
L . , < @
- \ S / . .


Roughly equivalent to this C code:

#include <stdio.h>

int main() {
// RED PATH
int a, b;
a = b = getchar();
while (1) {
int c = getchar();
if (c == EOF) {
// BLUE PATH
printf("%d\n", b - a);
return 0;
} else {
// GREEN PATH
b = c;
}
}
}


# Thunno 2SB, 1 byte

Ṣ


Try it online!

#### Explanation

   # Implicit input
# Convert to ordinals
Ṣ  # Take the deltas
# Sum the list
# Implicit output


# Raku, 18 bytes

{[-] .ords[*-1,0]}


Try it online!

.ords returns a list of the ordinal values of the characters in the input string, [* - 1, 0] slices into it to return the last and first values, and [-] reduces that two-element list using subtraction.

import Data.Char
f s=sum$g$ord<$>s g(a:b:r)=b-a:g(b:r) g _=[]  # Java 7, 100 96 bytes int c(String s){char[]a=s.toCharArray();int r=0,i=a.length-1;for(;i>0;r+=a[i]-a[--i]);return r;}  Ungolfed & test code: Try it here. class M{ static int c(String s){ char[] a = s.toCharArray(); int r = 0, i = a.length-1; for(; i > 0; r += a[i] - a[--i]); return r; } public static void main(String[] a){ System.out.println(c("Hello World!")); } }  Output: -39 # Clojure, 31 bytes #(-(int(last %))(int(first %)))  Someone reduced the task already to a single operation. # PHP, 39 Bytes <?=ord(substr($s=$argv[1],-1))-ord($s);


## Mathematica, 30 bytes

Tr@Differences@ToCharacterCode


# Befunge-93, 16 bytes

~:v_$-.@ ~\<^0:  # Befunge-98, 14 bytes ~v >-.@ \>#^~  Reads (~) the first character, and then loops reading (~) a character and swaping (\) the top 2 items in the stack (so the first character keeps being raised to the top of the stack) until EOF, then substracts (-) the top 2 items, outputs (.) the result and ends (@). # C, 69 bytes main(i,e,z){while(EOF!=(i =getc(stdin))){z+=e-i;e=i;}putc(z,stdout);}  • Useless whitespace at i =getc. Commented Sep 28, 2016 at 13:41 # Pyth (fork), 5 bytes s.+CM  Does not work on the online interpreter, as current online interpreters are based only on the main repo. Explanation:  CM Map ordinals over input .+ Deltas, calculates differences between consecutive numbers s Sums the result  ## Haskell, 27 bytes \x->(ord.last)x-(ord.head)x  Unfortunately, Haskell has no knowledge of character codes until you import Data.Char. Fortunately, Haskell already treats strings as character arrays. \x-> --Syntax for anonymous function of 'x' (ord --Character code of... .last) --last element in the array x --Apply to argument - (ord --Character code of... .head) --first element in the array x --Apply to argument  • Don't you have to import ord? – Jo King Commented Aug 20, 2019 at 1:05 ## Pyke, 5 bytes m.o$s


Try it here!

m.o   -   map(ord, input)
\$  -  delta(^)
s - sum(^)


## Racket 121 bytes

(λ(s)(let((l(map char->integer(string->list s))))(for/sum((i(range 1(length l))))(-(list-ref l i)(list-ref l(- i 1))))))


Ungolfed version

(define f
(λ(s)
(let ((l (map char->integer
(string->list s))))
(for/sum ((i (range 1 (length l))))
(- (list-ref l i)
(list-ref l (- i 1)))))))


Testing

(f "Hello World!")

-39


# Groovy (80 Bytes)

f={n->s=[];n.eachWithIndex{x,i->s<<(i+1<n.size()?(int)n[i+1]-(int)x:0)};s.sum()}


# brainfuck, 31 bytes

>+[>,>,[-<->]<]<<[>[-<+>]<<]>-.


Try it online!

• How does this print negative numbers? Also, I don't think this works?
– Jo King
Commented Aug 20, 2019 at 0:31

# C# .NET, 156 bytes

public class P{public static void Main(string[]a){int q=0;for(int i=0;i<a[0].Length;i++){q+=i!=a[0].Length-1?a[0][i+1]-a[0][i]:0;}System.Console.Write(q);}}


Try online

# Jelly, 3 bytes

OIS


Try it online!

Charcodes, deltas, sum.

# Factor, 25 bytes

[ dup last swap first - ]


Try it online!

# C (gcc), 64 bytes

Takes input from command line: ./a.out "<input>"

main(c,v)char**v;{for(v++;1[*v];)c-=**v-*++*v;printf("%d",c-2);}


Try it online!

# J, 12 bytes

1#.2-~/\3&u:


Attempt This Online!

1#.2-~/\3&u:
3&u:  NB. convert to codepoints
2   \      NB. for each consecutive pair
-~/       NB. minus reduce the first from the second
1#.           NB. sum reduce


# Alternative 12 bytes

3({:-{.)@u:]


Attempt This Online!

3({:-{.)@u:]
3        u:]   NB. convert input to codepoints
@      NB. then
{:           NB. tail
-          NB. subtract


# JavaScript, 36 bytes

s=>s.at(-1)[x='charCodeAt']()-s[x]()


Try it:

f=s=>s.at(-1)[x='charCodeAt']()-s[x]()

input.addEventListener('input', e => result.innerHTML = f(input.value))
result.innerHTML = f(input.value)
<input id="input" value="Hello world!"/>
<div id="result"></div>`