From 48fe457f6f217c634cff3b5590739ce0d22991af Mon Sep 17 00:00:00 2001 From: Prajwal <40351128+TheDaemonLord@users.noreply.github.com> Date: Thu, 3 Oct 2019 16:58:16 +0530 Subject: [PATCH] Update median.md --- snippets/median.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/snippets/median.md b/snippets/median.md index d9b0481ad..fb0d5f3ad 100644 --- a/snippets/median.md +++ b/snippets/median.md @@ -4,16 +4,16 @@ tags: math,beginner --- Finds the median of a list of numbers. -Sort the numbers of the list using `list.sort()` and find the median, which is either the middle element of the list if the list length is odd or the average of the two middle elements if the list length is even.. +Sort the numbers of the list using `list.sort()` and find the median, which is either the middle element of the list if the list length is odd or the average of the two middle elements if the list length is even. ```py def median(list): - list.sort() # The sort function of python - list_length = len(list) - if list_length%2==0: - return (list[int(list_length/2)-1] + list[int(list_length/2)])/2 - else: - return list[int(list_length/2)] + list.sort() + list_length = len(list) + if list_length%2==0: + return (list[int(list_length/2)-1] + list[int(list_length/2)])/2 + else: + return list[int(list_length/2)] ``` ```py median([1,2,3]) # 2