Background
It was a normal presentation that I were in as a audience, until the presenter gave a math problem about repeat taking 2 number out of a list a replacing them with average, claiming that there will be something special about it, and our math teacher, sitting at the end of classroom, exciting rushed to me, and tasked me to code that out.
Task
2 input length
and generation
Generate a range, start from 0, with length of
length
Randomly choose 2 number (or item)
X
andY
and replace bothX
andY
with the average ofX
andY
Repeat step 2 for
generation
timesOutput minimum, maximum, average, and the processed list.
Example
I'll let this semi-golfed python code explain itself :)
import random
a,b=map(int,input().split())
l=[*range(a)]
s=lambda:random.randint(0,a-1)
for i in range(b):
x,y=s(),s();l[x],l[y]=[(l[x]+l[y])/2]*2
print(min(l),max(l),sum(l)/a,l)
Rules
- Standard rules applies
- Input can be in any convenience form.
- As long as you output is clean, understandable, readable, it is allowed.
- The two random value
X
,Y
should be uniform statistically independent random by itself(X
andY
are not affecting each other no matter what)
what you should do?
- Start golfing right now!
X
andY
be equal? \$\endgroup\$