25
\$\begingroup\$

Your task

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

Our answer is 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

You can use this TIO link to get your score.

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
\$\endgroup\$
13
  • 1
    \$\begingroup\$ 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 \$\endgroup\$ Nov 28, 2022 at 17:34
  • 1
    \$\begingroup\$ 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...)? \$\endgroup\$ Nov 28, 2022 at 18:59
  • 3
    \$\begingroup\$ @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! \$\endgroup\$ Nov 29, 2022 at 10:54
  • 1
    \$\begingroup\$ @Deadcode yes it is. I'll add comments below the answers that don't support this. \$\endgroup\$
    – The Thonnu
    Mar 10, 2023 at 17:53
  • 1
    \$\begingroup\$ @ShaunBebbington yeah, that's fine \$\endgroup\$
    – The Thonnu
    Jun 4, 2023 at 18:57

54 Answers 54

1
2
1
\$\begingroup\$

J-uby, 21 bytes + 87 = 108

:bytes|:-%[:max,:min]

Attempt This Online!

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

q, 19 bytes

(-/)7h$(max;min)@\:

k4, 17 bytes

(-/)7h$(|/;&/)@\:
\$\endgroup\$
1
  • 3
    \$\begingroup\$ Don't forget to add the range of your source code to calculate your final score... \$\endgroup\$ Nov 29, 2022 at 10:23
1
\$\begingroup\$

J, score = 15 + 79 = 94

[:(>./-<./)3&u:

Attempt This Online!

[:(>./-<./)3&u:
[:               NB. enforce f (g y)
           3&u:  NB. convert string to list of char codes
  (       )      NB. monadic fork
       <./       NB. min
   >./           NB. max
      -          NB. subtract
\$\endgroup\$
1
\$\begingroup\$

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.

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

05AB1E, score: 158 124 (4 bytes)

Ç{¥O

Try it online or verify all test cases or verify its score.

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

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

><>, 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.

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

Perl 5 -plF'' 33 bytes + 84 = 117

@_=map ord,sort@F;$_=$_[-1]-$_[0]

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ Note the custom scoring criteria - you need to add ASCII range of your code to the score \$\endgroup\$
    – Kirill L.
    Nov 29, 2022 at 14:01
  • \$\begingroup\$ @KirillL.– edited now \$\endgroup\$
    – Kjetil S
    Nov 29, 2022 at 17:17
1
\$\begingroup\$

Factor, 70 range + 21 bytes = 91 score

math.statistics:range

enter image description here

Requires modernish Factor for fully-qualified word syntax, hence nothing online will suffice.

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

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;echo$m-$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;ECHO$M-$N;

Try it online!

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

APL, 11 chars + 9069-codepoint range = 9080.

(In Dyalog one-byte encoding, range is 186 for a total of 197.)

(⌈/-⌊/)⎕UCS

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ 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." \$\endgroup\$
    – Jordan
    Nov 30, 2022 at 19:55
1
\$\begingroup\$

Python 3, 32 bytes + 88 = 120

lambda x:ord(max(x))-ord(min(x))

Try it online!

\$\endgroup\$
5
  • \$\begingroup\$ This challenge has its own scoring criteria (rather than bytes), so your score is actually 120 \$\endgroup\$
    – The Thonnu
    Dec 19, 2022 at 12:29
  • \$\begingroup\$ @TheThonnu Fixed! Mind upvoting my answer :) \$\endgroup\$ Dec 19, 2022 at 12:39
  • 1
    \$\begingroup\$ Sorry, I've reached my daily voting limit. I will tomorrow. \$\endgroup\$
    – The Thonnu
    Dec 19, 2022 at 12:39
  • 1
    \$\begingroup\$ @TheThonnu No problem, also on my other post :) \$\endgroup\$ Dec 19, 2022 at 12:41
  • 1
    \$\begingroup\$ @TheThonnu Feel free whenever you have time :) \$\endgroup\$ Dec 20, 2022 at 2:06
1
\$\begingroup\$

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

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 ->

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

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.

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

KamilaLisp v0.2, 88 + 35 = 123

[- $(foldl1 max) $(foldl1 min)]@ucs
\$\endgroup\$
1
\$\begingroup\$

Stax, 2 bytes + 57 = 59 score

:s

Run and debug it

Apparently there's a builtin...

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

Go, 95 bytes + 93 = 188

func(s string)int{m:=rune(s[0]);n:=m;for _,r:=range s{if m<r{m=r};if n>r{n=r}};return int(m-n)}

Attempt This Online!

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

Python 3, 39 bytes + 80 range = 119

print(ord(max(a:=input()))-ord(min(a)))

Try it online!

A full program is one point better here because it avoids the minimum character " " (32) for "(" (40).

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

Arturo, 31 bytes + 33 = 106 94 64

function[x][do[x|min]x|max|sub]

Try it!

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

PowerShell, 58 bytes + 93 = 151

$args|%{$a=($_|% t*y)|%{[byte][char]$_}|sort;$a[-1]-$a[0]}

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Your score is your bytes plus the range of your code which in this case is 93 for a total of 151. \$\endgroup\$
    – chunes
    Jun 4, 2023 at 19:00
1
\$\begingroup\$

Raku (Perl 6) (rakudo), 16 bytes + 78 = 94

+*.ords.minmax-1

Attempt This Online!

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

Nekomata, 6 bytes + 66 = 72

eo:h-l

Attempt This Online!

eo:h-l
e       Convert to codepoints
 o      Sort
  :     Duplicate
   h    Head
    -   Subtract
     l  Last
\$\endgroup\$
1
\$\begingroup\$

Vyxal 2.4.1 K, 4 bytes + 26 range = Score 30

G?-G

Try it Online!

If I knew what I was doing in 2021, this could have been 3 bytes with the G flag, but I never did figure out how to make the K flag play nice with other flags.

Explained

G?-G­⁡​‎‎⁡⁠⁡‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁢‏⁠‎⁡⁠⁣‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁤‏‏​⁡⁠⁡‌­
G     # ‎⁡Greatest character in the input
 ?-   # ‎⁢With each character code subtract from it
   G  # ‎⁣Greatest of those
💎

Created with the help of Luminespire.

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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