I want to find a specific algorithm to choose either 2 or 3 out of 3 given values. Like if I have a set of values 10,11,12 all three are close enough so I will calculate the mean of the value and return the mean. For another example if the values are 6,10,12. then I will choose 10 and 12 and calculate the mean and return the mean. Though these value can not necessarily be of of this range , they can go in thousands as well . But there will only be three number of values . I hope I am clear enough. I am not sure how to proceed over this and I need some help . Thank you
Asked
Active
Viewed 134 times
-2
-
Is the set of values always sorted? – CodingTil Jun 30 '20 at 12:57
-
2Define `close enough` - do you have any threshold? – Grzegorz Skibinski Jun 30 '20 at 12:58
-
Are the values integers only, as in the examples ? – Damien Jun 30 '20 at 13:13
-
I have to set the the threshold , It will be in between 10-20. @GrzegorzSkibinski – Utkarsh Ghadge Jun 30 '20 at 13:18
-
Yes the values are Integrers only @Damien – Utkarsh Ghadge Jun 30 '20 at 13:18
2 Answers
0
Simply calculated all 4 possible selects (3 possibilities for 2 values, 1 for all three), calculate the means, check which one 'like' the most and return it.
So the only problem is to define 'close enough' to decide which of the 4 means is the one you want to return.

MrSmith42
- 9,961
- 6
- 38
- 49
0
python
version:
def algo(a:int, b:int, c:int, threshold:int):
n=[0,0,0]
s=0
if(abs(a-b)<=threshold):
s+=(1-n[0])*a+(1-n[1])*b
n[0]=1
n[1]=1
if(abs(a-c)<=threshold):
s+=(1-n[0])*a+(1-n[2])*c
n[0]=1
n[2]=1
if(abs(c-b)<=threshold):
s+=(1-n[1])*b+(1-n[2])*c
n[1]=1
n[2]=1
return s/sum(n)

Grzegorz Skibinski
- 12,624
- 2
- 11
- 34