I've been scrolling around this site for a while, but just recently got really interested in actually trying out some of the challenges. I was intending to try my hand at some of the existing code-golf topics, but I didn't have Internet access for a while yesterday, and in the meantime, I thought up my own challenge.
Your task is to create a program or function that takes an array of Floats a
and an integer n
, then sets each value in a
to the average of the two beside it, n
times. When repeatedly used with increasing values of n
, this generates a wave-like motion:
Specifics:
- If there happens to be only one item in
a
, or ifn
is 0 or less, the program should return the original array. - Inputs and outputs can be in any format you desire, as long as they are visibly separated.
For each step:
- The first item in
a
should become the average of itself and the next item. - The last item in
a
should become the average of itself and the previous item. - Any other item in
a
should become the average of the previous item and the next item. - Make sure you are calculating off the previous step's array and not the current one!
Test cases: NOTE: Your inputs/outputs do not have to be in this format!
[0, 0, 1, 0, 0], 1 -> [0, 0.5, 0, 0.5, 0]
[0, 0, 1, 0, 0], 2 -> [0.25, 0, 0.5, 0, 0.25]
[0, 0, 1, 0, 0], 0 -> [0, 0, 1, 0, 0]
[0, 0, 1, 0, 0], -39 -> [0, 0, 1, 0, 0]
[0, 16, 32, 16, 0], 1 -> [8, 16, 16, 16, 8]
[0, 1, 2, 3, 4, 5], 1 -> [0.5, 1, 2, 3, 4, 4.5]
[0, 64], 1 -> [32, 32]
[0], 482 -> [0]
[32, 32, 32, 16, 64, 16, 32, 32, 32], 4 -> [33, 27, 40, 22, 44, 22, 40, 27, 33]
This is code-golf, so shortest answer in bytes wins. The winner will be chosen in one week (on August 1). Good luck!
Edit: Congrats to the winner, @issacg, with a whopping 18 bytes!
n
is not supplied anda
is not supplied cases - they don't really add much. \$\endgroup\$n
should be 4, not 5. \$\endgroup\$