मेरी सूचियों के औसत की तुलना करें


18

एक सूची का औसत प्राप्त करना (उदाहरण के लिए [2,6,7])

  • सूची की लंबाई प्राप्त करें: [2,6,7] -> 3
  • सूची में संख्याओं को जोड़ो 2 + 6 + 7 = 15:।
  • योग को उनकी गिनती से विभाजित करें 15 / 3 = 5:।

यदि एन का उच्च औसत है, तो एक मान का औसत औसत है, अगर एम का उच्च औसत है, और टाई के मामले में एक और एक है, तो आपको सकारात्मक पूर्णांक एन और एम की दो सूचियों के औसत की तुलना करनी चाहिए ।


I / O नियम

सभी मानक इनपुट और आउटपुट विधियों की अनुमति है।

इनपुट

आप इनपुट को दो अलग-अलग सूचियों, एक नेस्टेड सूची, या किसी अन्य चीज के रूप में ले सकते हैं जिसे आप कार्य के लिए उपयुक्त मानते हैं। कृपया प्रारूप निर्दिष्ट करें।

उत्पादन

प्रदान किए गए मान अलग-अलग होने चाहिए और उनमें कम से कम एक गैर-व्हाट्सएप चरित्र शामिल होना चाहिए। इसके अलावा, उन्हें रन ( एन के लिए एक एकल मान, एम के लिए एक एकल मूल्य, टाई के लिए एक एकल मूल्य ) के बीच सुसंगत होना चाहिए । कृपया अपने उत्तर में उन को निर्दिष्ट करें। मान गैर-रिक्त स्ट्रिंग्स, बूल मान, पूर्णांक, या आपके द्वारा उपयुक्त कुछ भी हो सकते हैं।


ऐनक

  • जरूरी नहीं कि सूचियों की लंबाई समान हो।

  • आपको गारंटी दी जाती है कि सूचियाँ गैर-रिक्त हैं।


परीक्षण के मामलों

मैंने मूल्यों को चुना N wins, M winsऔर Tie, जो बहुत अधिक स्पष्ट हैं।

एन, एम -> आउटपुट (लाभ)

