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
= 72e
= 101l
= 108l
= 108o
= 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
-
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\$caird coinheringaahing– caird coinheringaahing ♦2022年11月28日 17:34:15 +00:00Commented 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\$Dominic van Essen– Dominic van Essen2022年11月28日 18:59:36 +00:00Commented 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\$Dominic van Essen– Dominic van Essen2022年11月29日 10:54:26 +00:00Commented 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– The Thonnu2023年03月10日 17:53:41 +00:00Commented Mar 10, 2023 at 17:53
-
1\$\begingroup\$ @ShaunBebbington yeah, that's fine \$\endgroup\$The Thonnu– The Thonnu2023年06月04日 18:57:15 +00:00Commented Jun 4, 2023 at 18:57
58 Answers 58
MATL, score 37 (length 7, range 30)
X>GX<ZP
Try it online! Or verify all test cases and source code.
X> % Implicit input. Maximum
G % Push input again
X< % Minimum
ZP % Distance (of code points). Implicit display
C (clang), 79 bytes + 28 = (削除) 145 138 129 112 111 (削除ここまで) 107
-7 score thanks to @Digital Trauma
(削除) -9 (削除ここまで) -10 score thanks to @jdt
A,B;D(*C)??<*C<A?A=*C:*C>B?B=*C:0;*++C?D(C):0;??>C(*C)??<A=B=*C;D(C);*C=B-A;??>
Explanation
??<
and ??>
are trigraphs for {
and }
respectively
A,B; // A will track the minimum, B will track the maximum
D(*C){ // D() will traverse C updating the values of A and B
*C<A? // if current character < A
A=*C // update A with it's value
:*C>B? // else if current character > B
B=*C // update B with it's value
:0; // else, do nothing
*++C? // if we're not at the end of the string
D(C) // call D recursively with the next character
:0; // else, do nothing
} // end D()
C(*C){ // C() takes the string, which is also where we'll store the output
A=B=*C; // initialize A and B with the first character in C
D(C); // call D
*C=B-A; // store the range (max - min) in the out argument
} // end C()
-
1\$\begingroup\$ You can reduce the overall score to 138 if you use trigraphs: ... \$\endgroup\$Digital Trauma– Digital Trauma2022年11月28日 20:23:11 +00:00Commented Nov 28, 2022 at 20:23
-
\$\begingroup\$ tio.run/… \$\endgroup\$Digital Trauma– Digital Trauma2022年11月28日 20:23:23 +00:00Commented Nov 28, 2022 at 20:23
-
\$\begingroup\$ @DigitalTrauma nice one, braces are really expensive \$\endgroup\$c--– c--2022年11月28日 20:24:00 +00:00Commented Nov 28, 2022 at 20:24
-
1\$\begingroup\$ score: 111 \$\endgroup\$jdt– jdt2022年11月30日 17:29:58 +00:00Commented Nov 30, 2022 at 17:29
-
\$\begingroup\$ @jdt that's interesting, I didn't know you could do that \$\endgroup\$c--– c--2022年11月30日 22:11:27 +00:00Commented Nov 30, 2022 at 22:11
Bash + GNU utilities, 52 bytes + 92 range = 144
od -An -w1 -td1 -v|sort -n|sed -n '1p;$p'|dc -e??r-p
Pip, 13 bytes + 39 = 52
a:A^aMXaADMNa
Explanation
A straight code-golf solution to the challenge might look like this:
A*:a;Ma-Na
a First command-line argument
A* Get ASCII value of each character
: and assign that list of integers back to a
; Statement separator
Na Minimum of a
a- Subtract that from each element in a
M and take the maximum of the resulting list
This gets a score of 65. The a
for getting the command-line argument and A
for taking ASCII values are both necessary, so to improve the score we'll need to look at the characters with ASCII values less than A
.
The smallest character is *
, which we can get rid of by splitting the string into a list of characters with ^
and passing that list to A
directly:
A*:a
->
a:A^a
The next-smallest character is -
, which we can get rid of by using the absolute difference operator AD
instead. Then we have to rearrange a bit more to avoid needing a space; we'll use the old MX
and MN
operators for max and min instead of the newer M
and N
.
Ma-Na
->
MXaADMNa
(We could also keep using M
, but since it also requires a semicolon to parse correctly, the score is the same if we replace it with MX
which doesn't need the semicolon.)
Terse, 7 bytes + 6769 = 6776
找没最手样找到
Could be worse.
找 Char codes
没 Maximum, after
最手 Squaring and then square root (identity)
样 Subtract
找到 Minimum
-
\$\begingroup\$ what kind of wretched 残体字 programming language is this ?? \$\endgroup\$RARE Kpop Manifesto– RARE Kpop Manifesto2022年12月19日 05:12:19 +00:00Commented Dec 19, 2022 at 5:12
-
\$\begingroup\$ @RAREKpopManifesto let's say I did not have readability in mind when I designed the stuff \$\endgroup\$Gymhgy– Gymhgy2023年01月01日 20:29:20 +00:00Commented Jan 1, 2023 at 20:29
-
\$\begingroup\$ that's not my point - i could read simplified. the question was about why use a character set invented by CCP instead of one that has existed since 嬴政 統一六國 文字 ? \$\endgroup\$RARE Kpop Manifesto– RARE Kpop Manifesto2023年01月02日 19:13:23 +00:00Commented Jan 2, 2023 at 19:13
-
\$\begingroup\$ Unfortunately this program can't handle strings of length 1, for which it should return \0ドル\$. \$\endgroup\$The Thonnu– The Thonnu2023年03月10日 17:54:30 +00:00Commented Mar 10, 2023 at 17:54
Alumin, (削除) 36 (削除ここまで) 19 bytes + (削除) 22 (削除ここまで) 20 = (削除) 58 (削除ここまで) 39
idiqdhhhwrvsruripkc
Both the right language for the job(削除) , and horribly inefficient at it! (削除ここまで) Turns out, I'm just an inefficient thinker!
Explanation
idiqdhhhwrvsruripkc
explanation | stack
id take input, duplicate | MAX MIN
iq ip while input > 0 | MAX MIN in
d duplicate | MAX MIN in in
hhhw grab top 3 | MAX [ MIN in in ]
r reverse stack | MAX [ in in MIN ]
v get lesser of two | MAX [ in MIN' ]
s ungrab | MAX in MIN'
r reverse stack | MIN' in MAX
u get greater of two | MIN' MAX'
r reverse stack | MAX' MIN'
(this gives us a better score than using `y` to swap 2)
loop finishes | MAX MIN -1
k discard eof | MAX MIN
c subtract | MAX-MIN
Explanation (Old)
hqidrlhcwrspkrklhhgwfufsykrlhcwfvfsc
h push 1 (to start loop)
q p input loop
id take input, duplicate
r reverse stack
lhc stack length - 1
wrs reverse that many elements off the stack
this has the effect of pushing two copies of the input
krk pop trailing EOF twice
lhhg stack length / 2
w grab that many elements into a new stack
fuf fold over maximum
sykr place on bottom stack, remove initial 1
lhc stack length - 1
w grab that many elements as before
fvf fold over minimum
sc push and subtract
Husk, 11 bytes + 63 = 74
ASz>o;FYOmc
ASz>o;FYOmc
m # map over input
c # getting character values
O # and sort into ascending order;
z # now, zip together
> # getting differences
S # the sorted list itself
# and
o; # the single-element list of
F # fold over the sorted list
Y # getting pairwise maxima
A # finally, get the average
# of the resulting single-element list.
Nibbles, (削除) 9 (削除ここまで) 12 bytes + (削除) 56 (削除ここまで) (削除) 49 (削除ここまで) 38 = 50
;*;-;o;/;\;`<@@o;/_$;`$$
This corresponds to the Nibbles program with nibbles:
6 a 6 9 6 e 6 a 6 b 6 e 4 d 4 e 6 a 5 3 6 d 7 3
Which, paired-up into bytes, gives
6a 69 6e 6a 6b 6e 4d 4e 6a 53 6d 73
which can be read-out as a string (with the same ASCII values)
jinjknMNjSms
with ASCII range 115 (73
) - 77 (4d
) = 38.
How?
We start from a straightforward code-golf solution to the question: -o/\;`<$$o/
(5.5 bytes, 11 nibbles):
-o/\;`<$@o/
- # subtract
o # character value of
/ # fold over
\ # reverse of
; # (save)
`< # sorted
@ # input
$ # returning left element each time
# (so the fold returns the first element)
# from
o # character value of
/ # fold over
# (implicit) saved sorted input
# (implicit) returning left element each time
The nibbles are slightly re-arranged in the final program, since the 2-nibble 'sort' function ( `<
) wraps around its argument (here $
). Thus, the paired-up nibbles from this are:
-o /\ ;` @< $o / # program code (in final order)
9e ab 6e 4d 3e a # nibbles
We can reduce the range of bytes by inserting 'save' operators (;
, nibble 6
) to shift high-valued nibbles into the least-significant-nibble position of each byte.
This doesn't affect the output, but we need to override the final implicit variable to account for the saved (but ignored) values. Hence:
;- ;o ;/ ;\ ;` @< $o ;/ ;$ # program code (in final order), with final variable changed to ;$
69 6e 6a 6b 6e 4d 3e 6a 63 # nibbles
Modifying both folds (so returning right element @
instead of left element $
) avoids the lowest byte 3e
, but unfortunately outputs a negated value.
So we multiply (and save: ;*
) the negated value by its own sign (also saved: ;`$
): this corrects the output, introducing a new highest byte of 73
, but the overall score is still reduced.
-
\$\begingroup\$ @TheThonnu - The actual program corresponds to the nibbles in the .nbb file. The string that you've calculated from is just a human-readable form (which uses one character for every nibble of the program). The bytes of the program that you've scored were hex values
69 6a 6c 3e 38 34 6a 4a 34
, giving the all-ASCII stringijl>84jJ4
with range 56. \$\endgroup\$Dominic van Essen– Dominic van Essen2022年11月29日 07:48:36 +00:00Commented Nov 29, 2022 at 7:48 -
\$\begingroup\$ @TheThonnu here is an example of using the raw bytes of the (now updated) program directly. Obviously, the program is unintelligible for a human like this (since the characters represent pairs of nibbles that are smashed-together), but this is what it looks like if interpreted as characters... \$\endgroup\$Dominic van Essen– Dominic van Essen2022年11月29日 07:55:03 +00:00Commented Nov 29, 2022 at 7:55
Java 8, score: 134 (54 bytes)
s->s.chars().max().orElse(0)-s.chars().min().orElse(0)
Try it online (also contains code to extract the lambda function from the code-block and calculate its score: (max_codepoint - min_codepoint) + length
).
Not too much too improve unfortunately.
- We could change the
x
to\u0078
, but unfortunately the next highest codepoint then becomesu
which isn't too much lower thanx
, so the +5 in byte-count is higher than the -3 in codepoint, giving a +2 in score instead of decrease: Try it online. - Using a regular for-loop requires both
{}
and a space, so the difference of the max-min codepoints would widen on both ends. And in addition it's longer anyway: Try it online.
Explanation:
s-> // Method with String parameter and integer return-type
s.chars() // Convert the String to an IntStream of codepoint integers
.max() // Get the maximum of these codepoints
.orElse(0) // Convert the OptionalInt to an int
// (`.orElse(0)` is 1 byte shorter than `.getAsInt()`)
- // And subtract
s.chars().min().orElse(0)
// The same to get the minimum codepoint
Excel, score = (削除) 103 (削除ここまで) 94 (length 46, range 48)
-9 score thanks to Engineer Toast
=LET(A,CODE(RIGHT(A1,ROW(A:A))),MAX(A)-MIN(A))
-
2\$\begingroup\$ Same range but shorter length (46) by abusing CODE() which will convert only the first character of a multi-character string:
=LET(A,CODE(RIGHT(A1,ROW(A:A))),MAX(A)-MIN(A))
\$\endgroup\$Engineer Toast– Engineer Toast2022年11月29日 14:58:14 +00:00Commented Nov 29, 2022 at 14:58
MATL, 3 bytes + 32 = 35
Sds
Explanation
Sds
S % Sort implicit input
d % Difference between adjacent elements
s % Sum the values
Jelly, 5 bytes + 149 = 154
I think this is about as good as it's going to get in Jelly due to the distance between O
(unavoidable as it's the only way to get the ordinals of the input string) and the large ordinals of Unicode characters representing the bytes for mapping and getting maximums and minimums, index into a list etc.
OÞOIS
A monadic Link that accepts a list of characters and yields the range size as an integer.
How?
OÞOIS - Link: list of characters (ordinal)
Þ - sort by: (222)
O - ordinal value (79)
O - ordinal values (79)
I - forward differences (73)
S - sum (83)
----
222
-ひく 73
=わ=わ=わ=わ
149
-
\$\begingroup\$ Yeah, the golflangs with custom codepages are probably going to struggle on this challenge... \$\endgroup\$The Thonnu– The Thonnu2022年11月28日 18:13:54 +00:00Commented Nov 28, 2022 at 18:13
-
\$\begingroup\$ @TheThonnu strictly speaking the code is bytes (the Unicode characters are just an abstraction for ease of reading), but I think this may still be best using that measure too (with a score of \5ドル + 63 = 68\$). \$\endgroup\$Jonathan Allan– Jonathan Allan2022年11月28日 18:18:24 +00:00Commented Nov 28, 2022 at 18:18
-
\$\begingroup\$ Would it help if you used literal UTF-8 bytes and ran with
fu
oreu
? \$\endgroup\$naffetS– naffetS2022年11月28日 19:58:49 +00:00Commented Nov 28, 2022 at 19:58 -
2\$\begingroup\$ No, I mean take the literal indices in the Jelly codepage and interpret it as a UTF-8 byte sequence, then run with Jelly's
fu
/eu
flags. I thought it might be a lower score. (In other words, use the indices in the Jelly codepage, and have the program use those indices as UTF-8 byte sequence.) \$\endgroup\$naffetS– naffetS2022年11月29日 01:46:15 +00:00Commented Nov 29, 2022 at 1:46 -
1\$\begingroup\$ Sorry, I had it backwards lol. Yes, what i meant is running without the
u
flag. Try it online! \$\endgroup\$naffetS– naffetS2022年11月29日 22:11:23 +00:00Commented Nov 29, 2022 at 22:11
R, 29 bytes + 77 = 106
\(a)diff(range(utf8ToInt(a)))
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.
Ly, 8 bytes + 70 = 78
ia0I-s>l
I found a shorter program, but the character range of the code was big enough that it had a higher total score.
ia - read STDIN onto stack as codepoints, sort the stack
0I - copy the bottom of the stack to the top
- - subtract to get the range
s>l - save result, switch to clean stack, load result
-
1\$\begingroup\$ Note the custom scoring criteria - you need to add ASCII range of your code to the score \$\endgroup\$Kirill L.– Kirill L.2022年11月29日 14:01:29 +00:00Commented Nov 29, 2022 at 14:01
-
\$\begingroup\$ @KirillL. Got it, thanks. \$\endgroup\$hkdtam– hkdtam2022年11月29日 14:07:23 +00:00Commented Nov 29, 2022 at 14:07
-
1\$\begingroup\$ By our site consensus, you can't take input from a variable (
@_
in this case). You can take input from a function:{[-] .ords.minmax[*-1,0]}
for a score of 118. You can also use a full program:put [-] get.ords.minmax[*-1,0]
which is also 118 score. \$\endgroup\$naffetS– naffetS2022年11月29日 21:58:52 +00:00Commented Nov 29, 2022 at 21:58 -
-
\$\begingroup\$ Sorry, no, because you're still taking input from a variable (
$_
). \$\endgroup\$naffetS– naffetS2022年11月30日 17:10:27 +00:00Commented Nov 30, 2022 at 17:10
J, score 70 (16 bytes, range 54)
[:+/2-/\a.I.]\:]
]
\:
]
: Pass the input (using the identity function]
) on both sides of\:
to sort it in descending order.a.
I.
: For each character of the sorted input, find its index in the "alphabet"; at least on TIO's setup, this contains all of ISO-8859-1 in order, which includes ASCII. Thus, this produces the ASCII values of the characters, while keeping the highest used character lower than the straightforward method of3
u:
would.
(I.
would also take the following index if a character is not found, but that never happens; it was chosen overi.
to reduce the highest used character.)2-/\
: For each length-2 subarray (from the2
and\
), subtract the second value from the first (-
/
).[:
+/
: Take the sum (+
/
) of the values, using[:
to do so monadically.
Charcoal -v, 35 bytes, score 61
castminusordceilceil[q]ordminmin[q]
Try it online! Abusing Charcoal's verbose parser again. In order for it to recognise q
as a variable name I can't follow it with a letter, so the next nearest suitable characters are the list delimiters. I've also used ceil
instead of max
to avoid using an x
; this costs 2 bytes but overall saves 1 from my score. Normally the program would be written like this:
Print(Cast(Minus(Ordinal(Maximum(q)), Ordinal(Minimum(q)))));
The best I could do in succinct Charcoal was a score of 10135:
⭆ψ−c/o⌈θc/o⌊θ
Try it online! Link is to verbose version of code. Explanation: The limiting factors here are the Greek letter θ
needed for the two occurrences of the input variable (the only other input method uses S
which has a ridiculously high Unicode code point of 65531) and the ⭆
used to stringify the result (again, I
would have had far too high a code point).
ψ Predefined string null character
⭆ Map over characters and join
θ Input string
⌈ Maximium
c/o Ordinal
− Minus
θ Input string
⌊ Minimum
c/o Ordinal
Implicitly print
If using bytes from Charcoal's code page was allowed, its score would "only" be 210:
SαI−c/o⌈αc/o⌊α
Try it online! Link is to verbose version of code. Explanation: The limiting factors here are that ⌊
(Minimum
) is byte index 25 while α
(variable a
) is byte index 225, although I did reduce my score by 14 by taking the input into α
rather than the default input of θ
(variable q
, byte index 241).
S Input as a string
α Store in variable `a`
α Input string
⌈ Maximum
c/o Ordinal
− Subtract
α Input string
⌊ Minimum
c/o Ordinal
I Cast to string
Implicitly print
Pyth, 8 bytes + 34 = 42
+F.+CDCM
CM
: Map to ASCII valueCD
: Sort (equivalent toS
).+
: Deltas (Differences between consecutive elements, all nonegative because of sort)+F
: Sum (equivalent tos
)
Using .+
is golfier than h
and e
, and also stays within the established ASCII value range.
-
\$\begingroup\$ Using deltas is a neat trick. K doesn’t have a handy sort so it ends up longer. f:+/1_-':@/1<:\. \$\endgroup\$doug– doug2022年11月30日 02:43:18 +00:00Commented Nov 30, 2022 at 2:43
-
\$\begingroup\$ Ah, but it lowers the score! Thanks. \$\endgroup\$doug– doug2022年11月30日 05:08:13 +00:00Commented Nov 30, 2022 at 5:08
Burlesque, 12 bytes + 52 = 64
)**J>]\/<].-
)** # Map to char code
J # Duplicate
>] # Maximum
\/ # Swap
<] # Minimum
.- # Difference
C# (Visual C# Interactive Compiler), 98 score (18 bytes)
ASCII range: 80
Length: 18
Total score: 98
s=>s.Max()-s.Min()
- -27 score thanks to Kevin Cruijssen for the tip to use C# Interactive
C# (Visual C# Compiler), 125 score (36 bytes)
ASCII range: 89
Length: 36
Total score: 125
s=>s.Max()-s.Min()
If we're going traditional through the compiler then we have to include an extra 18 bytes for using System.Linq;
-
\$\begingroup\$ 98 score (18
length
+ 80max-min
) by using theVisual C# Interactive Compiler
as language, so you won't need theUsing
: try it online. \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2022年11月29日 17:42:28 +00:00Commented Nov 29, 2022 at 17:42 -
\$\begingroup\$ @KevinCruijssen Ah good tip, it's been a while so a little rusty on some of the tips and tricks! \$\endgroup\$TheLethalCoder– TheLethalCoder2022年11月30日 08:55:47 +00:00Commented Nov 30, 2022 at 8:55
-
\$\begingroup\$ Why the additional
f
? It seems like the older 6 byte version both meets all test cases and has a lower score \$\endgroup\$emirps– emirps2023年04月03日 15:08:47 +00:00Commented Apr 3, 2023 at 15:08 -
\$\begingroup\$ @emirps it only works because of an update made a couple days ago that make
G
andg
on numbers be a no-op instead of erroring. Thanks \$\endgroup\$naffetS– naffetS2023年04月04日 00:35:43 +00:00Commented Apr 4, 2023 at 0:35
-
\$\begingroup\$ This challenge has its own scoring criterion (not code-golf) so your score is 116 \$\endgroup\$The Thonnu– The Thonnu2023年04月06日 05:56:03 +00:00Commented Apr 6, 2023 at 5:56
Thunno 2 GB
, 2 + 24 = 26
G_
Port of lyxal's Vyxal answer.
Explanation
G_ # Implicit input
# Convert to codepoints
G # Maximum codepoint
_ # Subtract each input codepoint
# Find the maximum
# Implicit output
Sequences, score = 73 + 4.12 = 77.12
(Sequences uses the 96 printable ASCII characters as its codepage, so the byte count of this program is \5ドル\log_{256}(96) \approx 4.12\$)
`vMm-
Explanation
`vMm- # Implicit input as a string
`v # Get the ASCII values of the input string in a list
M # Get the maximum value of that list
m # Get the minimum value of that list
- # And subtract to find the range
# (implicit output)
-
\$\begingroup\$ Same ASCII range, two bytes shorter:
->s{y=s.bytes;y.max-y.min}
\$\endgroup\$naffetS– naffetS2022年11月28日 19:53:47 +00:00Commented Nov 28, 2022 at 19:53 -
\$\begingroup\$ @Steffan Cool, thanks. \$\endgroup\$Jordan– Jordan2022年11月28日 20:06:37 +00:00Commented Nov 28, 2022 at 20:06