From ccec5b489a8c7d4839bd76f49658b596b8a3017f Mon Sep 17 00:00:00 2001 From: Zach Holcomb Date: Thu, 30 Jan 2020 13:48:34 -0700 Subject: [PATCH 1/8] Add standard deviation solution --- lib/standard_deviation.rb | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/standard_deviation.rb b/lib/standard_deviation.rb index e17b03a..68a6edf 100644 --- a/lib/standard_deviation.rb +++ b/lib/standard_deviation.rb @@ -1,5 +1,21 @@ ages = [24, 30, 18, 20, 41] -# Your code here for calculating the standard deviation +ages_sum = ages.sum.to_f +num_of_ages = ages.count.to_f +average_age = ages_sum / num_of_ages + +ages.map! do |age| + (((age.to_f) - average_age.round(1))**2).round(2) +end + +standard_deviation = (Math.sqrt(((ages.sum.round(1)) / 5).round(2))).round(2) + +p standard_deviation + + + + + + # When you find the standard deviation, print it out From 1fc33dfe2ce3fc15347a7f87d674da82d5d31a72 Mon Sep 17 00:00:00 2001 From: Zach Holcomb Date: Thu, 30 Jan 2020 14:00:45 -0700 Subject: [PATCH 2/8] Add event class and test --- lib/event.rb | 10 ++++++++++ test/event_test.rb | 15 +++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 lib/event.rb create mode 100644 test/event_test.rb diff --git a/lib/event.rb b/lib/event.rb new file mode 100644 index 0000000..7f1e96e --- /dev/null +++ b/lib/event.rb @@ -0,0 +1,10 @@ +class Event + attr_reader :name, :participants_ages + + def initialize(name, participants_ages) + @name = name + @participants_ages = [] + end + + +end diff --git a/test/event_test.rb b/test/event_test.rb new file mode 100644 index 0000000..bd238c2 --- /dev/null +++ b/test/event_test.rb @@ -0,0 +1,15 @@ +require 'minitest/autorun' +require 'minitest/pride' +require './lib/event' + +class EventTest < Minitest::Test + def setup + @event = Event.new("Curling", [24, 30, 18, 20, 41]) + end + + def test_does_it_exist + + assert_instance_of Event, @event + end + +end From 46f50fb6e7501d0a381d077677c4dad9bb3c2456 Mon Sep 17 00:00:00 2001 From: Zach Holcomb Date: Thu, 30 Jan 2020 14:11:46 -0700 Subject: [PATCH 3/8] Add max_age test and method --- lib/event.rb | 9 ++++++--- test/event_test.rb | 12 ++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/event.rb b/lib/event.rb index 7f1e96e..dedf44f 100644 --- a/lib/event.rb +++ b/lib/event.rb @@ -1,10 +1,13 @@ class Event - attr_reader :name, :participants_ages + attr_reader :name, :ages - def initialize(name, participants_ages) + def initialize(name, ages) @name = name - @participants_ages = [] + @ages = ages end + def max_age + @ages.max + end end diff --git a/test/event_test.rb b/test/event_test.rb index bd238c2..b0a387c 100644 --- a/test/event_test.rb +++ b/test/event_test.rb @@ -12,4 +12,16 @@ def test_does_it_exist assert_instance_of Event, @event end + def test_does_event_have_attributes + + assert_equal "Curling", @event.name + assert_equal [24, 30, 18, 20, 41], @event.ages + end + + def test_find_max_age + + assert_equal 41, @event.max_age + end + + end From 20913a0f361bf8cd7df90a08812ef3b816bbd010 Mon Sep 17 00:00:00 2001 From: Zach Holcomb Date: Thu, 30 Jan 2020 14:13:52 -0700 Subject: [PATCH 4/8] Add min_age test and method --- lib/event.rb | 4 ++++ test/event_test.rb | 7 ++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/event.rb b/lib/event.rb index dedf44f..a67d4be 100644 --- a/lib/event.rb +++ b/lib/event.rb @@ -9,5 +9,9 @@ def initialize(name, ages) def max_age @ages.max end + def min_age + @ages.min + end + end diff --git a/test/event_test.rb b/test/event_test.rb index b0a387c..92f4dfd 100644 --- a/test/event_test.rb +++ b/test/event_test.rb @@ -22,6 +22,11 @@ def test_find_max_age assert_equal 41, @event.max_age end - + + def test_find_min_age + + assert_equal 18, @event.min_age + end + end From df0d134abab77077665b8cddb45cc397c08f8bd9 Mon Sep 17 00:00:00 2001 From: Zach Holcomb Date: Thu, 30 Jan 2020 14:44:50 -0700 Subject: [PATCH 5/8] Add standard_deviation_age method and test --- lib/event.rb | 19 ++++++++++++++++++- test/event_test.rb | 9 +++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/event.rb b/lib/event.rb index a67d4be..f7d8414 100644 --- a/lib/event.rb +++ b/lib/event.rb @@ -4,6 +4,8 @@ class Event def initialize(name, ages) @name = name @ages = ages + + end def max_age @@ -12,6 +14,21 @@ def max_age def min_age @ages.min end - + def average_age + ages_sum = @ages.sum.to_f + num_of_ages = @ages.count.to_f + ages_sum / num_of_ages + end + + def standard_deviation_age + average_age = self.average_age + + @ages.map! do |age| + (((age.to_f) - average_age.round(1))**2).round(2) + end + + standard_deviation = (Math.sqrt(((ages.sum.round(1)) / 5).round(2))).round(2) + return standard_deviation + end end diff --git a/test/event_test.rb b/test/event_test.rb index 92f4dfd..bfe295f 100644 --- a/test/event_test.rb +++ b/test/event_test.rb @@ -28,5 +28,14 @@ def test_find_min_age assert_equal 18, @event.min_age end + def test_find_average_age + + assert_equal 26.6, @event.average_age + end + + def test_find_standard_deviation_age + + assert_equal 8.28, @event.standard_deviation_age + end end From d4a6ae87aebdeb86b9130aa78d21627ac6a70fd8 Mon Sep 17 00:00:00 2001 From: Zach Holcomb Date: Thu, 30 Jan 2020 15:31:33 -0700 Subject: [PATCH 6/8] Add user event_input functionality --- lib/runner.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 lib/runner.rb diff --git a/lib/runner.rb b/lib/runner.rb new file mode 100644 index 0000000..d2ffa5f --- /dev/null +++ b/lib/runner.rb @@ -0,0 +1,23 @@ +require './lib/event' +require './lib/games' +# 20.times(print "-") +puts "Welcome! Tell me the year of the event you are inquiring about?" +print "> " +year = gets.chomp + +new_olympics = Games.new(year) + +puts "Great! Now let's add some events! Enter 'Exit' to quit." + +event_input = "" + +until event_input == "Exit" + if event_input == "Exit" + puts "Thanks, let's move on." + else + print "> " + event_input = gets.chomp + new_olympics.add_event(event_input.capitalize) + end + require "pry"; binding.pry +end From 1e61092a015d4c2c61b25d2c9154372cff565677 Mon Sep 17 00:00:00 2001 From: Zach Holcomb Date: Thu, 30 Jan 2020 15:44:51 -0700 Subject: [PATCH 7/8] Fix event_input bug --- lib/runner.rb | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/lib/runner.rb b/lib/runner.rb index d2ffa5f..71fc603 100644 --- a/lib/runner.rb +++ b/lib/runner.rb @@ -11,13 +11,12 @@ event_input = "" -until event_input == "Exit" +loop do + print "> " + event_input = gets.chomp if event_input == "Exit" - puts "Thanks, let's move on." - else - print "> " - event_input = gets.chomp - new_olympics.add_event(event_input.capitalize) + puts "Thanks, let's move on." + break end - require "pry"; binding.pry + new_olympics.add_event(event_input.capitalize) end From 32e1cd8b80fff31486ff333dd2563209afb83c71 Mon Sep 17 00:00:00 2001 From: Zach Holcomb Date: Thu, 30 Jan 2020 16:00:03 -0700 Subject: [PATCH 8/8] Update functionality of user input loop --- lib/runner.rb | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/runner.rb b/lib/runner.rb index 71fc603..3bcca5e 100644 --- a/lib/runner.rb +++ b/lib/runner.rb @@ -10,13 +10,25 @@ puts "Great! Now let's add some events! Enter 'Exit' to quit." event_input = "" +ages = [] loop do + puts "Add another event? Use 'Exit' to quit." print "> " - event_input = gets.chomp + event_input = gets.chomp.capitalize if event_input == "Exit" puts "Thanks, let's move on." break end - new_olympics.add_event(event_input.capitalize) + loop do + puts "I also need to know their ages, say 'Next' to enter next event." + print "> " + age_input = gets.chomp.capitalize + if age_input == "Next" + break + end + ages << age_input + end + new_event = Event.new(event_input, ages) + new_olympics.add_event(new_event) end