Skip to content

Instantly share code, notes, and snippets.

@Ikhiloya
Created November 3, 2018 07:35
Show Gist options
  • Select an option

  • Save Ikhiloya/6f48d21744e7a323d1c4eb72b87efc45 to your computer and use it in GitHub Desktop.

Select an option

Save Ikhiloya/6f48d21744e7a323d1c4eb72b87efc45 to your computer and use it in GitHub Desktop.
calculates the median of a list of numbers
def median(numbers):
sorted_numbers= sorted(numbers)
size = len(sorted_numbers)
median = 0
if(size % 2 == 0):
index_1 = size/2
print index_1
index_2 = index_1 -1
print index_2
sum =(sorted_numbers[index_1] + sorted_numbers[index_2])
median = sum/ 2.0
else:
median = sorted_numbers[(size / 2)]
return median
print median([4, 5,5,4])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment