Range of ASCII values

Given a string, output the range of ASCII values.

Example

Let's say we have the string Hello.

We get the ASCII values:

• H = 72
• e = 101
• l = 108
• l = 108
• o = 111

Now, we get the range (max - min):

• 111 - 72 = 39

Test cases

Input           Output
Hello, World!   82
aaaaa           0
Code Golf       79
Stack Exchange  88
ASCII           18


Scoring

For scoring, we will get the range of ASCII/Unicode values of your program source code, and add that to the length of your code in bytes.

For example, with the program abc, the score would be:

• 3 (the length of the program) + 2 (the ASCII range of the program) = 5

Clarifications

• There will be no non-ASCII characters in the input (but your program source code may contain non-ASCII characters)
• There will be no newline characters in the input (but your program source code may include newlines)
• Lowest score wins
• Sandbox Nov 28, 2022 at 17:30
• What if our language uses a different code page e.g. Jelly's code page instead of ASCII/Unicode? I'd suggest using the range of code points + length Nov 28, 2022 at 17:34
• What about languages for which the basic coding unit is not a character? For instance, machine code (basic units = raw bytes) or Nibbles (basic units = raw 4-bit nibbles). Should we score using the range of values of the basic unit, or the range of values of bytes in the code, or the range of Unicode values if the code was interpreted as if it was a string (but I imagine this might not always be parseable...)? Nov 28, 2022 at 18:59
• @JeppeStigNielsen - don't forget the range of the characters of your code, though: in this case it seems to be a whopping 9069: not very competitive! Nov 29, 2022 at 10:54

R, 29 bytes + 77 = 106

$$a)diff(range(utf8ToInt(a)))  Attempt This Online! Seems that most golfy approach has also the lowest score, since we need to use utf8ToInt() and it has the range of 77 by itself. 05AB1E, score: 158 124 (4 bytes) Ç{¥O  Explanation: Ç # Get a list of codepoint integers of the (implicit) input-string { # Sort them ¥ # Get the forward differences/deltas O # Sum those together # (after which the result is output implicitly)  MathGolf, score: 200 191 (4 6 bytes†) †: MathGolf uses Code page 437. _╙\╓-  Increased the byte-count to slightly improve the score, but this is pretty hopeless.. The ord builtin  is too far apart from most of the other useful builtins like min(╓)/max(╙)/sum(Σ).. Input as a list of characters. Try it online. Explanation:  # Get the codepoint integers of each character in the (implicit) input-list _ # Duplicate it ╙ # Pop and push its maximum \ # Swap ╓ # Pop and push its minimum - # Subtract this minimum from the maximum # (after which the entire stack is output implicitly)  ><>, Score: 50 bytes + 74 = 124 0ff*i:0(ee+*98++0.:@:@:@)??>@:@:@(??>30.@-n;  Try it online! Explanation Initialize max = 0 and min = 225. Then go through the input and update these values as needed. At the end, print max - min. Range Max: n = 110 We need n to print a number, so there isn't much we can do about this. Range Min:  = 36  swaps the top 2 values of the stack. Replacing it could save 6 on the range, but since we are using it in 6 places it seems unlikely that could we could save on the score by the reducing the range. Code We've replaced ~ = 126 with ?  and r = 114 with @ which saves 16 on the range at the cost of 3 bytes. We've replaced the line feed = 10 with jumps . = 46 which saves 26 on the range at the cost of 6 bytes. Perl 5-plF'' 33 bytes + 84 = 117 @_=map ord,sort@F;_=_[-1]-_[0]  Try it online! • Note the custom scoring criteria - you need to add ASCII range of your code to the score Nov 29, 2022 at 14:01 • @KirillL.– edited now Nov 29, 2022 at 17:17 Pyth, 8 bytes + 34 = 42 +F.+CDCM  Try it online! • CM: Map to ASCII value • CD: Sort (equivalent to S) • .+: Deltas (Differences between consecutive elements, all nonegative because of sort) • +F: Sum (equivalent to s) Using .+ is golfier than h and e, and also stays within the established ASCII value range. • Using deltas is a neat trick. K doesn’t have a handy sort so it ends up longer. f:+/1_-':@/1<:\. – doug Nov 30, 2022 at 2:43 • Ah, but it lowers the score! Thanks. – doug Nov 30, 2022 at 5:08 Factor, 70 range + 21 bytes = 91 score math.statistics:range  Requires modernish Factor for fully-qualified word syntax, hence nothing online will suffice. PHP, 39 bytes + 84 -> 123 fn(s)=>max(a=unpack('C*',s))-min(a)  Try it online! EDIT: OK this time I admit functional approach is way better :P OLD VERSION: PHP, -F 67 bytes + 78 -> 145 for(n=INF;c=ord(argn[i++]);c<m?:m=c)c>n?:n=c;echom-n;  Try it online! I wanted to give it a shot without arrays / min / max, in my traditional horrific-full of dollars-for loop Style and try to optimize for ASCII range rather. Stupid argn cannot be in full caps. funny that it gives the same score like this (caps and no space nor brackets was the original plan): PHP, -F 88 bytes + 57 -> 145 FOR(N=INF;C=ORD(GLOBALS[STRTOLOWER(ARGN)][I++]);C<M?:M=C)C>N?:N=C;ECHOM-N;  Try it online! APL, 11 chars + 9069-codepoint range = 9080. (In Dyalog one-byte encoding, range is 186 for a total of 197.) (⌈/-⌊/)⎕UCS  Try it online! • Don't forget to include the character code range of your code in your score for this challenge. There's a link to automatically score your submission above under "Scoring." Nov 30, 2022 at 19:55 Python 3, 32 bytes + 88 = 120 lambda x:ord(max(x))-ord(min(x))  Try it online! • This challenge has its own scoring criteria (rather than bytes), so your score is actually 120 Dec 19, 2022 at 12:29 • @TheThonnu Fixed! Mind upvoting my answer :) Dec 19, 2022 at 12:39 • Sorry, I've reached my daily voting limit. I will tomorrow. Dec 19, 2022 at 12:39 • @TheThonnu No problem, also on my other post :) Dec 19, 2022 at 12:41 • @TheThonnu Feel free whenever you have time :) Dec 20, 2022 at 2:06 GolfScript, 8 bytes, score 64 ($$@-\;


Try it online!

Explanation

char     : usage                                : stack
$: ($)ort the (implicit) input          : <sorted input>
(       : take the first element out           : <sorted input> <first item>
\      : swap the top two values of the stack : <first item> <sorted input>
)     : last item                            : <first item> <sorted input> <last item>
@    : rotate the stack                     : <sorted input> <last item> <first item>
-   : subtract, giving us the range        : <sorted input> <range>
\  : swap the top two values on the stack : <range> <sorted input>
; : discard the top of the stack         : <range>
(after which the stack is implicitly output)


Java 8, 106 108 bytes + 80 range = score 186 188

-2 bytes thanks to @ceilingcat

s->(s.chars().reduce(0,(a,b)->a>0?b>a>>16?b*65792-a%256*255:b<a%256?a/65536*65792-b*255:a:b*65537)>>8)%256


Try it online!

There is a better Java solution already posted, but maybe my answer could interest some people.

I'm not sure how to properly explain it, but by trying to keep only 1 inline Java stream, my goal was to carry 3 numbers < 256 inside only 1 integer ("a", the accumulator of the stream reduction) using this structure:
[_____max_value_____] [difference_between_max_and_min] [____min_value____]
<- 65 536 and over -> <- from 256 to 65 535 -> <- from 0 to 255 ->

Lua, 66 57 bytes + 89 86 = score 155 143

s=...print(math.max(s:byte(1,#s))-math.min(s:byte(1,#s)))


Try it online!

Program which takes command line argument for the strings.

Stax, 7 bytes + 79 = 86 score

c|Ms|m-


Run and debug it

Techincally there's a shorter solution length wise, but it would have a score of 9553...

c|Ms|m-
c       # copy the input string | stack: <string> <string>
|M     # max of array          | stack: <string> <max>
s    # swap                  | stack: <max> <string>
|m  # min of array          | stack: <max> <min>
- # subtract              | stack: <max-min>


KamilaLisp v0.2, 88 + 35 = 123

[- $(foldl1 max)$(foldl1 min)]@ucs