Restricted average (arithmetic mean) – i.e. without obvious built-ins

-2

Introduction

The arithmetic mean is defined as being equal to the sum of the numerical values of each and every observation divided by the total number of observations. Symbolically, if we have a data set containing the values a1,…,an. The arithmetic mean A is defined by the formula

A=\frac{1}{n}\sum_{i=1}^n a_i.

Objective

The challenge here is, given a non-empty list of observations, to calculate the arithmetic mean without any built-ins for mean, sum, division, or count. All other operations are allowed, e.g. median, product, multiplication, and sequence generation.

If your language cannot process lists without knowing the number of elements it has, you may use a counting built-in for such (looping, loading, etc.) purpose only, but not for the actual computation.

Test cases

[5]5
[1,-2]-0.5
[6,1,3]3.333333333

If your language can handle complex numbers:

[5,1+3i]3+1.5i

Adám

Posted 2016-05-18T07:11:02.127

Reputation: 37 779

Question was closed 2016-05-18T15:17:24.977

This is a cut-and-dry Do X without Y challenge.

– Mego – 2016-05-18T07:24:02.353

1

Possible duplicate of Operations with Lists

– Mego – 2016-05-18T07:25:21.530

@Mego How can it be a dup if the other one isn't a Do X without Y? – Adám – 2016-05-18T08:11:59.337

The core challenge is the same, and the "without Y" part doesn't significantly distinguish it. – Mego – 2016-05-18T08:12:54.543

@Mego No the core here isn't list operations. It is alternative ways to do things. – Adám – 2016-05-18T08:14:16.477

If that were true, that would mean this is a chameleon challenge, which is similarly bad.

– Mego – 2016-05-18T08:20:43.740

Compute the mean without even knowinf how many things there are? I don't even know if this is possible... – Leaky Nun – 2016-05-18T08:20:51.650

@Mego What? Isn't the title pretty clear? – Adám – 2016-05-18T08:25:29.557

What if my language can only handle integers? – Leaky Nun – 2016-05-18T08:26:06.580

@KennyLau It is. Possible? Why not? Other array/list operations are allowed. – Adám – 2016-05-18T08:27:06.477

Such as enumerate? – Leaky Nun – 2016-05-18T08:27:40.137

@KennyLau Then Either round/trunc/floor to int and/or use sample input that is 1000 times bigger! – Adám – 2016-05-18T08:28:20.133

@KennyLau If "enumerate" means generating the indices of the list then, yes, max(enumerate(input)) would give you the length. – Adám – 2016-05-18T08:31:45.633

If I only use string manipulations to do that? (Further question of the "what if I only use integers") – Leaky Nun – 2016-05-18T08:37:36.887

@KennyLau Again, only 1. mean, 2. sum, 3. division, and 4. counting are prohibited. E.g. median, product, multiplication, and sequence generation are all permitted. – Adám – 2016-05-18T08:44:05.310

@Mego Can you explain why this is unclear. You may dislike this (type of) question, but what is unclear about it? – Adám – 2016-05-18T19:18:31.010

I did not vote to close as unclear - I voted to close as duplicate. – Mego – 2016-05-18T19:30:21.590

@Mego Huh?! so why does the system say put on hold as unclear what you're asking by Mego, xnor, ...? – Adám – 2016-05-18T19:36:24.623

The on hold message shows the close reason that the majority of the close voters chose. – Mego – 2016-05-18T19:44:15.473

Does evaluating an array as a scalar (which yields its count in Perl) count as using a built-in for count? – msh210 – 2016-05-18T21:48:11.783

Answers

1

Pyth, 13 bytes

l@*F^L2Qhe.ek

Try it online!

Uses exponential arithmetic to replace sum and division.

Uses enumerate to find number of elements.

Leaky Nun

Posted 2016-05-18T07:11:02.127

Reputation: 45 011

Perfect, that's the type of substitutions I had in mid. – Adám – 2016-05-18T08:38:15.590

1

Retina, 46 37 bytes

+`x;
;x
;x
;;:x
;
x
^(x+):(\1)*x*
$#2

Try it online!

It's quite a trouble not to use arithmetic...

Leaky Nun

Posted 2016-05-18T07:11:02.127

Reputation: 45 011