18
\$\begingroup\$

Getting the average of a list (e.g [2,6,7])

  • Get the length of the list: [2,6,7] -> 3
  • Sum the numbers in the list: 2 + 6 + 7 = 15.
  • Divide the sum by their count: 15 / 3 = 5.

You should compare the averages of two lists of positive integers N and M, by returning a value if N has a higher average, another value if M has a higher average, and another one in case of a tie.


I/O rules

All the standard Input and Output methods are allowed.

Input

You may take input as two separate lists, a nested list, or anything else you consider suitable for the task. Please specify the format.

Output

The values provided have to be distinct and must consist of at least one non-whitespace character. Also, they must be consistent between runs (a single value for N, a single value for M, a single value for Tie). Please specify those in your answer. The values can be non-empty Strings, Bool values, Integers, or anything you consider suitable.


Specs

  • The lists will not necessarily have equal length.

  • You are guaranteed that the lists are non-empty.


Test Cases

I chose the values N wins, M wins and Tie, which are pretty much self-evident.

N, M                     ->   Output     (Averages)

[7], [6]                 -> N wins  (N has 7, M has 6 )
[4,5], [4,4]             -> N wins  (N has 4.5, M has 4)
[2,3,4], [4,5,6]         -> M wins  (N has 3, M has 5)
[4,1,3], [7,3,2,1,1,2]   -> Tie     (both have 2.666...)
[100,390,1], [89,82,89]  -> N wins  (N has 163.666..., M has 86.666...)
[92,892], [892,92]       -> Tie     (lists are basically identical) 
[10,182], [12,78,203,91] -> Tie     (both have 96)

Default Loopholes apply. Explanations are encouraged! This is , so the shortest code in bytes wins!

\$\endgroup\$
  • \$\begingroup\$ Sandbox. \$\endgroup\$ – Mr. Xcoder Jul 5 '17 at 17:55
  • \$\begingroup\$ if my language of choice only supports integers, can I take input multiplied by 1000? That way the calculated averages would still be accurate to 3 decimal places \$\endgroup\$ – Skidsdev Jul 7 '17 at 10:28
  • \$\begingroup\$ @Mayube Yes, that is allowed \$\endgroup\$ – Mr. Xcoder Jul 7 '17 at 10:29
  • \$\begingroup\$ We have to return an output of at least 1 character. Does that mean we are required to return a character or a string? Or do you mean an output whose string value is of a least 1 character? \$\endgroup\$ – Olivier Grégoire Jul 7 '17 at 12:14
  • \$\begingroup\$ @OlivierGrégoire The output given must be at least 1 character long (you cannot return an empty string, but can return any String of at least 1 character, and also any non-whitespace character). IT's up to you. \$\endgroup\$ – Mr. Xcoder Jul 7 '17 at 12:19

45 Answers 45

1
\$\begingroup\$

Haskell, 40 bytes

f x=sum x/sum(1<$x)
x!y=compare(f x)$f y
\$\endgroup\$
1
\$\begingroup\$

C#, 75 bytes

using System.Linq;n=>m=>n.Average()>m.Average?1:m.Average()>n.Average()?2:0
\$\endgroup\$
1
\$\begingroup\$

Gallina, 103 bytes

Definition a l := Z.div (fold_left Z.add l 0) (Zlength l).
Definition g l l' := Z.compare (a l) (a l').
\$\endgroup\$
1
\$\begingroup\$

SQL (MS T-SQL), 99 bytes

select SIGN((x.a)-(y.a))from(select avg(a) a from(values<list1>)a(a))x,(select avg(a) a from(values<list2>)a(a))y

This returns 1 if the first list has a higher average, 0 for ties and -1 for a heavier second list. Input uses this format: (1),(2),(3), which I have substituted with the placeholders <list1> and <list2> in the code above.

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

Retina, 69 66 bytes

\d+
$*
1(?=.*;(1+,)+)
1$#1$*
1(?<=(,1+)+;.*)
1$#1$*
,

+`1;1
;
D`1

Try it online! Link includes test cases. Edit: Saved 3 bytes thanks to @MartinEnder. Takes two comma-separated lists joined with a semicolon and returns ; for a tie, 1; if the first list's average is larger and ;1 if the second list's average is. Explanation: The first stage converts to unary. The second stage multiplies each element of the first list by the length of the second list, while the third stage multiplies each element of the second line by the length of the first. The totals on both sides are now the averages multiplied by the product of the length of the lists, so they are directly comparable. The fourth stage totals the values together and the fifth stage subtracts the totals from each other, while the final stage takes the sign of the result.

