Skip to content

Instantly share code, notes, and snippets.

@malandro-sv
Last active May 2, 2016 22:34
Show Gist options
  • Select an option

  • Save malandro-sv/3a5b068ed79a7543fa530c617de0639f to your computer and use it in GitHub Desktop.

Select an option

Save malandro-sv/3a5b068ed79a7543fa530c617de0639f to your computer and use it in GitHub Desktop.
# Practice makes perfect, median.
# l1 = [8, 9, 10, 5, 6, 7, 1, 2, 3, 4]
def median(l):
l.sort()
print l
result = []
#odd numbers:
#for i in l:
if len(l) % 2 > 0:
result = l[len(l)/2]
return result
else: #even numbers:
half1 = l[0:len(l)/2]
half2 = l[len(l)/2:len(l)]
mid1= half1[len(half1) - 1]
mid2 = half2[0]
result = (mid1 + mid2) / 2.0
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment