13
\$\begingroup\$

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
  • No using pre-made functions that perform this exact task
  • Creative solutions encouraged
  • Have fun
  • This is marked as , the shortest answer in bytes wins but will not be selected.
\$\endgroup\$
5
  • 20
    \$\begingroup\$ Dennis's observation shows that this task is phrased in a more complicated way than necessary. \$\endgroup\$ Commented Sep 26, 2016 at 2:47
  • \$\begingroup\$ Can a language accept input as a character array even if it supports string types? \$\endgroup\$ Commented Sep 26, 2016 at 15:13
  • \$\begingroup\$ @Poke sorry, has to be a string \$\endgroup\$ Commented Sep 27, 2016 at 20:19
  • 1
    \$\begingroup\$ @GregMartin I actually did not realize that until later. The challenge should stay this way though. \$\endgroup\$ Commented Sep 27, 2016 at 20:19
  • \$\begingroup\$ @DJMcMayhem Good to know, all other forms of output are hereby allowed. \$\endgroup\$ Commented Sep 27, 2016 at 20:58

53 Answers 53

1
2
1
+100
\$\begingroup\$

APL (Dyalog Extended), 9 bytes

⊢/-⍥⎕UCS⊃ 

Try it online!

-13 thanks to @Adám

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 
\$\endgroup\$
7
  • \$\begingroup\$ Doesn't ⊢/-⍥⎕UCS⊃ do the trick? \$\endgroup\$ Commented Mar 4, 2023 at 19:12
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Mar 4, 2023 at 23:18
  • \$\begingroup\$ @Adám thanks. I'm still a bit new to APL. \$\endgroup\$ Commented Mar 5, 2023 at 7:09
  • \$\begingroup\$ @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? \$\endgroup\$ Commented Mar 5, 2023 at 14:03
  • \$\begingroup\$ It is a fork: ⊢/(-⍥⎕UCS )⊃ subtracting - the first from the last ⊢/ but preprocessing both arguments to - using ⎕UCS. \$\endgroup\$ Commented Mar 5, 2023 at 16:55
1
\$\begingroup\$

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. monadic fork {. NB. head {: NB. tail - NB. subtract 
\$\endgroup\$
1
\$\begingroup\$

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; } } } 
\$\endgroup\$
1
\$\begingroup\$

Thunno 2 SB, 1 byte

Try it online!

Explanation

 # Implicit input # Convert to ordinals Ṣ # Take the deltas # Sum the list # Implicit output 
\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

Haskell, 61 bytes

import Data.Char f s=sum$g$ord<$>s g(a:b:r)=b-a:g(b:r) g _=[] 
\$\endgroup\$
0
\$\begingroup\$

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

\$\endgroup\$
0
\$\begingroup\$

Clojure, 31 bytes

#(-(int(last %))(int(first %))) 

Someone reduced the task already to a single operation.

\$\endgroup\$
0
\$\begingroup\$

PHP, 39 Bytes

<?=ord(substr($s=$argv[1],-1))-ord($s); 
\$\endgroup\$
0
0
\$\begingroup\$

Mathematica, 30 bytes

Tr@Differences@ToCharacterCode 
\$\endgroup\$
0
\$\begingroup\$

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 (@).

\$\endgroup\$
0
\$\begingroup\$

C, 69 bytes

main(i,e,z){while(EOF!=(i =getc(stdin))){z+=e-i;e=i;}putc(z,stdout);} 
\$\endgroup\$
1
  • \$\begingroup\$ Useless whitespace at i =getc. \$\endgroup\$ Commented Sep 28, 2016 at 13:41
0
\$\begingroup\$

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 
\$\endgroup\$
0
\$\begingroup\$

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 
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Don't you have to import ord? \$\endgroup\$ Commented Aug 20, 2019 at 1:05
0
\$\begingroup\$

Pyke, 5 bytes

m.o$s 

Try it here!

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

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 
\$\endgroup\$
0
\$\begingroup\$

Groovy (80 Bytes)

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

brainfuck, 31 bytes

Probably easy to outgolf, any ideas about this code will be appreciated.

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

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ How does this print negative numbers? Also, I don't think this works? \$\endgroup\$ Commented Aug 20, 2019 at 0:31
0
\$\begingroup\$

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

\$\endgroup\$
0
\$\begingroup\$

Jelly, 3 bytes

OIS 

Try it online!

A monadic link, I think.

Charcodes, deltas, sum.

\$\endgroup\$
0
\$\begingroup\$

Factor, 25 bytes

[ dup last swap first - ] 

Try it online!

\$\endgroup\$
0
\$\begingroup\$

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!

\$\endgroup\$
0
\$\begingroup\$

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>

\$\endgroup\$
1
2

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.