\$\endgroup\$
  • \$\begingroup\$ The penultimate stage can be +`1;1 --> ; and the final stage can be D`1. \$\endgroup\$ – Martin Ender Jul 6 '17 at 15:46
  • \$\begingroup\$ @MartinEnder Nice; I've never had reason to use the D stage before! \$\endgroup\$ – Neil Jul 6 '17 at 15:53
1
\$\begingroup\$

PowerShell 69 Bytes

param($n,$m)(($n|measure -A|% A*)-($m|measure -A|% A*))|% T*g "N;M;T"

Outputs N when 'N' is bigger, M for 'M', and T for a Tie.

gets the average using the builtin Measure-Object -Average command.

PS C:\Users\sweeneyc\Desktop> $t = @((7),(6)
(4,5),(4,4)
(2,3,4),(4,5,6)
(4,1,3),(7,3,2,1,1,2)
(100,390,1),(89,82,89)
(92,892),(892,92)
(10,182),(12,78,203,91))

for ($a=0;$a-lt14;$a+=2){
    .\avg.ps1 -a $t[$a] -b $t[$a+1]
}
A
A
B
T
A
T
T
\$\endgroup\$
1
\$\begingroup\$

C# (.NET Core), 40 + 18 = 58 bytes

using System.Linq;n=>m=>n.Average().CompareTo(m.Average())

Try it online!

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

Braingolf, 20 bytes

VRlm&+vlM&+vmc/v/c-s

Try it online!

Takes input multiplied by 1000. This can be increased to a higher power of 10 for better accuracy.

Takes first input as individual integers, and second input as an array of integers.

Outputs 1 if first array's average is larger

Outputs -1 if second array's average is larger

Outputs 0 for tie

Only accurate for averages up to 3 decimal places (so if the average of the first list is 3.5554, and the average of the second list is 3.5557, this will output 0 for a tie)

Explanation

VRlm&+vlM&+vmc/v/c-s  Implicit input from command-line args
                      2nd input as array is inputted to separate stack
VR                    Create stack3, return to stack1
  lm                  Push length of stack, move it to previous stack (wrap to stack3)
    &+                Sum entire stack
      v               Switch to stack2
       lM             Push length of stack, move it to next stack (stack3)
         &+           Sum entire stack
           v          Switch to stack3
            m         Move last item back to stack2
             c        Collapse stack3 into stack1
              /       Divide
               v      Switch to stack2
                /     Divide
                 c    Collapse into stack1
                  -   Subtract
                   s  Sign, positive becomes 1, negative becomes -1, 0 is not affected.
                      Implicit output of last item on stack
\$\endgroup\$
1
\$\begingroup\$

CJam, 18 16 15 bytes

-1 thanks to @ErikTheOutgolfer

q~{_:+\,d/}%~-g

Input is given as [[1 2 3] [4 5 6]]. Output is -1, 0, or 1.

Explanation:

q    e# Read input: "[[1 2 3] [4 5 6]]"
~    e# Eval:       [[1 2 3] [4 5 6]]
{    e# For each:     [1 2 3]
 _   e#   Duplicate:  [1 2 3] [1 2 3]
 :+  e#   Sum:        [1 2 3] 6
 \   e#   Swap:       6 [1 2 3]
 ,   e#   Length:     6 3
 d/  e#   Float div:  2.0
}%   e# End:        [2.0 5.0]
~    e# Unpack:     2.0 5.0
-    e# Subtract:   -3.0
g    e# Signum:     -1
\$\endgroup\$
  • \$\begingroup\$ You're overusing vectorization, something like q~{_:+\,d/}%~-g would be shorter. \$\endgroup\$ – Erik the Outgolfer Jul 6 '17 at 13:14
  • \$\begingroup\$ why is sum over 1,2,3 the value 2 and sum over 4,5,6 the value 5? (might only be typo in explanation) \$\endgroup\$ – avl42 Oct 27 '17 at 20:44
  • \$\begingroup\$ @avl42 Golfed and fixed. \$\endgroup\$ – Esolanging Fruit Oct 29 '17 at 16:38
