Maximum average ord

Take a list of strings as the input, and output the maximum average ord.

Example

Given the list ['hello', 'world', 'bye']:

• The average ord of 'hello' is:
• (ord(h) + ord(e) + ord(l) + ord(l) + ord(o)) / len('hello')
• = 106.4
• The average ord of 'world' = 110.4
• The average ord of 'bye' = 106.7

The maximum average ord is 110.4. This is your output.

Test cases

Note: For the last two test cases, I have given the roundings for 1, 2, and 3 decimal places. As mentioned in the rules below, you can round to any number of decimal places.

Input                                        Output
['hello', 'world', 'bye']                    110.4
['code', 'golf', 'stack', 'exchange']        106.8

In the third row, we fist push the last value to the register & . We use this to later check if this was the end of the string. Then we divide by the length to get the new ORD value. Then we use the :{:@ trick to copy the stack if it has only 2 elements (which is always the case) from the stack and compare them. If the new one is better we swap the stack with $. In either case we delete the top item. Lastly, we take the value of the register and check if it is negative, &0(?. If so, we print the maximum ord and exit. If not, we jump back to 00. To get the correct direction we use the 00!.| trick. ><>, 6545 44 40 bytes 000i:"!"(?v+$1+$20.$~$0(?n00.>@$,:{:@(?


Try it online!

Takes input as a space separated string.

• Welcome back! \o/ First post in 3 years :) Nov 9, 2022 at 1:14

Python, 46 bytes

lambda l:max(sum(map(ord,s))/len(s)for s in l)


Attempt This Online!

Unfortunately, the only built-in mean function Python has is in the statistics module, and that's too expensive to import.

Factor, 24 bytes

[ 0 [ mean max ] foldl ]


Attempt This Online!

Just fold with the max of the mean.

R, 38 bytes

\(x)max(sapply(Map(utf8ToInt,x),mean))


Attempt This Online!

               Map(utf8ToInt,x)        # for every string in the input, store the vector of its codepoints in a list
sapply(                ,mean)  # apply mean to each of the list elements and return a vector
\(x)max(                             ) # take max


Jelly, 4 bytes

OÆmṀ


Try it online!

05AB1E, 10 9 4 bytes

ÇÅAà


-5 thanks to Sʨɠɠan

Try it online!

Explained:

ÇÅAà  # Implicit input as a list
# Implicit map over the input:
Ç     #   Get the ord of each character
ÅA   #   Get the average of the ords
à  # Get the maximum value

• I think that answering one's own challenge three times within 25 minutes of posting it is probably a record, although possibly not one that I'd encourage anyone to attempt to break... Nov 5, 2022 at 14:19
• @DominicvanEssen I'll probably delete this one if someone comes up with a better 05AB1E solution. Nov 5, 2022 at 14:22
• Why not just ÇÅAà? Ç is the same as εÇн} and everything vectorizes so you don't need any maps Nov 5, 2022 at 20:26
• @Sʨɠɠan wow, I didn't know that was possible! Nov 5, 2022 at 20:57

M:$+A*_/#_Mg  Try It Online! Knight (v2), 56 bytes ;=mF;W=pP;=s!=i~1;W>Lp=i+1i=s+*10A Gp iTs&<m=t/sLp=m tOm  Try it online! Because Knight doesn't support floating point values, it outputs the maximum average ord multiplied by $$\10\$$, as allowed by the rules: It may be rounded to any number ($$\\ge1\$$) of decimal places. If your language does not support floating points, you may output the average multiplied by 10. Julia, 34 bytes !a=max(sum.(Int,a)./length.(a)...)  Attempt This Online! Excel (ms365), 70 bytes Formula in F1: =MAX(MAP(A1:C1,LAMBDA(a,SUM(CODE(MID(a,SEQUENCE(LEN(a)),1)))/LEN(a))))  • you can get this down to 68 bytes if you expect input in array format (eg. ={"hello","world","bye"}) and use # to denote an array input (map(A1#,...) Nov 26, 2022 at 20:31 MathGolf, 4 bytes $m▓╙


Input as a list of lists of characters.

Try it online.

Explanation:

$# Get the codepoint of each inner-most character of the (implicit) input-list m # Map over each list of integers: ▓ # Pop and push the average of the list ╙ # After the map: pop and push the maximum # (after which the entire stack is output implicitly as result)  J, 18 bytes [:>./(1#.3&u:%#)&>  Accepts list of boxed strings Attempt This Online! [:>./(1#.3&u:%#)&> [: NB. cap, [: f g y -> f (g y) &> NB. for each input item, result will be unboxed ( ) NB. monadic fork # NB. length 3&u: NB. convert input to list of char codes % NB. vectorized division 1#. NB. sum the result to compute average >./ NB. max item  PHP 8.x, 75 73 bytes Creates an anonymous function that returns the expect values, hopefully. fn($x)=>max(array_map(fn($z)=>array_sum(unpack('C*',$z))/strlen($z),$x));


-2 bytes thanks to Sʨɠɠan.

How does it work?

This uses the unpack('C*', ...) function to convert all characters into an unsigned char (0-255).
This assumes all characters are part of the ASCII table.

Then it calculates the average using array_sum([...])/strlen([...]).
The function array_sum() takes an array and returns the sum of all elements, and strlen() returns the length of the string.

The function max() takes the array with the averages and returns the highest value from it.

Anonymous functions implicitly return values.

Example usage

You need to assign this to a variable, or call with call_user_func() or call_user_func_array():

<?php
$fn = fn($x)=>max(array_map(fn($z)=>array_sum(unpack('C*',$z))/strlen($z),$x));

// Should output: float(110.4)
var_dump($fn(['hello', 'world', 'bye']));  You can try this here (with test examples): https://onlinephp.io/c/2a424 • fn($x)=>max(array_map(fn($z)=>array_sum(unpack('C*',$z))/strlen($z),$x)); saves two bytes. Also you don't need to count the semicolon Nov 5, 2022 at 20:41
• ... How dumb of me to forget to use the string length ... Thank you! I count the semicolon, as it is a required part of the arrow function. Nov 5, 2022 at 22:10

T-SQL, 125 bytes

WITH C as(SELECT len(x)m,0b,*FROM @
UNION ALL SELECT m-1,b+ascii(right(x,m)),x
FROM C WHERE
m>0)SELECT max(b*1./len(x))FROM c


Try it online

Raku, 27 bytes

*».&{.ords.sum/.chars}.max


Try it online!

Edit: as per advice from @Steffan

• Welcome to Code Golf, and nice answer! Nov 14, 2022 at 19:45
• This is actually 26 bytes (25 characters), since we count in bytes Nov 14, 2022 at 19:46
• We don't allow hardcoding the input into the code - the best way to take input in Raku is via a function. This only costs one byte, you can just add a * at the beginning to make it a WhateverCode block. :) Try it online! Nov 14, 2022 at 19:47
• @Steffan : Thanks and amended as per your advice. Nov 15, 2022 at 13:49
• .chars can be .comb or .ords for -1. I was trying to figure out a way of using a HyperWhatever, but I couldn't find something shorter
– Jo King
Dec 19, 2022 at 13:01

VyxalG, 3 bytes

Cvṁ


Try it Online!

Ruby, 48 bytes

->l{l.map{_1.chars.sum(&:ord)/_1.size.to_f}.max}


As the division of two integers in ruby gives an integer, and what was expected was a float, it's long :/
And .fdiv is one byte longer

Attempt This Online!

• 40 bytes: ->l{l.map{1.0*_1.bytes.sum/_1.size}.max} Nov 5, 2022 at 20:45
• @Dingus, I think string sum won't work, as it wraps around with modulo, so it may pass given test cases, but not some big string Nov 6, 2022 at 9:18
• @KirillL. Thanks for pointing that out. You are right. I should've read the docs! Nov 6, 2022 at 10:15

Sequences, $$\9 \log_{256}(96) \approx 7.41\$$ bytes

[$v$aH]gM


Explanation

[$v$aH]gM  // Implicit list of string input
[     ]    // Loop through the input list:
$v$       //   Get a list of ords of each character
aH     //   Get the average and append to g
g   // Push the list g
M  // Get the maximum value
// Implicit output


Mathematica, 29 bytes

Max[Mean/@ToCharacterCode@#]&


View it on Wolfram Cloud!

Fig, $$\6\log_{256}(96)\approx\$$ 4.939 bytes

]KemMC


Try it online!

]KemMC
C - Charcodes
emM  - Mean of each
K     - Sort
]      - Last

• When maximum is two bytes long Nov 5, 2022 at 20:50
• what do you mean Nov 6, 2022 at 21:56
• ]K should be one byte Nov 6, 2022 at 23:43
• Ah yes. Pu that on my todo list Nov 7, 2022 at 2:42
• Lol.. Didn't knew there were two of you: Sʨɠɠan and Sʨɠɠan. The conversation in the comments above is pretty confusing if it would be only a single person. xD Nov 10, 2022 at 13:34

Java 8, 8179 56 bytes

a->a.mapToDouble(s->s.chars().average().orElse(0)).max()


Input as a Stream<String> and output as an OptionalDouble.

Try it online.

Explanation:

a->                  // Method with String-Stream parameter & OptionalDouble return-type
a.map             //  Map over the input-Stream
ToDouble(s-> //  and convert them to a Stream of Doubles
s.chars()      //   Convert the String to an IntStream of its codepoint integers
.average()    //   Then take the average of that IntStream (as OptionalDouble)
.orElse(0))   //   And convert the OptionalDouble to double
//   (.orElse(0) is shorter than .getAsDouble())
.max()           //  After the map: leave the maximum (again as OptionalDouble)


BQN, 13 12 bytes

-1 byte inspired by Aiden Chow's APL answer

⌈´+´∘-⟜@¨÷≠¨


Anonymous tacit function; takes a list of strings and returns a float. Try it at BQN online!

Explanation

⌈´+´∘-⟜@¨÷≠¨
≠¨  Length of each string
÷    Divide the following by the above:
+´∘           Sum of
-⟜@       Subtract null character from (each character in)
¨       Each string
⌈             Get the maximum of the resulting list of averages


Go, 132 108 bytes

func f(S[]string)(m int){for a,s:=range S{a=0
for _,r:=range s{a+=int(r)}
if a=a*10/len(s);a>m{m=a}}
return}


Attempt This Online!

• -24 bytes by @Sʨɠɠan: return as an int, multiplied by 10
• 108 (returns multiplied by 10) Nov 8, 2022 at 17:44
• I think that is only valid for languages that doesn't support floating point operations.
– jdt
Nov 10, 2022 at 16:58

Arturo, 48 bytes

$[b]->max map b=>[average to[:integer]to->:char]  Try it • $[b]-> A function taking an argument b
• map b=>[...] Map over the values in b using [...] with the current value implicitly supplied on the far right
• to->:char Convert the implicit string value to a block of chars
• to[:integer] Convert the block of chars to a block of integers
• average Find the average
• max Return the maximum value in the block of averages

Japt-h, 9 bytes

Takes input as an array of character arrays.

®xc /ZlÃñ


Try it

®xc /ZlÃñ     :Implicit input of 2D array of characters
®             :Map each sub-array Z
x            :  Reduce by addition of
c           :    Codepoints
/         :  Divide by
Zl       :    Length of Z
Ã      :End map
ñ     :Sort
:Implicit output of last element
`