# 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. – Greg Martin Sep 26 '16 at 2:47
• Can a language accept input as a character array even if it supports string types? – Poke Sep 26 '16 at 15:13
• @Poke sorry, has to be a string – dkudriavtsev Sep 27 '16 at 20:19
• @GregMartin I actually did not realize that until later. The challenge should stay this way though. – dkudriavtsev Sep 27 '16 at 20:19
• @DJMcMayhem Good to know, all other forms of output are hereby allowed. – dkudriavtsev Sep 27 '16 at 20:58

# Python, 29 bytes

lambda s:ord(s[-1])-ord(s[0])


The sum of the differences forms a telescopic series, so most summands cancel out and
(s1 - s0) + (s2 - s1) + … + (sn-1 - sn-2) + (sn - sn-1) = sn - s0.

If taking a byte string as input is allowed

lambda s:s[-1]-s[0]


will work as well for 19 bytes.

Test both on Ideone.

• Does this print the result? – dkudriavtsev Sep 27 '16 at 20:10
• In a REPL, I guess it does. The intended form of output is a return value though, which is one of our default methods of output. If that's not allowed, most of the answers in production languages are invalid. – Dennis Sep 27 '16 at 20:30

# MATL, 2 bytes

ds


Try it online!

Explanation:

d gets the difference between consecutive characters and s sums the resulting array. Then, the value on top of the stack is implicitly printed. Not much else to say about that.

Interestingly enough, even though Dennis discovered an awesome shortcut, using it would be significantly longer in MATL.

# Jelly, 3 bytes

OIS


Try it online!

Take the Ordinals of the input string’s characters, then the Increments of that list, then the Sum of that list.

• Yeah, printing the result (and taking input in the first place) happens implicitly in Jelly. – Lynn Sep 27 '16 at 20:11

# MATLAB, 16 bytes

@(x)sum(diff(x))


This creates an anonymous function named ans which can be called like: ans('Hello world!').

Here is an online demo in Octave which requires an additional byte + to explicitly convert the input string to a numeric array prior to computing the element-to-element difference

# Python, 63 bytes

x=map(ord,input())
print sum(map(lambda(a,b):b-a,zip(x,x[1:])))


Ideone it!

# Cubix, 13 bytes

Cubix is a 2-dimensional language wrapped around a cube.

i?u//O-t#;/.@


Test it online! This maps to the following cube net:

    i ?
u /
/ O - t # ; / .
@ . . . . . . .
. .
. .


Where the IP (instruction pointer) starts at the top-left of the far-left face.

### How it works

First, the IP hits the mirror / which redirects it onto the i on the top face. The top face is a loop which continually inputs charcodes until EOF is reached. When the input is empty, the result of i is -1; the IP turns left from the ?, hitting the / on the far right face and going through the following commands:

• ; - Pop the top item (-1).
• # - Push the length of the stack.
• t - Pop the top item and get the item at that index in the stack. This pulls up the bottom item.
• - - Subtract.
• O - Output as an integer.
• / - Deflects the IP to the @, which ends the program.

# C#, 22 bytes

s=>s[s.Length-1]-s[0];


Full source code with test case:

using System;

namespace StringCumulativeSlope
{
class Program
{
static void Main(string[] args)
{
Func<string,int>f= s=>s[s.Length-1]-s[0];
Console.WriteLine(f("Hello World!"));
}
}
}


# C# with LINQ, 17 bytes

A shorter version, using LINQ, thanks to hstde:

s=>s.Last()-s[0];


However, an extra import is necessary:

using System.Linq;

• s=>s.Last()-s[0]; would be only 17 bytes – hstde Sep 27 '16 at 6:06

## Ruby, 23 bytes

->s{s[-1].ord-s[0].ord}


Assign to a variable like f=->s{s[-1].ord-s[0].ord} and call like f["Hello World!"]

Uses Dennis's observation about telescopic series.

• You don't need to print the output, only return it, so you can get rid of $><<. – Jordan Sep 26 '16 at 5:52 • Yes, I read the question also. Luckily, there is broad consensus on the definition of "output" (see also: the many answers on this page which return rather than print a value). But hey, it's your code. – Jordan Sep 27 '16 at 20:04 # reticular, 12 bytes idVc~@qVc-o;  Try it online! Using Dennis's observation, we can shorten an iterative process into a simpler one. idVc~@qVc-o; i take input d duplicate V pop input copy, push last character c get its char code ~ put it under the input in the stack @q reverse the item at the top of the stack V get the last item of that (first item of input) c convert to char - subtract o output ; and terminate  # Brain-Flak, 51 bytes 48 bytes of code plus three bytes for the -a flag, which enables ASCII input (but decimal output. How convenient. :D) {([{}]({})<>)<>}<>{}([]<>){({}[()])<>({}{})<>}<>  Try it online! This ones a little bit harder than my other answer, haha. Lets walk through it. { While the top of the stack is nonzero: ( Push: [{}] The top of the stack times negative one. Pop this off. ({}) Plus the value on top of the stack, which is duplicated to save for later. <> On to the other stack ) <> Move back to the first stack } <> After the loop, move back again. {} We have one extra element on the stack, so pop it ([]<>) Push the height of the alternate stack back onto the first stack { While the top of the stack is nonzero: ({}[()]) Decrement this stack <> Move back to the alternate stack ({}{}) Sum the top two elements <> Move back tothe first stack } <> Switch back to the stack holding the sum  # 05AB1E, 3 bytes Ç¥O  Try it Online! Uses CP-1252 encoding. Explanation Ç Converts input string to ASCII ¥ Compute difference between successive elements O Sum the result  • OK, cool. Sorry. – dkudriavtsev Sep 27 '16 at 20:14 # Brachylog, 7 bytes @c$)@[-