1
\$\begingroup\$

Japt v2.0a0, 12 11 10 bytes

Takes input as an array of arrays. Returns -1 for N, 1 for M or 0 for tie.

®x÷ZÊÃrn g

Test it


Explanation

®     Ã

Map over the array.

x

Reduce the current sub-array by addition.

÷ZÊ

Dividing each element in the sub-array by the length (Ê) of the sub-array in the process.

rn

Reduce (r) the main array by subtracting the current value from the current element.

g

Get the sign of the result.

\$\endgroup\$
  • \$\begingroup\$ rn is a nice trick. Do you need the g though? \$\endgroup\$ – ETHproductions Jul 5 '17 at 18:17
  • \$\begingroup\$ @ETHproductions: Yeah, output values need to be consistent. \$\endgroup\$ – Shaggy Jul 5 '17 at 18:34
  • \$\begingroup\$ If g was skippable I could save 6 byes :/ \$\endgroup\$ – CalculatorFeline Jul 5 '17 at 19:23
  • \$\begingroup\$ Oh duh, I'm sorry, I thought g got the first item from a 1-item array... :P \$\endgroup\$ – ETHproductions Jul 5 '17 at 20:09
1
\$\begingroup\$

Tcl, 93 bytes

proc C A\ B {proc M L {expr ([join $L +])/[llength $L].}
expr [set n [M $A]-[M $B]]<0?2:$n>0}

Try it online!

I saved 1 byte returning 2 instead of -1.

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

Perl 5, 44 + 1 (-a) = 45bytes

push@a,eval(join'+',@F)/@F}{say$a[1]<=>$a[0]

Try it online!

Outputs -1 if the first has a greater average, 1 if the second has the greater average, or 0 if the averages are equal.

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

AWK, 69 bytes

{for(i=0;i++<NF;)d=M[NR-1]-(M[NR]+=$i/NF)}z=(NR-1)%2{$0=(d<0)-(d>0)}z

Try it online!

Input is by rows with odd row line numbers corresponding to N and even rows corresponding to M. Output is -1 if N is greater than M. 0 if they are equal, and 1 if M > N. Output only occurs on even rows.

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

Python 3, 44 bytes

Takes two lists n and m. Returns True if the average of n is larger, False if that of m is, and throws a ZeroDivisionError if they are equal.

lambda n,m:1/(sum(n)/len(n)-sum(m)/len(m))>0

Try it online!

\$\endgroup\$
  • 2
    \$\begingroup\$ Does not work for ties. Erros may not be thrown as a form of output \$\endgroup\$ – Mr. Xcoder Jul 5 '17 at 20:26
  • \$\begingroup\$ @Mr.Xcoder Am I interpreting this incorrectly? Specifically "Programs may output to STDERR" and "Functions may output via the same methods as full programs". \$\endgroup\$ – C McAvoy Jul 5 '17 at 23:04
  • 1
    \$\begingroup\$ Your forms of output must be constant. You cannot use both STDOUT and STDERR. \$\endgroup\$ – Mr. Xcoder Jul 6 '17 at 6:11
  • \$\begingroup\$ @Mr.Xcoder Got it, I'll keep it in mind. Thanks for the help. \$\endgroup\$ – C McAvoy Jul 6 '17 at 13:43
-1
\$\begingroup\$

Python 3, 87 101 84 Bytes

def c(M,N):
  j=sum(N)/len(N)-sum(M)/len(M);print('N')if j>0else print('MT'[j==0])

input is standard Python lists called M and N Output is name of larger list or T for tie.

Try it online!

Explanation

Get an int to represent the comparison.

Compare in nested ternary.

Thank you @Morgan Thrapp for the improvements!

\$\endgroup\$
  • 1
    \$\begingroup\$ Welcome to PPCG! Where are you taking the input? Right now it looks like you're assuming that the lists are hard coded. This is generally not allowed, see this meta post for more details. Also, you can save a few bytes by removing the whitespace after numbers/parenthesis and using the implicit conversion between boolean and int to index a string in your print call, instead of writing out full print calls/ternaries. Eg, print('N') if j>0 else print('TM'[j==0]) \$\endgroup\$ – Morgan Thrapp Jul 7 '17 at 15:07

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.