Let's say that this array is how many press-ups I've achieved each day in the last 28 days:
[
20,20,20,30,30,30,30,
35,35,40,40,40,45,45,
50,50,50,50,50,50,50,
60,70,80,90,100,110,120
]
As you can see, it's taken a steep upward trend in the last week, and that's the part of this data I'm most interested in. The further in the past it is, the less I want that data to feature in my 'average' number of press-ups.
To that end, I want to work out an 'average' where each week is worth more than the previous week.
Background information, not part of this problem.
Normal average:
The sum of all values / the number of values
For above:
1440 / 28 = 51.42857142857143
Weighted average:
Split the array into 4 groups of 7, and start up a new array.
- Add the first group to the array.
- Add the second group to the array twice.
- Add the third group to the array thrice.
- Add the fourth group to the array four times.
Sum all of the new array, and divide by the length of the new array.
For above:
Convert the array to this:
[
20,20,20,30,30,30,30, # first week once
35,35,40,40,40,45,45,
35,35,40,40,40,45,45, # second week twice
50,50,50,50,50,50,50,
50,50,50,50,50,50,50,
50,50,50,50,50,50,50, # third week thrice
60,70,80,90,100,110,120,
60,70,80,90,100,110,120,
60,70,80,90,100,110,120,
60,70,80,90,100,110,120 # Fourth week four times
]
Then run a normal average on that array.
4310 / 70 = 61.57142857142857
Note that it's higher than the normal average value because of the upward trend in the last week.
The rules:
- The input is a flat array of 28 nonnegative integers.
- Any language you'd like to write in.
- Output a number.
- I always like to see TIO links.
- Try to solve the problem in the smallest number of bytes.
- The result should be a decimal accurate to at least 4 decimal places (either truncated or rounded up from the test case values is fine) or an exact fraction.
Test cases:
Case 1: Upward trend
[
20,20,20,30,30,30,30,
35,35,40,40,40,45,45,
50,50,50,50,50,50,50,
60,70,80,90,100,110,120
]
Normal average: 51.42857142857143 Weighted average: 61.57142857142857
Case 2: Leaving the lull behind
(I had a bad week, but it was a while ago)
[
50,50,50,50,50,50,50,
10,10,10,10,10,10,10,
50,50,50,50,50,50,50,
50,50,50,50,50,50,50
]
Normal average: 40 Weighted average: 42
Case 3: Giving up
I had a bad week, it's pulling my average down fast.
[
50,50,50,50,50,50,50,
50,50,50,50,50,50,50,
50,50,50,50,50,50,50,
10,10,10,10,10,10,10
]
Normal average: 40 Weighted average: 34
Case 4: Averaging out
Okay, so I'm just playing around here, I thought it might be the same value for the normal and weighted averages, but, of course, it was not.
[
60,60,60,60,60,60,60,
30,30,30,30,30,30,30,
20,20,20,20,20,20,20,
15,15,15,15,15,15,15
]
Normal average: 31.25 Weighted average: 24.0
Bonus problem:
What combination of 28 values would have the same normal average and weighted average?
Happy golfing!
new_avg = α*weekly_sum + (1-α)*old_avg
for someα∈(0,1)
\$\endgroup\$0
press-ups every day, so my weighted average is the same as my normal average. \$\endgroup\$