25
\$\begingroup\$

Your task

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
['!@#', '$%^', '&*(']                        55.7 / 55.67 / 55.667 / etc.
['qwertyuiop[', 'asdfghjkl;', 'zxcvbnm,']    110.9 / 110.91 / 110.909 / etc.

Rules/clarifications

  • The output must be only the average ord. You may not output anything else.
  • It may be rounded (up or down) to any number (\$\ge\$ 1) of decimal places. If your language does not support floating points, you may output the average multiplied by 10.
  • Floating point innacuracies are ok.
  • You may assume that the input list will always have a length of 2 or more
  • You may assume that the strings will never be empty
  • You may assume that the strings will not contain whitespace or non-ASCII characters
  • This is , so shortest code in bytes wins!
\$\endgroup\$
10
  • \$\begingroup\$ Sandbox \$\endgroup\$
    – The Thonnu
    Nov 5, 2022 at 13:52
  • 1
    \$\begingroup\$ @Arnauld - oops, I forgot that wasn't an ASCII character. I've changed the test case. \$\endgroup\$
    – The Thonnu
    Nov 5, 2022 at 14:00
  • \$\begingroup\$ If you allow ≥0 decimal places, then languages that work only with integers would be able to compete, too. Or, as an alternative, could one output the average times ten? \$\endgroup\$ Nov 5, 2022 at 14:03
  • 2
    \$\begingroup\$ @DominicvanEssen - for languages that don't have floating points, I'll allow outputting the average times 10. I'll edit that in now. \$\endgroup\$
    – The Thonnu
    Nov 5, 2022 at 14:04
  • 1
    \$\begingroup\$ What is ord or why does ord(h) + ord(e) + ord(l) + ord(l) + ord(o)) / len('hello') give 106.4? \$\endgroup\$
    – QBrute
    Nov 7, 2022 at 21:37

49 Answers 49

1
2
3
\$\begingroup\$

C (clang), 87 86 84 81 bytes.

v;*p;f(**t,float*r){for(*r=0;p=*t;*r=fmax(*r,v*1./(p-*t++)))for(v=0;*p;)v+=*p++;}

Try it online!

\$\endgroup\$
3
\$\begingroup\$

C++ (gcc), 166 120 bytes

-46 thanks to the combined efforts of @ceilingcat and @jdt.

#import<bits/stdc++.h>
auto f(auto a,float*r){*r=0;for(char*t:a){int u=0,s=0;for(;*t;u+=*t++)s++;*r=fmax(*r,(0.+u)/s);}}

Try it online!

Original Answer:

Attempting to apply general-purpose programming to code golf doesn't seem to work out well.

#import<bits/stdc++.h>
auto g(char*a){int t=0,s=0;for(a--;*++a;s++,t+=*a);return(0.0+t)/s;}auto f(std::list<char*>a){float m=0;for(char*t:a)m=m>g(t)?m:g(t);return m;}

Try it online!

Ungolfed:

// Gets the average ord value of a string
auto g(char* a) {
    int t=0, s=0;             // Running total and size 

    for (a--;*++a;s++,t+=*a); // Loop until a equals 0 and add a onto the running total
    
    return (0.0+t) / s;       // Cast to float by adding 0.0 and divide the total by the size.
}

// "Main" function
auto f(std::list<char*> a) {
    float m = 0;           // Maximum value
    
    for (char* t : a) {    // Loop through all the strings in the list
        m = m>g(t)?m:g(t); // If m is greater than the output from the function, return m. Otherwise, return the output of the function
    }
    
    return m;              // m is the final result that we want
}
\$\endgroup\$
8
  • 1
    \$\begingroup\$ 120 bytes \$\endgroup\$
    – jdt
    Nov 10, 2022 at 15:35
  • \$\begingroup\$ or 108 bytes for a direct port of my C answer. \$\endgroup\$
    – jdt
    Nov 10, 2022 at 15:49
  • 1
    \$\begingroup\$ Building on @ceilingcat 93 bytes \$\endgroup\$
    – jdt
    Nov 10, 2022 at 18:13
  • 1
    \$\begingroup\$ Building on @jdt 90 bytes \$\endgroup\$
    – ceilingcat
    Nov 11, 2022 at 20:10
  • 1
    \$\begingroup\$ also 87 bytes but without the ugly casting. \$\endgroup\$
    – jdt
    Nov 11, 2022 at 20:47
