From 752006c58233fdb0ddd387a34ec6c119bb788034 Mon Sep 17 00:00:00 2001 From: Shriprajwal Date: Thu, 3 Oct 2019 14:32:17 +0530 Subject: [PATCH 1/6] Added the median snippet --- snippets/median.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 snippets/median.md diff --git a/snippets/median.md b/snippets/median.md new file mode 100644 index 000000000..aa63231bb --- /dev/null +++ b/snippets/median.md @@ -0,0 +1,23 @@ +--- +title: median +tags: math,median,beginner +--- +Find the median of a list of numbers + +Sort the numbers of the list using the sort function and find the median, which is the middlemost element of the list. + +```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 # Mean of the middle two elements + else: + return list[int(list_length/2)] # The element in the middle + +``` + +```py +median([1,2,3]) # 2 +median([1,2,3,4]) # 2.5 +``` From 3933fac94435b2fe3fd5d35a0975dde4b48c3051 Mon Sep 17 00:00:00 2001 From: Prajwal <40351128+TheDaemonLord@users.noreply.github.com> Date: Thu, 3 Oct 2019 16:35:46 +0530 Subject: [PATCH 2/6] Update snippets/median.md Co-Authored-By: Angelos Chalaris --- snippets/median.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/median.md b/snippets/median.md index aa63231bb..3d38bc5a9 100644 --- a/snippets/median.md +++ b/snippets/median.md @@ -4,7 +4,7 @@ tags: math,median,beginner --- Find the median of a list of numbers -Sort the numbers of the list using the sort function and find the median, which is the middlemost element of the list. +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): From ebadaa8c3782fe2ec88fe9cd26283544e0e159a4 Mon Sep 17 00:00:00 2001 From: Prajwal <40351128+TheDaemonLord@users.noreply.github.com> Date: Thu, 3 Oct 2019 16:36:16 +0530 Subject: [PATCH 3/6] Update snippets/median.md Co-Authored-By: Angelos Chalaris --- snippets/median.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/median.md b/snippets/median.md index 3d38bc5a9..d09484f66 100644 --- a/snippets/median.md +++ b/snippets/median.md @@ -2,7 +2,7 @@ title: median tags: math,median,beginner --- -Find the median of a list of numbers +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.. From b974426bcd5d5c3b01b743dfd4952f9d56d3a37f Mon Sep 17 00:00:00 2001 From: Prajwal <40351128+TheDaemonLord@users.noreply.github.com> Date: Thu, 3 Oct 2019 16:38:21 +0530 Subject: [PATCH 4/6] Update median.md --- snippets/median.md | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/snippets/median.md b/snippets/median.md index d09484f66..d9b0481ad 100644 --- a/snippets/median.md +++ b/snippets/median.md @@ -1,6 +1,6 @@ --- title: median -tags: math,median,beginner +tags: math,beginner --- Finds the median of a list of numbers. @@ -11,12 +11,10 @@ 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 # Mean of the middle two elements + return (list[int(list_length/2)-1] + list[int(list_length/2)])/2 else: - return list[int(list_length/2)] # The element in the middle - + return list[int(list_length/2)] ``` - ```py median([1,2,3]) # 2 median([1,2,3,4]) # 2.5 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 5/6] 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 From b3b895cb670cea6928240dc03eb1ab8b72846bfc Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Fri, 4 Oct 2019 09:13:50 +0300 Subject: [PATCH 6/6] Update median.md --- snippets/median.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/snippets/median.md b/snippets/median.md index fb0d5f3ad..1717685d6 100644 --- a/snippets/median.md +++ b/snippets/median.md @@ -2,6 +2,7 @@ title: median 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. @@ -15,6 +16,7 @@ def median(list): else: return list[int(list_length/2)] ``` + ```py median([1,2,3]) # 2 median([1,2,3,4]) # 2.5