Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Calculate the Progressive MeanTM

Disclaimer: This challenge is inspired by a coding error I once made.


Okay, time for a maths lesson. A normal mean average looks like this:

Work out the sum of all numbers in a list
then divide by the size of the list.

But what if we don't know all the numbers at the time we're working out the average? We need a way to work out the average which can be added to over time. For this reason, I present the algorithm for a Progressive MeanTM

The running total is the first number in the list
For each of the remaining numbers
 Add the number to the running total
 Divide the running total by two

So in effect we're averaging each number with the current average. (We could add to this later and get the same result)

BUT

This doesn't give the same result at all. It gives an average, but it differs from the standard methodology for finding the mean. Now the order of the list of numbers is significant.

Of course, being a curious type, I want to work out if the Progressive MeanTM tells us anything about the order of our list of numbers. So for this reason I want to compare Mean with Progressive MeanTM by means of a simple subtraction:

trend = Progressive MeanTM - Standard Mean

The Challenge

  • Write a piece of code which accepts a list of numbers (in any format) which then calculates three pieces of information about it:
    • Standard Mean
    • Progressive MeanTM
    • Trend (Progressive - standard)
  • Work in any language you like.
  • It's golf, attempt to do the challenge in as few bytes as you can.
  • Avoid Standard Loopholes
  • I want the output to be human-readable numbers.
  • Please include a link to an online interpreter such as tio.run

Test Cases:

[1,2,3]
Normal Mean: 2.0
Progressive Mean: 2.25
Trend: 0.25
[3, 2, 1]
Normal Mean: 2.0
Progressive Mean: 1.75
Trend: -0.25
[10, 20, 30]
Normal Mean: 20.0
Progressive Mean: 22.5
Trend: 2.5
[300, 200, 100]
Normal Mean: 200.0
Progressive Mean: 175.0
Trend: -25.0
[10, 100, 10]
Normal Mean: 40.0
Progressive Mean: 32.5
Trend: -7.5
[4, 4, 9, 8, 1, 8, 6, 9, 1, 1]
Normal Mean: 5.1
Progressive Mean: 2.62890625
Trend: -2.4710937499999996
[1, 1, 1, 4, 4, 6, 8, 8, 9, 9]
Normal Mean: 5.1
Progressive Mean: 8.5390625
Trend: 3.4390625000000004
[9, 9, 8, 8, 6, 4, 4, 1, 1, 1]
Normal Mean: 5.1
Progressive Mean: 1.47265625
Trend: -3.6273437499999996

Answer*

Draft saved
Draft discarded
Cancel
2
  • 4
    \$\begingroup\$ For anyone else wondering why the example [a,b,c,d] -> [a,b,c,c,d,d,d,d] doesn't match the explanation of the code, it actually transforms [a,b,c,d] to [a,b,b,c,c,c,c,d,d,d,d,d,d,d,d,a] (so two of each value in comparison to the example). The example confused me for a moment. But nice answer, +1 from me! I also like that it has all these different types of the letter 'A' in the code: āÅÅAÂÆª. xD \$\endgroup\$ Commented Feb 3, 2020 at 14:32
  • 1
    \$\begingroup\$ @KevinCruijssen I changed the code to match the explanation (same byte count). \$\endgroup\$ Commented Feb 3, 2020 at 14:46

AltStyle によって変換されたページ (->オリジナル) /