[[], [६] -> एन जीत (एन में 6, एम में ६ है)
[४,५], [४,४] -> एन जीत (एन में ४.५ है, एम में ४ है)
[२,३,४], [४,५,६] -> एम जीत (एन में ३, एम में ५ है)
[4,1,3], [7,3,2,1,1,2] -> टाई (दोनों में 2.666 ...)
[100,390,1], [89,82,89] -> एन जीत (एन में 163.666 है ..., एम में 86.666 है ...)
[92,892], [892,92] -> टाई (सूचियां मूल रूप से समान हैं) 
[१०,१,२], [१२,,,,२०, ९ १] -> टाई (दोनों में ९ ६ हैं)

डिफ़ॉल्ट ढीले लागू होते हैं। स्पष्टीकरण को प्रोत्साहित किया जाता है! यह , इसलिए बाइट्स में सबसे छोटा कोड जीतता है!



अगर मेरी पसंद की भाषा केवल पूर्णांकों का समर्थन करती है, तो क्या मैं इनपुट को 1000 से गुणा कर सकता हूं? इस तरह से गणना की औसत अभी भी 3 दशमलव स्थानों के लिए सटीक होगी
स्काइडसाइड

@ माय्यूब हां, यह अनुमति है
श्री एक्सकोडर

हमें कम से कम 1 वर्ण का आउटपुट वापस करना होगा। क्या इसका मतलब है कि हमें एक चरित्र या एक स्ट्रिंग वापस करने की आवश्यकता है? या क्या आप ऐसे आउटपुट से मतलब करते हैं जिसका स्ट्रिंग मान कम से कम 1 वर्ण का हो?
ओलिवियर ग्राएगोयर

@ OlivierGrégoire दिया गया आउटपुट कम से कम 1 वर्ण लंबा होना चाहिए (आप एक रिक्त स्ट्रिंग नहीं लौटा सकते, लेकिन कम से कम 1 वर्ण के किसी भी स्ट्रिंग को लौटा सकते हैं, और किसी भी गैर-व्हाट्सएप चरित्र को भी)। यह आप पर निर्भर करता है।
मिस्टर एक्सकोडर

जवाबों:



15

मैथेमेटिका, 15 बाइट्स

Order@@Mean/@#&

इसे ऑनलाइन आज़माएं!

Functionजो दो सूचियों की सूची की अपेक्षा करता है। Mean/@#इनपुट में प्रत्येक सूची का अंकगणित माध्य लेता है, फिर उन साधनों को पास किया जाता है Order, जो -1यदि पहली सूची जीतता है, 0यदि कोई टाई है, और 1यदि दूसरी सूची जीत जाती है, तो वापस आ जाती है।


7

जावास्क्रिप्ट (ईएस 6), 52 50 बाइट्स

(@ बग्घी के लिए 2 बाइट्स को सहेजा गया।)

यहाँ दो 50-बाइट समाधान हैं:

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

रिटर्न इन्फिनिटी एन, के लिए -Infinity M के लिए, और NaN एक टाई के लिए।

पहले समाधान को पुनरावृत्ति के कारण स्पष्टीकरण की आवश्यकता हो सकती है:

फ़ंक्शन के पहले कॉल पर, सरणी aके औसत के रूप में आरंभीकृत किया जाता है N:

a=eval(N.join`+`)/N.length

M इस बिंदु पर एक मान है, इसलिए सशर्त अभिव्यक्ति का पहला भाग कहा जाता है:

M ? (a-f(M))/0 : a  ----------    

फ़ंक्शन इस अभिव्यक्ति के भीतर कहा जाता है, इस समय के Mलिए प्रतिस्थापित कर रहा हैN

फ़ंक्शन के लिए इस दूसरी कॉल पर, aऔसत के रूप में शुरू किया जाता है N-जो थाM कि पिछली कॉल में था।

चूंकि फ़ंक्शन के लिए इस कॉल के दौरान कोई दूसरा पैरामीटर नहीं है, सशर्त अभिव्यक्ति का दूसरा भाग चालू होता है, जो औसत लौटाता है:

M ? (a-f(M))/0 : a  --

अब हम अभिव्यक्ति को बेहतर तरीके से समझ सकते हैं:

(a - f(M)) / 0

आईटी इस:

(the average of N  minus  the average of M) divided by 0

औसत के बीच का अंतर एक सकारात्मक संख्या, एक नकारात्मक संख्या या 0 होगा।

इन्फिनिटी , -इनफिनिटी , या NaN में 0 परिणामों के अंतर को विभाजित करना के रूप में आवश्यक तीन अलग मान प्रदान -।

परीक्षण के मामलों:


क्या आप Aफंक्शन पैरामीटर्स पर जाकर कुछ बाइट्स बचा सकते हैं ?
झबरा


5

MATL , 8 बाइट्स

Soooo कई संशोधक ( Yऔर Z)। मुझे इसे छोटा बनाने का कोई तरीका नहीं मिल रहा है। sum / number_of_elementsतीन बाइट्स है। यह करने के लिए एक बेहतर तरीका हो सकता है -ZS, लेकिन मुझे एक नहीं मिल सकता है।

YmiYm-ZS

इसे ऑनलाइन आज़माएं!

           % Take first input implicitly
Ym         % Mean of that input
  i        % Grab second input
   Ym      % Mean of that input
     -     % Subtract
      ZS   % Sign

रिटर्न 1अगर पहला इनपुट बड़ा है, 0वे टाई, और अगर -1दूसरे इनपुट बड़ा है अगर।





3

ऑक्टेव , 27 बाइट्स

@(x,y)sign(mean(x)-mean(y))

इसे ऑनलाइन आज़माएं!

x.yइनपुट के रूप में दो वैक्टर लेते हैं, meanदोनों वैक्टर को लेते हैं , और एक को दूसरे से घटाते हैं। पाने के लिए 1, 0और -1तीन अलग-अलग विकल्पों के लिए इस पर हस्ताक्षर करें ।



3

APL (Dyalog) , 11 बाइट्स

दो सूचियों की सूची के लिए संकेत। प्रिंट 1यदि बाईं ओर का औसत औसत है, तो यदि उनके पास समान औसत है, और ¯1यदि दाएं का औसत औसत है।

×-/(+/÷≢)¨⎕

इसे ऑनलाइन आज़माएं!

 प्रेरित करना

( प्रत्येक के लिए निम्नलिखित tacit फ़ंक्शन लागू करें:

+/ योग

÷ द्वारा विभाजित

 टैली

-/ उनके बीच एक ऋण डालें (मूल्यांकन करें)

× Signum


3

जावास्क्रिप्ट, 81 66 58 56 बाइट्स

ल्यूक के लिए धन्यवाद 15 बाइट्स बचाए

जस्टिन मेरिनर की बदौलत 2 बाइट्स बचाए

n=>m=>Math.sign((a=b=>eval(b.join`+`)/b.length)(m)-a(n))

टाई 0 है, एम 1 है और एन -1 है। करी सिंटैक्स का उपयोग करके कॉल किया जाता है, जैसे।f([7])([6])


1
आप इसे कुछ बाइट्स द्वारा सुधार सकते हैं: आप चर असाइनमेंट को हटा सकते हैं, आप करी सिंटैक्स का उपयोग कर सकते हैं, आप अलर्ट को हटा सकते हैं, और आप आसानी से एरेज़ का उपयोग करके योग कर सकते हैं eval(a.join`+`)a=>(b=a.map(c=>eval(c.join`+`)/c.length))[0]-b[1]?b[0]>b[1]:061 बाइट्स के लिए। यह सरणियों के एक सरणी के रूप में इनपुट लेता है, और एम के लिए और एन के 0लिए एक टाई के लिए आउटपुट ।truefalse
ल्यूक

आप इसे अपने उत्तर के रूप में क्यों पोस्ट नहीं करते?
सुपरस्टॉर्मर

1
आप aपहली बार उपयोग किए जाने वाले फ़ंक्शन ( ) का उपयोग करके दो और बाइट्स बचा सकते हैं n=>m=>Math.sign((a=b=>eval(b.join`+`)/b.length)(m)-a(n)):।
जस्टिन मेरिनर


3

हास्केल, 65 43 बाइट्स

22 बाइट्स को सहेजने के लिए धन्यवाद nimi!

a x=sum x/sum[1|_<-x] 
x#y=compare(a x)$a y

वहाँ एक बेहतर तरीका होना चाहिए ... लेकिन प्रकार रूपांतरणों ने मुझे खराब कर दिया।

प्रयोग

(#) [7] [6]

रिटर्न GTयदि पहला तर्क जीत, LTअगर दूसरा तर्क जीतता है, और EQअगर वे टाई।

इसे ऑनलाइन आज़माएं!


1
के sum$xसाथ कास्ट करने की जरूरत नहीं है f.। इसके अलावा: के length xसाथ प्रतिस्थापित किया जा सकता है sum[1|_<-x], ताकि आप fपूरी तरह से छुटकारा पा सकें a x=sum x/sum[1|_<-x]:।
नीमी

बहुत सुंदर! ऐसा करने के लिए भी नहीं सोचा था।
हेनरी

1
... ओह और में #: ...(a x)$a y
नीमी

1
... और भी बेहतर: अपने मुख्य कार्य के साथ पॉइंटफ्री करें, फिर आप इसके लिए नाम भी बचा सकते हैं (.a).compare.a:। उपयोग: ( (.a).compare.a ) [7] [6]
नीमी

2
एक और: [1|_<-x]जैसा है (1<$x)
निमि

3

जे, 10 बाइट्स

*@-&(+/%#)

एक सूची बाईं ओर, एक दाईं ओर दी गई है। _1 लौटाएगा यदि बाएं औसत छोटा है, 1 यदि यह बड़ा है, और 0 यदि वे समान हैं

  • (+/%#) किसी सूची के औसत की गणना के लिए एक मानक J कांटा है
  • &डाइएडिक कांटा पर एक भिन्नता प्रदान करता है। यह दोनों तर्कों में दाईं ओर (औसत क्रिया, इस मामले में) लागू होता है, और फिर उन्हें बाईं ओर क्रिया के साथ गुजरता है, जो इस मामले में है ...
  • *@- "साइन" के बाद घटाएँ: इसलिए दाईं ओर बाईं ओर से घटाया जाता है, और हमें परिणाम का संकेत दिया जाता है - _1, 1, या 0

3

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.

Try It Online


You can save a byte by replacing h.+ with -F
isaacg

3

TI-Basic, 25 21 13 12 10 bytes

-2 bytes thanks to lirtosiast

:tanh(ᴇ9mean(L₁-mean(L₂

2
This can be golfed by using Ans instead of C: mean(L₁)>mean(L₂:Ans+3(mean(L₁)=mean(L₂, 21 bytes.
Scott Milner

You can remove the ( and the ).
lirtosiast

2

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

Try it online!

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)

I knew Jelly would be pretty good at this challenge, I just don't know the language very well. Well done on beating me :P
Okx

I'm not 100% sure this is not possible in, say, 5...!
Jonathan Allan

@JonathanAllan I am? Basically to get the averages you map the average function, which isn't a builtin yet, so you use the shortest (I suppose) counterpart, S÷L, and then you convert it to single-link via S÷¥L$ which can be shortened to S÷Lµ since it's at the very start of the program and then you put an right there to map and then since there's no builtin for comparing you'd use _/Ṡ but you can shorten to IṠ since that's still 3 distinct cmp outputs...yeah, pretty sure it can't be done in 5. Also 5 won't help since I FGITW'd. :)
Erik the Outgolfer

2

Perl 6, 25 bytes

{sign [-] .map:{.sum/$_}}

Try it online!

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.


2

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.


2

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.


1
You can probably save some bytes by replacing Math.sign(y-x) by y-x?x>y:0. Outputs 0 for Tie, true for N and false for M.
Luke

1
x-y&&x>y perhaps?
Neil

@Neil Nice, even better
Justin Mariner

2

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 variable a.
  • #*a
    Map # to the function's argument, getting the lengths of the sublists. Result: [3 4]
  • a/#*a
    Divide (the elements of) the sublists of a by 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 on CM, which gives -1, 0, or 1 depending on the comparison of its two operands (like Python's cmp). Result: -1 (because 3 is smaller than 3.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$+*_/#*_.


2

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.


Is taking length as an argument within the specs? Cuts significant code from a lot of these if it is.
Henry

I don't know if there's another way for C to retrieve the length of an array. The length itself is more like an intrinsic part of array.
Keyu Gan

2

Brachylog, 8 bytes

⟨+/l⟩ᵐ-ṡ

Try it online!

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

2

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.


Since "anything can be an input", just use Streams directly, like I did.
Olivier Grégoire

2

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.


1
Is this an anonymous function that can be called without assignment? I don't know R but if it is you don't need the f=.
Post Rock Garf Hunter

@WheatWizard you are correct; additionally you can remove the {} from the function body.
Giuseppe

Thanks for the input. It's my first attempt at codegolf.
zelite

2

MATL, 6 bytes

Don't be so mean!*

!-ssZS

Input stack order:

M
N

Output:

 1 = N wins  
-1 = M wins  
 0 = tie

Try it online!

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


2

Java (OpenJDK 8), 76 62 bytes

a->b->Math.signum(a.average().orElse(0)-b.average().orElse(0))

Try it online!

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!

The way you've chosen to take the input is really clever!
David Conrad

1
@DavidConrad I actually had the long version of this answer since yesterday (just prepend java.util.Arrays.stream(array).map(java.util.Arrays::stream)). It's only when I re-read the question today that I thought this input format is as valid as any.
Olivier Grégoire

1
Would .orElse(0) be a viable shortening of .getAsDouble()?
Zircon

1
If you instead just take 2 streams for input, (a,b)->Math.signum(a.average().orElse(0)-b.average().orElse(0)); is 64 bytes
Xanderhall

These are only good ideas, guys! Continue :p
Olivier Grégoire

1

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


हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.