From 854382891ee132aa3d790621ee05fe1848c9e96d Mon Sep 17 00:00:00 2001 From: Saud Date: Mon, 10 Jul 2017 18:52:13 +0000 Subject: [PATCH 1/7] First half completed --- app/controllers/calculations_controller.rb | 32 ++++++++++++++++++---- 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/app/controllers/calculations_controller.rb b/app/controllers/calculations_controller.rb index f143fcc..af486d0 100644 --- a/app/controllers/calculations_controller.rb +++ b/app/controllers/calculations_controller.rb @@ -83,18 +83,38 @@ def descriptive_statistics # The numbers the user input are in the array @numbers. # ================================================================================ - @sorted_numbers = "Replace this string with your answer." + # @squared_numbers = [] - @count = "Replace this string with your answer." + # @numbers.each do |num| + # square = num * num + # @squared_numbers.push(square) + # end - @minimum = "Replace this string with your answer." + # @sorted_numbers = @squared_numbers - @maximum = "Replace this string with your answer." + ####################SORTED NUMBERS#################### + @sorted_numbers = @numbers.sort! - @range = "Replace this string with your answer." + ####################COUNT#################### + @count = @numbers.size - @median = "Replace this string with your answer." + ####################MINIMUM#################### + @minimum = @numbers.min + ####################MAXIMUM#################### + @maximum = @numbers.max + + ####################RANGE#################### + @range = @numbers.max - @numbers.min + + ###################MEDIAN#################### + if(@count % 2 == 0) + @median = (@numbers[(@count/2).to_i - 1] + @numbers[(@count/2).to_i])/2.0 + else + @median = @numbers[(@count/2).to_i] + end + + @sum = "Replace this string with your answer." @mean = "Replace this string with your answer." From 3b8f778f37c49f5555ee8cafdab065a170318864 Mon Sep 17 00:00:00 2001 From: Saud Date: Mon, 10 Jul 2017 20:04:32 +0000 Subject: [PATCH 2/7] First attempt at everything except mode --- app/controllers/calculations_controller.rb | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/app/controllers/calculations_controller.rb b/app/controllers/calculations_controller.rb index af486d0..2b2643d 100644 --- a/app/controllers/calculations_controller.rb +++ b/app/controllers/calculations_controller.rb @@ -114,15 +114,27 @@ def descriptive_statistics @median = @numbers[(@count/2).to_i] end - - @sum = "Replace this string with your answer." + ###################SUM#################### + @sum = @numbers.sum - @mean = "Replace this string with your answer." + ###################MEAN#################### + @mean = @sum/@count - @variance = "Replace this string with your answer." + ###################VARIANCE#################### + @calculation = [] - @standard_deviation = "Replace this string with your answer." + @numbers.each do |num| + difference = num - @mean + difference = difference * difference + @calculation.push(difference) + end + + @variance = (@calculation.sum) / (@count - 1) + ###################STANDARD DEVIATION#################### + @standard_deviation = @variance ** (1/2) + + ###################STANDARD DEVIATION#################### @mode = "Replace this string with your answer." # ================================================================================ From bae97d9c7c62ee1d2789bdb2d575c39707b5a81c Mon Sep 17 00:00:00 2001 From: Saud Date: Tue, 11 Jul 2017 03:35:15 +0000 Subject: [PATCH 3/7] First attempt at everything except mode --- app/controllers/calculations_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/calculations_controller.rb b/app/controllers/calculations_controller.rb index 2b2643d..07d1b5c 100644 --- a/app/controllers/calculations_controller.rb +++ b/app/controllers/calculations_controller.rb @@ -135,7 +135,7 @@ def descriptive_statistics @standard_deviation = @variance ** (1/2) ###################STANDARD DEVIATION#################### - @mode = "Replace this string with your answer." + @mode = "The mode will be shown here." # ================================================================================ # Your code goes above. From 4e8215dcf13bb74dcbd8a3727ad13210c6b25050 Mon Sep 17 00:00:00 2001 From: Saud Date: Tue, 11 Jul 2017 14:32:52 +0000 Subject: [PATCH 4/7] Variance and standard deviation completed --- app/controllers/calculations_controller.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/app/controllers/calculations_controller.rb b/app/controllers/calculations_controller.rb index 07d1b5c..0d4e270 100644 --- a/app/controllers/calculations_controller.rb +++ b/app/controllers/calculations_controller.rb @@ -125,14 +125,13 @@ def descriptive_statistics @numbers.each do |num| difference = num - @mean - difference = difference * difference - @calculation.push(difference) + @calculation.push(difference * difference) end - @variance = (@calculation.sum) / (@count - 1) + @variance = (@calculation.sum) / (@count) ###################STANDARD DEVIATION#################### - @standard_deviation = @variance ** (1/2) + @standard_deviation = @variance ** (1.0/2) ###################STANDARD DEVIATION#################### @mode = "The mode will be shown here." From 85287b906daf1c9a7282a181c797fee6b8a8a64a Mon Sep 17 00:00:00 2001 From: Saud Date: Tue, 11 Jul 2017 23:28:50 +0000 Subject: [PATCH 5/7] Mode partially completed --- app/controllers/calculations_controller.rb | 24 +++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/app/controllers/calculations_controller.rb b/app/controllers/calculations_controller.rb index 0d4e270..cfd57c1 100644 --- a/app/controllers/calculations_controller.rb +++ b/app/controllers/calculations_controller.rb @@ -109,9 +109,9 @@ def descriptive_statistics ###################MEDIAN#################### if(@count % 2 == 0) - @median = (@numbers[(@count/2).to_i - 1] + @numbers[(@count/2).to_i])/2.0 + @median = (@sorted_numbers[(@count/2).to_i - 1] + @sorted_numbers[(@count/2).to_i])/2.0 else - @median = @numbers[(@count/2).to_i] + @median = @sorted_numbers[(@count/2).to_i] end ###################SUM#################### @@ -131,10 +131,24 @@ def descriptive_statistics @variance = (@calculation.sum) / (@count) ###################STANDARD DEVIATION#################### - @standard_deviation = @variance ** (1.0/2) + @standard_deviation = @variance ** (1.0/2.0) + + ###################MODE#################### + counter = Hash.new(0) + + @sorted_numbers.each do |num| + counter[num] += 1 + # p counter + end + + highest_mode = counter.max_by{|key,value| value}[1] #counter.sort_by{|key,value| value}[-1] + values = counter.select{|key,value| value==highest_mode} + + @mode = values + + #values.select{|k,v| v == hash.values.max} + #counter.invert["key"=>highest_mode] - ###################STANDARD DEVIATION#################### - @mode = "The mode will be shown here." # ================================================================================ # Your code goes above. From 41ec54a682aa5ce13411a950682f4ff9e5a6a5b1 Mon Sep 17 00:00:00 2001 From: Saud Date: Tue, 11 Jul 2017 23:43:01 +0000 Subject: [PATCH 6/7] Completed --- app/controllers/calculations_controller.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/controllers/calculations_controller.rb b/app/controllers/calculations_controller.rb index cfd57c1..720eda6 100644 --- a/app/controllers/calculations_controller.rb +++ b/app/controllers/calculations_controller.rb @@ -93,7 +93,8 @@ def descriptive_statistics # @sorted_numbers = @squared_numbers ####################SORTED NUMBERS#################### - @sorted_numbers = @numbers.sort! + @numbers = @numbers.sort! + @sorted_numbers = @numbers ####################COUNT#################### @count = @numbers.size From cead21d82b701f192b7a390c2061e0f1e9539c98 Mon Sep 17 00:00:00 2001 From: Saud Date: Tue, 11 Jul 2017 23:57:17 +0000 Subject: [PATCH 7/7] Completed (getting the last point) --- app/controllers/calculations_controller.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/controllers/calculations_controller.rb b/app/controllers/calculations_controller.rb index 720eda6..dc4aa97 100644 --- a/app/controllers/calculations_controller.rb +++ b/app/controllers/calculations_controller.rb @@ -93,8 +93,7 @@ def descriptive_statistics # @sorted_numbers = @squared_numbers ####################SORTED NUMBERS#################### - @numbers = @numbers.sort! - @sorted_numbers = @numbers + @sorted_numbers = @numbers.sort ####################COUNT#################### @count = @numbers.size