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.
Generate a range, start from 0, with length of
Randomly choose 2 number (or item)
Yand replace both
Ywith the average of
Repeat step 2 for
Output minimum, maximum, average, and the processed list.
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)
- 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
Yshould be uniform statistically independent random by itself(
Yare not affecting each other no matter what)
what you should do?
- Start golfing right now!