Skip to content

Instantly share code, notes, and snippets.

@lion137
Last active October 21, 2018 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lion137/7263b9c2fe0e12137be8c0104e12b2b1 to your computer and use it in GitHub Desktop.
Save lion137/7263b9c2fe0e12137be8c0104e12b2b1 to your computer and use it in GitHub Desktop.
some parameter finder
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