Last active
May 2, 2016 22:34
-
-
Save malandro-sv/3a5b068ed79a7543fa530c617de0639f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| # 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