From 364ac628f7586cce5c2dddb29854e4e795054202 Mon Sep 17 00:00:00 2001
From: Rahul Gupta
Date: Thu, 1 Oct 2020 20:36:20 +0530
Subject: [PATCH 1/4] Added Mean, Median, and Mode algo
---
README.md | 30 +++++++++++++++++++++++++-----
1 file changed, 25 insertions(+), 5 deletions(-)
diff --git a/README.md b/README.md
index a78cad2..5da59fe 100644
--- a/README.md
+++ b/README.md
@@ -77,7 +77,7 @@ __Algorithms practiced using JS__
3. Finding the Most Recurring Character
4. Sentence Capitalization
5. Palindromes
-
+6. Mean, Median, and Mode
## Explanation
1. String reversing
The string reversal algorithm is perhaps the most common JavaScript code challenge on the internet. In this article, we explore various string reversal techniques as a good number of string manipulation algorithms are dependent on ones ability to reverse a string.
@@ -447,15 +447,35 @@ method tests whether all elements pass the test or not which is implemented by p
-6. Name
+6. Mean, Median, and Mode
-__The challenge:__
+__The challenge:__ Given an array of numbers, calculate the mean, median, and mode.
-__Algorithmic Thinking:__
+__Algorithmic Thinking:__ In terms of difficulty, the algorithm to find the mean of a collection of numbers is the easiest. Statistically, the mean is defined as the sum of the collection divided by its size. Therefore, we can simply use the array’s reduce method to calculate its sum and then divide that by its length. This algorithm has runtime complexities of linear time and constant space because every number needs to be added while no internal memory is necessary.
+The algorithm to find the median of a collection is of medium difficulty. First, we need to sort the array, but if its size is even, we will need extra logic to deal with two middle numbers. In these cases, we will need to return the average of those two numbers. This algorithm has a linearithmic time complexity due to sorting and a linear space complexity because internal memory is needed to hold the sorted array.
-__code Implementation:__
+The algorithm to find the mode is the most challenging. Since the mode is defined as the number or numbers that appear the most often, we will need to maintain a frequency table. To complicate things further, if every value appears the same number of times, there is no mode. In code, this means we will need to create a hash map that tallies the frequency of each unique number, and then loop through it to collect the maximum number or numbers, or none. Because every number needs to be counted to create the hash table which is held in memory, this algorithm has a linear time and space complexity.
+
+
+__code Implementation:__ const stat1 = new Stats([1, 2, 3, 4, 4, 5, 5]);
+const stat2 = new Stats([1, 1, 2, 2, 3, 3, 4, 4]);describe("Mean", () => {
+ it("Should implement mean", () => {
+ assert.equal(Stats.round(stat1.mean()), 3.43);
+ assert.equal(Stats.round(stat2.mean()), 2.5);
+ });
+});describe("Median", () => {
+ it("Should implement median", () => {
+ assert.equal(stat1.median(), 4);
+ assert.equal(stat2.median(), 2.5);
+ });
+});describe("Mode", () => {
+ it("Should implement mode", () => {
+ assert.deepEqual(stat1.mode(), [4, 5]);
+ assert.deepEqual(stat2.mode(), []);
+ });
+});
From a06475a551499b2b8c10d34dacd677131a267cda Mon Sep 17 00:00:00 2001
From: Rahul Gupta
Date: Thu, 1 Oct 2020 20:37:45 +0530
Subject: [PATCH 2/4] Create Mean, Median, and Mode_6
---
Mean, Median, and Mode_6 | 1 +
1 file changed, 1 insertion(+)
create mode 100644 Mean, Median, and Mode_6
diff --git a/Mean, Median, and Mode_6 b/Mean, Median, and Mode_6
new file mode 100644
index 0000000..8b13789
--- /dev/null
+++ b/Mean, Median, and Mode_6
@@ -0,0 +1 @@
+
From e109090c678f43393e02fa7c5bac2ea7ae7a5e70 Mon Sep 17 00:00:00 2001
From: Rahul Gupta
Date: Thu, 1 Oct 2020 20:38:34 +0530
Subject: [PATCH 3/4] Delete Mean, Median, and Mode_6
---
Mean, Median, and Mode_6 | 1 -
1 file changed, 1 deletion(-)
delete mode 100644 Mean, Median, and Mode_6
diff --git a/Mean, Median, and Mode_6 b/Mean, Median, and Mode_6
deleted file mode 100644
index 8b13789..0000000
--- a/Mean, Median, and Mode_6
+++ /dev/null
@@ -1 +0,0 @@
-
From bfb113b73ecf7358e0ac9cd7c4206935ab2e6f86 Mon Sep 17 00:00:00 2001
From: Rahul Gupta
Date: Thu, 1 Oct 2020 20:39:26 +0530
Subject: [PATCH 4/4] Create MMM.js
---
Mean, Median, and Mode_6/MMM.js | 17 +++++++++++++++++
1 file changed, 17 insertions(+)
create mode 100644 Mean, Median, and Mode_6/MMM.js
diff --git a/Mean, Median, and Mode_6/MMM.js b/Mean, Median, and Mode_6/MMM.js
new file mode 100644
index 0000000..f3b9df2
--- /dev/null
+++ b/Mean, Median, and Mode_6/MMM.js
@@ -0,0 +1,17 @@
+const stat1 = new Stats([1, 2, 3, 4, 4, 5, 5]);
+const stat2 = new Stats([1, 1, 2, 2, 3, 3, 4, 4]);describe("Mean", () => {
+ it("Should implement mean", () => {
+ assert.equal(Stats.round(stat1.mean()), 3.43);
+ assert.equal(Stats.round(stat2.mean()), 2.5);
+ });
+});describe("Median", () => {
+ it("Should implement median", () => {
+ assert.equal(stat1.median(), 4);
+ assert.equal(stat2.median(), 2.5);
+ });
+});describe("Mode", () => {
+ it("Should implement mode", () => {
+ assert.deepEqual(stat1.mode(), [4, 5]);
+ assert.deepEqual(stat2.mode(), []);
+ });
+});