19
\$\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\$
11
  • \$\begingroup\$ Sandbox \$\endgroup\$
    – The Thonnu
    Nov 28, 2022 at 17:30
  • 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 at 17:53

45 Answers 45

1
2
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\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ Using deltas is a neat trick. K doesn’t have a handy sort so it ends up longer. f:+/1_-':@/1<:\. \$\endgroup\$
    – doug
    Nov 30, 2022 at 2:43
  • \$\begingroup\$ Ah, but it lowers the score! Thanks. \$\endgroup\$
    – doug
    Nov 30, 2022 at 5:08
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\$

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

KamilaLisp v0.2, 88 + 35 = 123

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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