3
\$\begingroup\$

><>, 36 35 bytes

000\&l3-,:{:@)?$&0(?n1[!
"(?\+:i:"!

Try it online!

Words are separated by spaces (or characters less than space like newline). This uses the length of the stack rather than a counter to get the length of the word.

\$\endgroup\$
3
\$\begingroup\$

Arturo, 48 22 bytes

$=>[map&=>average|max]

Try it

\$\endgroup\$
2
\$\begingroup\$

Husk, 6 bytes

▲moAmc

Try it online!

Outputs the highest average ord value as an exact fraction.

▲moAmc
 mo    # map 2 functions over each element of input
    mc #   get ord values of each character
   A   #   get the average of those
▲      # output the maximum.
\$\endgroup\$
2
\$\begingroup\$

JavaScript (Node.js), 58 bytes

a=>Math.max(...a.map(s=>eval(Buffer(s).join`+`)/s.length))

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Nibbles, 8 bytes (16 nibbles)

`/.$/*+.$o$10,$]

Nibbles works only in integers, so this returns the maximum average ord multiplied by 10 (and rounded-down to a whole number).

`/.$/*+.$o$10,$]
  .$              # map over each input string
       .$         #   map over each character
         o$       #     getting it's ord 
      +           #   sum them all
     *     10     #   multiply by 10
    /             #   and divide by    
             ,$   #   the length of the string
`/                # finally, fold over the list
               ]  #   getting the max of each pair

enter image description here

\$\endgroup\$
3
  • \$\begingroup\$ OP said in the sandbox that you have to round, not floor or ceil: codegolf.meta.stackexchange.com/questions/2140/… \$\endgroup\$
    – naffetS
    Nov 5, 2022 at 20:56
  • \$\begingroup\$ @Sʨɠɠan - Hm, I only read the challenge itself; I've asked OP to specify whether rounding-down is Ok or not... \$\endgroup\$ Nov 5, 2022 at 21:01
  • \$\begingroup\$ @Sʨɠɠan - rounding-down seems to be Ok with the OP, thank goodness: rounding-to-nearest would have increased the already-bloated code by another 50% to 12 bytes: /.$/+/*+.$o$100,$5 10] (mutiply by 100, integer-divide by length, add 5, integer-divide by 10)... \$\endgroup\$ Nov 6, 2022 at 11:13
2
\$\begingroup\$

sclin, 38 bytes

"dup S>c0\+ fold rev len /"map0\| fold

Try it here! This might be a good case for adding sum/prod/min/max functions to sclin, the fold construct is alright but it could be better.

For testing purposes:

["hello" "world" "bye"] ; 30N>d n>o
"dup S>c0\+ fold rev len /"map0\| fold

Explanation

Prettified code:

( dup S>c 0 \+ fold rev len / ) map 0 \| fold

Assuming input list xs.

  • (...) map map over xs...
    • dup S>c get codepoints
    • 0 \+ fold sum
    • rev len / divide by length (i.e. average)
  • 0 \| fold maximum
\$\endgroup\$
2
\$\begingroup\$

J-uby, 43 38 36 30 bytes

-5 bytes thanks to Steffan

Originally based on TKirishima's Ruby answer.

:*&:/%[:bytes|:sum,:+@|Q]|:max

Attempt This Online!

Explanation

:* & :/ % [ :bytes | :sum, :+@ | Q ] | :max

:* &                                         # Map with...
            :bytes | :sum                    #   Sum of bytes
     :/ % [              ,         ]         #   Divided by
                           :+@ | Q           #   Length converted to float
                                     | :max  # Get max

\$\endgroup\$
2
  • \$\begingroup\$ 38 bytes using bytes.sum instead of chars.sum(&:ord): :map+:/%[:bytes|:sum,:size|:to_f]|:max \$\endgroup\$
    – naffetS
    Nov 15, 2022 at 21:24
  • \$\begingroup\$ @Steffan Good catch. Thanks! \$\endgroup\$
    – Jordan
    Nov 15, 2022 at 21:31
2
\$\begingroup\$

CJam, 19 14 bytes

{:d_:+\,/}%$W=

Try it online!

A function which takes an input as an array. Link includes test cases.

Explanation

{:d_:+\,/}%$W= # function which takes an array as input
{        }%    # for each value in the input array...
 :d            # short map `:` the operator `d` to convert into an array of `d`oubles
   _:+         # duplicate and sum
      \        # swap
       ,       # get the length of the array
        /      # divide, giving the average
           $   # perform a $ort on the resulting array
            W= # and get index `W` (shorthand for `-1`), which is the last value of the sort, or the max
\$\endgroup\$
2
\$\begingroup\$

Stax, 10 9 bytes

é)u6║"⌂w╙

Run and debug it

This is a PackedStax program, which unpacks to the following:

{:V}kTlE:_

Run and debug it

Explanation (unpacked)

{  }k      # map the following over all the values in the input
 :V        # get the mean of the array
     T     # Get the maximum of this map
      l    # The output is a fraction, so it must be listified
       E   # Explode this list onto the stack
        :_ # Float division
\$\endgroup\$
2
\$\begingroup\$

Lua, 94 bytes

I tried two approaches here, both of which ended up having the same byte count.

List of Arguments

m=0 for i=1,#arg do n=0s=arg[i]s:gsub('.',load'n=s.byte(...)+n')m=math.max(m,n/#s)end print(m)

Try it online!

Space Delimited String

m=0(...):gsub('%S+',load'n=0s=...s:gsub(".",load"n=s.byte(...)+n")m=math.max(m,n/#s)')print(m)

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Octave, 18 bytes

@(x)max(mean(+x'))

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Zsh, 63 bytes

for w;c=&&for a (${(s::)w})((c+=#a,o=1.*c/$#w,m=o>m?o:m))
<<<$m

Try it online!  65b 74b

for w; implicitly iterates over the arguments ($@).
for a (${(s::)w}) iterates over each letter in word w.
c accumulates the ascii sum
o is the average ord for w
m is the maximum o found

Edit: Saved 6 bytes by chaining 3 math expressions in the parentheses((,,))!

\$\endgroup\$
2
\$\begingroup\$

Uiua, 15 bytes

/↥∵(÷⊃⧻/+-@\0⊔)

Try it!

/↥∵(÷⊃⧻/+-@\0⊔)
  ∵(          )  # map over input
             ⊔   # unbox a string
         -@\0    # subtract null character
    ÷⊃⧻/+        # mean
/↥               # max
\$\endgroup\$
1
\$\begingroup\$

Charcoal, 15 bytes

WS⊞υ∕ΣEι℅κLιI⌈υ

Try it online! Link is to verbose version of code. Takes input as a list of newline-terminated strings. Explanation:

WS

Repeat until the end of the list is reached...

⊞υ∕ΣEι℅κLι

... take the ordinals of the characters in the string, and divide their sum by the string's length.

I⌈υ

Output the maximum average.

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

Python 3, 71 70 bytes

lambda l:max(map(lambda s:mean(map(ord,s)),l))
from statistics import*

Try it online!

-1 thanks to The Thonnu

\$\endgroup\$
2
  • 1
    \$\begingroup\$ You can remove the space for 70 bytes: Try it online! \$\endgroup\$
    – The Thonnu
    Dec 19, 2022 at 12:28
  • 1
    \$\begingroup\$ @TheThonnu Feel free to vote on this tomorrow too :) \$\endgroup\$ Dec 19, 2022 at 12:41
1
\$\begingroup\$

Thunno 2 G, 2 bytes

€m

Try it online!

Take the mean of ach string in the (implicit) input list (this automatically converts to codepoints). Then, take the Greatest item.

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

Nekomata, 4 bytes

eᵐµṀ

Attempt This Online!

eᵐµṀ
e       Ord
 ᵐ      Map
  µ     Mean
   Ṁ    Maximum
\$\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.