| Bytes | Lang | Time | Link |
|---|---|---|---|
| 057 | AWK | 250905T163454Z | xrs |
| 004 | Thunno 2 | 230714T084639Z | The Thon |
| 029 | Arturo | 230221T152840Z | chunes |
| 024 | Juby | 221214T205543Z | Jordan |
| 008 | Japt v2.0a0 | 170705T180622Z | Shaggy |
| 004 | Husk | 201017T173622Z | LegionMa |
| 093 | Tcl | 170705T182939Z | sergiol |
| 069 | AWK | 180112T161433Z | Robert B |
| nan | Perl 5 | 171031T014319Z | Xcali |
| 015 | CJam | 170705T191006Z | Esolangi |
| 010 | TIBasic | 170705T214504Z | Oki |
| 084 | Python 3 | 170706T165047Z | Simon |
| 062 | Java OpenJDK 8 | 170707T120904Z | Olivier |
| 020 | Braingolf | 170707T103512Z | Mayube |
| 006 | Pyth | 170705T183941Z | jacoblaw |
| 006 | MATL | 170706T173002Z | beaker |
| nan | C# .NET Core | 170706T214508Z | raznagul |
| 069 | PowerShell 69 Bytes | 170706T171017Z | colsw |
| 066 | Retina | 170705T191028Z | Neil |
| 034 | R | 170706T121325Z | zelite |
| 099 | SQL MS TSQL | 170706T130530Z | steenber |
| 103 | Gallina | 170706T122138Z | Biv |
| 105 | Java | 170706T115345Z | Xanderha |
| 050 | JavaScript ES6 | 170705T221520Z | Rick Hit |
| 075 | C# | 170706T095648Z | TheLetha |
| 040 | Haskell | 170706T095010Z | Sergii M |
| 008 | Brachylog | 170706T081112Z | Fatalize |
| 010 | J | 170705T231410Z | Jonah |
| 071 | Common Lisp | 170705T224106Z | ceilingc |
| 027 | Julia | 170705T185248Z | Uriel |
| 043 | Haskell | 170705T204823Z | Henry |
| 009 | 05AB1E | 170705T212437Z | Emigna |
| 9198 | C gcc | 170705T181005Z | Keyu Gan |
| 013 | Pip | 170705T201935Z | DLosc |
| 068 | PHP | 170705T200730Z | Jör |
| 044 | Python 3 | 170705T195408Z | C McAvoy |
| 056 | Javascript | 170705T182639Z | SuperSto |
| 054 | JavaScript ES6 | 170705T183653Z | Justin M |
| 005 | Actually | 170705T182029Z | Erik the |
| 060 | JavaScript ES6 | 170705T185343Z | Luke |
| 014 | Dyalog APL | 170705T182209Z | Uriel |
| 025 | Perl 6 | 170705T183454Z | Sean |
| 015 | Mathematica | 170705T183432Z | user6198 |
| 043 | Python 2 | 170705T182439Z | Rod |
| 011 | APL Dyalog | 170705T182330Z | Adá |
| 049 | Python 2 | 170705T181938Z | mbomb007 |
| 027 | Octave | 170705T181357Z | Stewie G |
| 008 | MATL | 170705T181758Z | Stewie G |
| 007 | Jelly | 170705T180148Z | Jonathan |
| 021 | Mathematica | 170705T175855Z | Calculat |
Thunno 2, 4 bytes
€mẸṛ
Gives 1 for N wins, 0 for tie, and -1 for M wins.
Explanation
€mẸṛ # Implicit input
€m # Mean of each
Ẹṛ # Compare them
# Implicit output
J-uby, 24 bytes
Returns 1 if the first list has the greater average, -1 if the second list, or 0 for a tie.
:*&:/%[:sum|Q,:+@]|+:<=>
Explanation
:* & :/ % [:sum | Q, :+@] | +:<=>
:* & # Map arguments with...
:/ % [ , ] # Quotient of...
:sum | Q # Sum of list converted to float and
:+@ # Length of list
| +:<=> # Then compare
Japt v2.0a0, 12 11 10 8 bytes
Takes input as a 2D array. Returns -1 for M, 1 for N or 0 for tie.
®x÷ZÊÃrg
®x÷ZÊÃrg :Implicit input of 2D array
® :Map each Z
x : Reduce by addition
÷ : Divide by
ZÊ : Length of Z
à :End map
r :Reduce by
g : Sign of difference
Husk, 4 bytes
±¤-A
Try it online! Outputs -1 if N wins, 1 if M wins, and 0 if they are tied. Just takes the sign (±) of the difference (-) between the averages (¤..A) of the two lists.
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}
I saved 1 byte returning 2 instead of -1.
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
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.
Perl 5, 44 + 1 (-a) = 45bytes
push@a,eval(join'+',@F)/@F}{say$a[1]<=>$a[0]
Outputs -1 if the first has a greater average, 1 if the second has the greater average, or 0 if the averages are equal.
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
TI-Basic, 25 21 13 12 10 bytes
-2 bytes thanks to lirtosiast
:tanh(ᴇ9mean(L₁-mean(L₂
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.
Explanation
Get an int to represent the comparison.
Compare in nested ternary.
Thank you @Morgan Thrapp for the improvements!
Java (OpenJDK 8), 76 62 bytes
a->b->Math.signum(a.average().orElse(0)-b.average().orElse(0))
Since the input can be anything, I decided to take IntStreams as input. You can get such an input from a standard int[] with Arrays.stream(array).
The output is 1 for "N wins", -1 for "M wins", and 0 for tie.
Saves
- -14 bytes from insights of both @Zircon and @Xanderhall!
Braingolf, 20 bytes
VRlm&+vlM&+vmc/v/c-s
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
Pyth, 10 8 7 6 bytes
Thanks @isaacg for saving a byte
._-F.O
Input is taken as a nested list, [N, M]. Outputs -1 if N < M, 1 if N > M and 0 if they are equal.
MATL, 6 bytes
Don't be so mean!*
!-ssZS
Input stack order:
M
N
Output:
1 = N wins
-1 = M wins
0 = tie
!-ssZS
========
! % transpose M
- % N - M^T using elementwise subtraction and implicit expansion
s % sum columns of the result
s % sum the resulting row vector
ZS % sign of the sum
*This answer was golfed without being mean to any poor, defenseless numbers.
C# (.NET Core), 40 + 18 = 58 bytes
using System.Linq;n=>m=>n.Average().CompareTo(m.Average())
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
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.
R 38 34 bytes
function(a,b)sign(mean(a)-mean(b))
Function that takes as input two numeric vectors. Returns 1 if first list average is higher, 0 if they are the same and -1 if second list average is higher.
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.
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').
Java, 105 bytes
s->s.stream().map(l->l.stream().reduce((i,j)->i+j).get()/l.size()).reduce((i,j)->Math.signum(i-j)).get();
Lambda that takes a nested list, as per allowable inputs.
Streams the list of lists, converts both to their averages, then returns the sign of the difference. 1 if the first list is larger, -1 if the second list is larger, 0 for a tie.
JavaScript (ES6), 52 50 bytes
(Saved 2 bytes thanks to @Shaggy.)
Here are two 50-byte solutions:
f=(N,M,a=eval(N.join`+`)/N.length)=>M?(a-f(M))/0:a
(N,M,A=a=>eval(a.join`+`)/a.length)=>(A(N)-A(M))/0
Returns Infinity for N, -Infinity for M, and NaN for a tie.
The first solution may require a bit of explanation due to the recursion:
On the first call to the function, a is initialized as the average of the N array:
a=eval(N.join`+`)/N.length
M has a value at this point, so the first part of the conditional expression is called:
M ? (a-f(M))/0 : a
----------
The function is called within this expression, this time substituting M for N.
On this second call to the function, a is initialized as the average of N –– which was M in the previous call.
Since there is no second parameter during this call to the function, the second part of the conditional expression is triggered, which returns the average:
M ? (a-f(M))/0 : a
--
We can now understand the expression better:
(a - f(M)) / 0
It's:
(the average of N minus the average of M) divided by 0
The difference between the averages will be a positive number, a negative number, or 0.
Dividing the difference by 0 results in Infinity, -Infinity, or NaN – providing the three distinct values as required.
Test Cases:
f=(N,M,a=eval(N.join`+`)/N.length)=>M?(a-f(M))/0:a
console.log(f([7], [6] )) // N wins (N has 7, M has 6 )
console.log(f([4,5], [4,4] )) // N wins (N has 4.5, M has 4)
console.log(f([2,3,4], [4,5,6] )) // M wins (N has 3, M has 5)
console.log(f([4,1,3], [7,3,2,1,1,2] )) // Tie (both have 2.666...)
console.log(f([100,390,1], [89,82,89] )) // N wins (N has 163.666..., M has 86.666...)
console.log(f([92,892], [892,92] )) // Tie (lists are basically identical)
console.log(f([10,182], [12,78,203,91] )) // Tie (both have 96)
C#, 75 bytes
using System.Linq;n=>m=>n.Average()>m.Average?1:m.Average()>n.Average()?2:0
Haskell, 40 bytes
f x=sum x/sum(1<$x)
x!y=compare(f x)$f y
Brachylog, 8 bytes
⟨+/l⟩ᵐ-ṡ
Outputs 1 if the first list has a bigger average, -1 is the second list has a bigger average, and 0 if they are tied.
Explanation
ᵐ Map:
⟨ ⟩ Fork:
+ Sum…
/ …divided by…
l …length
- Subtract
ṡ Sign
J, 10 bytes
*@-&(+/%#)
One list given on the left, one on the right. Will return _1 if the left average is smaller, 1 if it's bigger, and 0 if they're equal
(+/%#)is a standard J fork for calculating the average of a list&provides a variation on the dyadic fork. it applies the right side (the average verb, in this case) to both arguments, and then passes them along to the verb on the left side, which in this case is...*@-subtract followed by "sign of": so the right avg is subtracted from the left, and we are given the sign of the result -- _1, 1, or 0
Common Lisp, 74 71 bytes
(defun g(x)(/(apply'+ x)(length x)))(defun f(x y)(signum(-(g x)(g y))))
Julia, 27 bytes
(x,y)->cmp(mean(x),mean(y))
Returns 1 if the first average is larger, -1 if second is, and 0 if they tie.
Haskell, 65 43 Bytes
Saved 22 bytes thanks to nimi!
a x=sum x/sum[1|_<-x]
x#y=compare(a x)$a y
There has to be a much better way... But type conversions screwed me.
Usage
(#) [7] [6]
Returns GT if the first argument wins, LT if the second argument wins, and EQ if they tie.
05AB1E, 9 bytes
1 if M wins, -1 if N wins and 0 for a tie.
vyOyg/}.S
Explanation
v # for each y in list of lists
yO # sum y
yg # get length of y
/ # divide
} # end loop
.S # compare
C (gcc), 91 98 bytes
u,v,j;f(x,y,a,b)int*a,*b;{for(u=v=0;x--;u+=a[x])for(j=0;j<y;)v+=b[j++];j=u*y-v;x=j>0?2:!j;}
Wrong place for C and probably the only answer that doesn't need division. At least the code is displayed without a slider.
Return 0,1,2 for M>N, M=N, M<N respectively. Takes input as length of M, length of N, M, N.
Pip, 13 bytes
{$CM$+*a/#*a}
This is a function that takes a list of lists. Returns 1 if the first average is bigger, -1 if the second is bigger, 0 if tied. Run all test cases here.
Background
This solution makes heavy use of two of Pip's metaoperators:
$, fold. Take a binary operator and apply it between the elements of a list. For instance,+is addition, but$+sums a list. Note that$makes a binary operator into a unary operator.*, map. Take a unary operator and apply it to each element of a list. For instance,#gives the length of a list, but#*gives (a list of) the lengths of the list's items.- These two metaoperators can be combined:
$+*maps fold/plus over a list, summing each of the list's elements.
The other thing to know about Pip is that a lot of operators work item-wise on lists by default. For instance, [1 2 3] * 5 gives [5 10 15]; [1 2 3] * [2 3 4] gives [2 6 12]; and [[1 2] [3 4]] * [5 6] gives [[5 10] [18 24]].
Explanation
We'll use an example input of [[2 3 4] [2 3 4 6]]:
{...}
Defines a function. The (first) argument is bound to the local variablea.#*a
Map#to the function's argument, getting the lengths of the sublists. Result:[3 4]a/#*a
Divide (the elements of) the sublists ofaby their respective lengths. Result:[[0.667 1 1.333] [0.5 0.75 1 1.5]]$+*a/#*a
Map$+(fold on addition) to that result, summing the sublists. Result:[3 3.75]$CM$+*a/#*a
Fold onCM, which gives-1,0, or1depending on the comparison of its two operands (like Python'scmp). Result:-1(because3is smaller than3.75).
You can also define functions in Pip by writing expressions containing the identity function _. For example, _*_ is a function that squares its argument--syntactic sugar for {a*a}, and fewer bytes. However, there's a bug in the current version of the interpreter that prevents _ from working with the * metaoperator. Once that's fixed, this solution can be 11 bytes: $CM$+*_/#*_.
PHP, 68 bytes
<?foreach($_GET as$v)$r[]=array_sum($v)/count($v);echo$r[0]<=>$r[1];
PHP, 69 bytes
<?=($s=array_sum)($a=$_GET[0])/count($a)<=>$s($b=$_GET[1])/count($b);
spaceship operator -1 less then , 0 tie , 1 greater then
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
Javascript, 81 66 58 56 bytes
saved 15 bytes thanks to Luke
saved 2 bytes thanks to Justin Mariner
n=>m=>Math.sign((a=b=>eval(b.join`+`)/b.length)(m)-a(n))
Tie is 0, M is 1 and N is -1. Called using currying syntax, eg. f([7])([6])
JavaScript (ES6), 60 54 bytes
-6 bytes thanks to @Luke and @Neil
(i,[x,y]=i.map(v=>eval(v.join`+`)/v.length))=>y-x&&x>y
Takes input as a 2-element array [N, M]. Outputs true, 0, or false for N, Tie, or M, respectively.
Explanation
(i, // input array: [N, M]
[x,y] = // destructure assignment: set x and y to...
i.map(v=> // the input values mapped as...
eval(v.join`+`) // the sum, by joining the array with +
/ v.length // divided by the length
)
) => y-x && x>y // return 0 for tie, or the result of avg(N) > avg(M)
Test Snippet
Input numbers separated by spaces/commas.
f=
(i,[x,y]=i.map(v=>eval(v.join`+`)/v.length))=>y-x&&x>y
<div oninput="O.value=f([N.value,M.value].map(x=>x.split(/[ ,]+/)))">N <input id=N> M <input id=M></div>
Out: <input id=O disabled>
Actually, 5 bytes
♂æi-s
1 for N > M, 0 for N = M, -1 for N < M.
Explanation:
♂æi-s Takes input in format [N, M]
♂æ Map average
i Dump to stack in reverse
- Subtract
s Get sign
JavaScript (ES6), 60 bytes
a=>(b=(c=a.map(d=>eval(d.join`+`)/d.length))[0])-c[1])?b>0:0
Outputs 0 for Tie, true for N and false for M.
Dyalog APL, 14 bytes
×(-/(+/÷≢)¨∘⊢)
1 if the left is greater, ¯1 if the right is and 0 on tie.
How?
¨∘⊢ for each list
+/÷≢ calculate average (+/ sum ÷ divide by ≢ length)
-/ subtract the averages
× sign of the result
Perl 6, 25 bytes
{sign [-] .map:{.sum/$_}}
Takes a single argument, a two-element list of lists of numbers. Returns 1 if the first list has a greater average, -1 if the second list does, and 0 if the averages are equal.
Mathematica, 15 bytes
Order@@Mean/@#&
Function which expects a list of two lists. Mean/@# takes the arithmetic mean of each list in the input, then those means are passed into Order, which returns -1 if the first list wins, 0 if there is a tie, and 1 if the second list wins.
APL (Dyalog), 11 bytes
Prompts for a list of two lists. Prints 1 if the left has higher average, 0 if they have the same average, and ¯1 if the right has higher average.
×-/(+/÷≢)¨⎕
⎕ prompt
(…)¨ apply the following tacit function to each:
+/ the sum
÷ divided by
≢ the tally
-/ insert (and evaluate) a minus between them
× signum
Octave, 27 bytes
@(x,y)sign(mean(x)-mean(y))
Takes two vectors x.y as input, takes the mean of both vectors, and subtract one from the other. Get the sign of this, to get 1, 0 and -1 for the three different alternatives.
MATL, 8 bytes
Soooo many modifiers (Y and Z). I can't find a way to make it shorter. sum / number_of_elements is three bytes. It might be a better way to do -ZS, but I can't find one.
YmiYm-ZS
% Take first input implicitly
Ym % Mean of that input
i % Grab second input
Ym % Mean of that input
- % Subtract
ZS % Sign
Returns 1 if the first input is larger, 0 if they tie, and -1 if the second input is larger.
Jelly, 7 bytes
S÷Lµ€IṠ
A monadic link accepting a list of the two lists, N,M which returns:
[-1] for N;
[1] for M; and
[0] for a tie.
As a full program it prints the result (single item lists print their content only, so -1, 1, or 0).
How?
S÷Lµ€IṠ - Link: list of lists, [N,M]
µ€ - perform the chain to the left for €ach (of N, M)
S - sum
L - length
÷ - divide (yields the average)
I - incremental differences (yields [avg(M) - avg(N)])
Ṡ - sign (yields: [1] if avg(M)>avg(N); [-1] if avg(N)>avg(M); or [0] if equal)
Mathematica, 21 bytes
Sign[Mean@#-Mean@#2]&
1 for # wins, -1 for #2 wins, 0 for tie.