Try it online!

### Explanation

@c        Convert "Hello World!" to [72,101,108,108,111,32,87,111,114,108,100,33]
$) Circular permute right: [33,72,101,108,108,111,32,87,111,114,108,100] @[ Take a prefix of the list - Subtract  Since subtract only works for an input of two integers, it will succeed once the selected prefix is [33, 72]. # Haskell, 32 bytes g=fromEnum f t=g(last t)-g(t!!0)  • @nimi It's the same. – xnor Sep 27 '16 at 7:06 ## R, 69 43 32 bytes A very non-competing answer although I thought it'd be fun to showcase a possible solution in R. sum(diff(strtoi(sapply(strsplit(readline(),"")[[1]],charToRaw),16L)))  The only interesting aspect of this answer is the use of sapplyand charToRaw. First I split the string into a vector of characters that I want to convert into its ASCII integer representations. The charToRaw function is not vectorized in R and instead of looping over each value in aforementioned vector I use sapply which effectively vectorizes the function. Subsequently take 1st difference and then sum. Edit: Turns out charToRaw transform a string into a vector where each element is the raw representation of each character, hence no need to use strsplit and sapply sum(diff(strtoi(charToRaw(readline()),16)))  Edit2: Turns out there is an even better way, the function utf8ToInt(x) does exactly what strtoi(charToRaw(x),16) which means we can save a few more bytes (Idea taken from @rturnbull's answer to another question): sum(diff(utf8ToInt(readline())))  # Perl, 19 bytes Includes +1 for -p Give input on STDIN without final newline echo -n "Hello World!" | slope.pl; echo  slope.pl: #!/usr/bin/perl -p$_=-ord()+ord chop


If you are sure the input string has at least 2 characters this 17 byte version works too:

#!/usr/bin/perl -p

# RProgN, 142 Bytes, Non-competing

function tostack 'b' asoc stack 'a' asoc 0 'v' asoc b pop byte 'o' asoc b len while [ v o b pop byte ] 'o' asoc - + 'v' asoc b len end [ v end


Non-competing, as the 'tostack' command was added after the discovery of this challenge (even though it has a terrible byte count)

# Test Cases

Hello, World!
-39

Cool, huh?
-4


# Explanation

function                        # Push the function between this and end to the stack
tostack 'b' asoc            # Convert the implicit input to a stack, associate it with 'b'
0 'v' asoc                  # Push 0 to the stack, associate it with 'v'
b pop byte 'o' asoc         # Pop the top value of b (The end of the input), get the byte value, associate it with 'o'.
b len                       # Push the size of b to the stack
while [                     # While the top of the stack is truthy, pop the top of the stack
v                       # Push v to the stack
o                   # Push o to the stack
b pop byte          # Pop the top value of b, push the byte value of that to the stack
] 'o' asoc          # Push a copy of the top of the stack, associate it with 'o'
-                   # Subtract the top of the stack from one underneith that, In this case, the old value of o and the byte.
+                       # Sum the top of the stack and underneith that, that is, the difference of the old value and new, and the total value
'v' asoc                # Associate it with 'v'
b len                   # Push the size of b to the stack (which acts as the conditional for the next itteration)
end [                       # Pop the top of the stack, which will likely be the left over size of b
v                           # Push the value of v to the top of the stack
end                             # Implicitely returned / printed


RProgN is an esoteric language I've been working on with Reverse Polish Notation in mind. It's currently pretty verbose, with it's variable assignment being 4 characters, and such, however I plan to in future add a bit of syntatic sugar.

Also, RProgN implicitly accesses arguments from the stack, and returns them the same way. Any string data left in the stack after the program has finished, is implicitly printed.

• "A bit of Sugar" really changed form in the few months this took. This entire thing is now ~{bid☼[+ and that's a bit adorable. – ATaco Dec 6 '16 at 0:40

<<<$[#1-#c]  Try it online! In arithmetic mode, #name gets the character code of the first character in name. We set c to the last character, and take the difference between the first and last codes. # Haskell, 30 bytes (-).x.last<*>x.head x=fromEnum  Try it online! Subtracts the ordinal value of the last character from the first one. # Haskell, 61 bytes 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 %)))