Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@k-popov
Last active January 11, 2019 10:53
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 k-popov/c582544af805ff9596f6 to your computer and use it in GitHub Desktop.
Save k-popov/c582544af805ff9596f6 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python2
source = [1,5,7,3,2,1,4]
median = sum(source)/float(len(source))
mindelta = abs(source[0] - median)
closest = source[0]
for item in source[1:]:
if abs(item - median) < mindelta:
closest = item
mindelta = abs(item - median)
print(closest)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment