Last active
October 21, 2018 08:36
-
-
Save lion137/7263b9c2fe0e12137be8c0104e12b2b1 to your computer and use it in GitHub Desktop.
some parameter finder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def find_median(xs): | |
ys = sorted(xs) | |
if not len(xs) % 2 == 0: | |
return ys[len(ys) // 2] | |
else: | |
return (ys[(len(ys) // 2)] + ys[(len(ys) // 2) - 1]) / 2 | |
def make_sum(xs, a): | |
return sum(map(lambda x: abs(x - a), xs)) | |
def find_x(xs): | |
a = find_median(xs) | |
i = a | |
param = None | |
param1 = None | |
while True: | |
s1 = make_sum(xs, i) | |
s2 = make_sum(xs, i + 0.5) | |
if s1 == s2: | |
param = i | |
break | |
if s2 < s1: | |
i += 0.5 | |
continue | |
if s2 > s1: | |
param = i | |
break | |
while True: | |
s1 = make_sum(xs, i) | |
s2 = make_sum(xs, i -0.5) | |
if s1 == s2: | |
param1= i | |
break | |
if s2 < s1: | |
i - 0.5 | |
continue | |
if s2 > s1: | |
param1= i | |
break | |
return param if make_sum(xs, param) <= make_sum(xs, param1) else param1 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment