# Compare the averages of my lists

## 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)

,                  -> 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!

• – Mr. Xcoder Jul 5 '17 at 17:55
• 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 – Skidsdev Jul 7 '17 at 10:28
• @Mayube Yes, that is allowed – Mr. Xcoder Jul 7 '17 at 10:29
• 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? – Olivier Grégoire Jul 7 '17 at 12:14
• @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. – Mr. Xcoder Jul 7 '17 at 12:19

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


# C#, 75 bytes

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


# 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').


# 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.

# Retina, 69 66 bytes

\d+
$* 1(?=.*;(1+,)+) 1$#1$* 1(?<=(,1+)+;.*) 1$#1$* , +1;1 ; D1  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. • The penultimate stage can be +1;1 --> ; and the final stage can be D1. – Martin Ender Jul 6 '17 at 15:46 • @MartinEnder Nice; I've never had reason to use the D stage before! – Neil Jul 6 '17 at 15:53 # 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  # C# (.NET Core), 40 + 18 = 58 bytes using System.Linq;n=>m=>n.Average().CompareTo(m.Average())  Try it online! # 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  ## CJam, 1816 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  • You're overusing vectorization, something like q~{_:+\,d/}%~-g would be shorter. – Erik the Outgolfer Jul 6 '17 at 13:14 • 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) – avl42 Oct 27 '17 at 20:44 • @avl42 Golfed and fixed. – Esolanging Fruit Oct 29 '17 at 16:38 # Japt v2.0a0, 1211 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. • rn is a nice trick. Do you need the g though? – ETHproductions Jul 5 '17 at 18:17 • @ETHproductions: Yeah, output values need to be consistent. – Shaggy Jul 5 '17 at 18:34 • If g was skippable I could save 6 byes :/ – CalculatorFeline Jul 5 '17 at 19:23 • Oh duh, I'm sorry, I thought g got the first item from a 1-item array... :P – ETHproductions Jul 5 '17 at 20:09 # 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.

• – sergiol Oct 29 '17 at 10:45

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

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


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.

# 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.

# 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!

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

# Python 3, 87101 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!

• 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]) – Morgan Thrapp Jul 7 '17 at 15:07