From f4d8437d6f7d5af2ecd8954fcd34ae18e0fb345c Mon Sep 17 00:00:00 2001 From: Mike Lustig Date: Wed, 9 Mar 2016 16:59:49 -0500 Subject: [PATCH 1/6] Complete basic challenge. --- GemCity.rb | 56 ------------------------------------------------ Gemfile | 2 +- gem_city.rb | 48 +++++++++++++++++++++++++++++++++++++++++ gem_city_spec.rb | 6 +++--- 4 files changed, 52 insertions(+), 60 deletions(-) delete mode 100644 GemCity.rb create mode 100644 gem_city.rb diff --git a/GemCity.rb b/GemCity.rb deleted file mode 100644 index 690dc84..0000000 --- a/GemCity.rb +++ /dev/null @@ -1,56 +0,0 @@ -class GemCity - -=begin -This class represents the town of GemCity -This is a town riddled with crime but we can find out how happy the town is -=end - def initialize - @people, @population = { - :thieves => 5, - :Officers => 1}, - 50 - end - - def thieves thieves_number=@people[:thieves] - return @people[:thieves] = thieves_number - end - - def officers - return @people[:Officers] - end - - def population; return @population; end - - def setOfficers officers - @people[:Officers] = officers - end - - def happiness_of_town - #happiness is random... people don't know what they want! - happinessVals = Array.new - happiness = 0 - for index in (1..@population) - happinessVals.push(rand((100 - successful_crime_rate) .. 100)) - index += 1 - end - happinessVals.each do |value| - happiness += value - end - return happiness / 100 - end - - def successful_crime_rate - thieves = @people[:thieves] - officers = @people[:Officers] - if thieves <= 0 - odds_percent = 0 - elsif officers > thieves - odds_percent = 0 - else - odds = 1 \ - - officers.to_f / thieves.to_f - odds_percent = odds * 100 - end - return odds_percent - end -end \ No newline at end of file diff --git a/Gemfile b/Gemfile index 94b6f8c..6fd63b3 100644 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,4 @@ source 'https://rubygems.org' gem 'rubocop', require: false -gem 'rspec' \ No newline at end of file +gem 'rspec' diff --git a/gem_city.rb b/gem_city.rb new file mode 100644 index 0000000..4cf106e --- /dev/null +++ b/gem_city.rb @@ -0,0 +1,48 @@ +# This class represents the town of GemCity +# This is a town riddled with crime but we can find out how happy the town is +class GemCity + attr_reader :population + def initialize + @people = { + thieves: 5, + Officers: 1 } + @population = 50 + end + + def thieves(thieves_number = @people[:thieves]) + @people[:thieves] = thieves_number + end + + def officers + @people[:Officers] + end + + def officers!(officers) + @people[:Officers] = officers + end + + def happiness_of_town + # happiness is random... people don't know what they want! + happiness_vals = [] + happiness = 0 + (1..@population).each do + happiness_vals.push(rand((100 - successful_crime_rate)..100)) + end + happiness_vals.each do |value| + happiness += value + end + happiness / 100 + end + + def successful_crime_rate + thieves = @people[:thieves] + officers = @people[:Officers] + if thieves <= 0 || officers > thieves + odds_percent = 0 + else + odds = 1 - officers.to_f / thieves.to_f + odds_percent = odds * 100 + end + odds_percent + end +end diff --git a/gem_city_spec.rb b/gem_city_spec.rb index 884b3cb..73365e8 100644 --- a/gem_city_spec.rb +++ b/gem_city_spec.rb @@ -1,5 +1,5 @@ require 'rspec' -require_relative 'GemCity' # This line may need to be changed +require_relative 'gem_city' describe 'Gem City' do let(:city) { GemCity.new } @@ -16,7 +16,7 @@ it 'officers = thieves' do city.thieves 1 - city.setOfficers 1 # This line may need to be changed + city.officers! 1 expect(city.successful_crime_rate).to eq(0) end @@ -27,7 +27,7 @@ it 'officers > thieves' do city.thieves 1 - city.setOfficers 2 # This line may need to be changed + city.officers! 2 expect(city.successful_crime_rate).to eq(0) end end From e9c972a46e90fc8875a71652ee2a94c383c0a62c Mon Sep 17 00:00:00 2001 From: Mike Lustig Date: Wed, 9 Mar 2016 18:22:55 -0500 Subject: [PATCH 2/6] Complete Bonus --- gem_city.rb | 24 ++++++++++++++++-------- gem_city_spec.rb | 38 +++++++++++++++++++------------------- 2 files changed, 35 insertions(+), 27 deletions(-) diff --git a/gem_city.rb b/gem_city.rb index 4cf106e..1f50796 100644 --- a/gem_city.rb +++ b/gem_city.rb @@ -5,7 +5,7 @@ class GemCity def initialize @people = { thieves: 5, - Officers: 1 } + officers: 1 } @population = 50 end @@ -13,12 +13,8 @@ def thieves(thieves_number = @people[:thieves]) @people[:thieves] = thieves_number end - def officers - @people[:Officers] - end - - def officers!(officers) - @people[:Officers] = officers + def officers(officers_number = @people[:officers]) + @people[:officers] = officers_number end def happiness_of_town @@ -36,7 +32,7 @@ def happiness_of_town def successful_crime_rate thieves = @people[:thieves] - officers = @people[:Officers] + officers = @people[:officers] if thieves <= 0 || officers > thieves odds_percent = 0 else @@ -45,4 +41,16 @@ def successful_crime_rate end odds_percent end + + def percentage(numerator, denominator) + format('%.0f%', (numerator.to_f / denominator.to_f) * 100.0) + end + + def city_demographics + civilians = @population - @people[:thieves] - @people[:officers] + + { thieves: percentage(@people[:thieves], @population), + officers: percentage(@people[:officers], @population), + civilians: percentage(civilians, @population) } + end end diff --git a/gem_city_spec.rb b/gem_city_spec.rb index 73365e8..89b0903 100644 --- a/gem_city_spec.rb +++ b/gem_city_spec.rb @@ -16,7 +16,7 @@ it 'officers = thieves' do city.thieves 1 - city.officers! 1 + city.officers 1 expect(city.successful_crime_rate).to eq(0) end @@ -27,7 +27,7 @@ it 'officers > thieves' do city.thieves 1 - city.officers! 2 + city.officers 2 expect(city.successful_crime_rate).to eq(0) end end @@ -43,21 +43,21 @@ end end - # context 'city_demographics' do - # it 'initialize' do - # demographics = city.city_demographics - # expect(demographics[:thieves]).to eq('10%') - # expect(demographics[:officers]).to eq('2%') - # expect(demographics[:civilians]).to eq('88%') - # end - - # it 'thieves = 10, officers = 25' do - # city.thieves = 10 - # city.officers = 25 - # demographics = city.city_demographics - # expect(demographics[:thieves]).to eq('20%') - # expect(demographics[:officers]).to eq('50%') - # expect(demographics[:civilians]).to eq('30%') - # end - # end + context 'city_demographics' do + it 'initialize' do + demographics = city.city_demographics + expect(demographics[:thieves]).to eq('10%') + expect(demographics[:officers]).to eq('2%') + expect(demographics[:civilians]).to eq('88%') + end + + it 'thieves = 10, officers = 25' do + city.thieves 10 + city.officers 25 + demographics = city.city_demographics + expect(demographics[:thieves]).to eq('20%') + expect(demographics[:officers]).to eq('50%') + expect(demographics[:civilians]).to eq('30%') + end + end end From 8ca684e1391b336ea3109e7b499c6177756bff8a Mon Sep 17 00:00:00 2001 From: Mike Lustig Date: Wed, 9 Mar 2016 18:35:29 -0500 Subject: [PATCH 3/6] A little refactoring for the challenge --- gem_city.rb | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/gem_city.rb b/gem_city.rb index 1f50796..39c83ec 100644 --- a/gem_city.rb +++ b/gem_city.rb @@ -19,14 +19,10 @@ def officers(officers_number = @people[:officers]) def happiness_of_town # happiness is random... people don't know what they want! - happiness_vals = [] - happiness = 0 - (1..@population).each do - happiness_vals.push(rand((100 - successful_crime_rate)..100)) - end - happiness_vals.each do |value| - happiness += value + happiness_vals = (1..@population).collect do + rand((100 - successful_crime_rate)..100) end + happiness = happiness_vals.reduce(0, :+) happiness / 100 end @@ -34,23 +30,21 @@ def successful_crime_rate thieves = @people[:thieves] officers = @people[:officers] if thieves <= 0 || officers > thieves - odds_percent = 0 + 0 else - odds = 1 - officers.to_f / thieves.to_f - odds_percent = odds * 100 + (1 - officers.to_f / thieves.to_f) * 100 end - odds_percent end - def percentage(numerator, denominator) - format('%.0f%', (numerator.to_f / denominator.to_f) * 100.0) + def percentage_of_population(group) + format('%.0f%', (group.to_f / @population.to_f) * 100.0) end def city_demographics civilians = @population - @people[:thieves] - @people[:officers] - { thieves: percentage(@people[:thieves], @population), - officers: percentage(@people[:officers], @population), - civilians: percentage(civilians, @population) } + { thieves: percentage_of_population(@people[:thieves]), + officers: percentage_of_population(@people[:officers]), + civilians: percentage_of_population(civilians) } end end From a20727b60d48d3f36abb6f105d58a3ca9ecb72af Mon Sep 17 00:00:00 2001 From: Mike Lustig Date: Thu, 10 Mar 2016 10:05:16 -0500 Subject: [PATCH 4/6] Break people out of unnecessary hash --- gem_city.rb | 29 +++++++++-------------------- gem_city_spec.rb | 16 ++++++++-------- 2 files changed, 17 insertions(+), 28 deletions(-) diff --git a/gem_city.rb b/gem_city.rb index 39c83ec..13ce6fc 100644 --- a/gem_city.rb +++ b/gem_city.rb @@ -2,21 +2,14 @@ # This is a town riddled with crime but we can find out how happy the town is class GemCity attr_reader :population + attr_accessor :thieves, :officers + def initialize - @people = { - thieves: 5, - officers: 1 } + @thieves = 5 + @officers = 1 @population = 50 end - def thieves(thieves_number = @people[:thieves]) - @people[:thieves] = thieves_number - end - - def officers(officers_number = @people[:officers]) - @people[:officers] = officers_number - end - def happiness_of_town # happiness is random... people don't know what they want! happiness_vals = (1..@population).collect do @@ -27,12 +20,10 @@ def happiness_of_town end def successful_crime_rate - thieves = @people[:thieves] - officers = @people[:officers] - if thieves <= 0 || officers > thieves + if @thieves <= 0 || @officers > thieves 0 else - (1 - officers.to_f / thieves.to_f) * 100 + (1 - @officers.to_f / @thieves.to_f) * 100 end end @@ -41,10 +32,8 @@ def percentage_of_population(group) end def city_demographics - civilians = @population - @people[:thieves] - @people[:officers] - - { thieves: percentage_of_population(@people[:thieves]), - officers: percentage_of_population(@people[:officers]), - civilians: percentage_of_population(civilians) } + { thieves: percentage_of_population(@thieves), + officers: percentage_of_population(@officers), + civilians: percentage_of_population(@population - @thieves - @officers) } end end diff --git a/gem_city_spec.rb b/gem_city_spec.rb index 89b0903..2eb3643 100644 --- a/gem_city_spec.rb +++ b/gem_city_spec.rb @@ -15,19 +15,19 @@ end it 'officers = thieves' do - city.thieves 1 - city.officers 1 + city.thieves = 1 + city.officers = 1 expect(city.successful_crime_rate).to eq(0) end it 'thieves = 0' do - city.thieves 0 + city.thieves = 0 expect(city.successful_crime_rate).to eq(0) end it 'officers > thieves' do - city.thieves 1 - city.officers 2 + city.thieves = 1 + city.officers = 2 expect(city.successful_crime_rate).to eq(0) end end @@ -38,7 +38,7 @@ end it 'successful_crime_rate = 0' do - city.thieves 0 + city.thieves = 0 expect(city.happiness_of_town).to eq(50) end end @@ -52,8 +52,8 @@ end it 'thieves = 10, officers = 25' do - city.thieves 10 - city.officers 25 + city.thieves = 10 + city.officers = 25 demographics = city.city_demographics expect(demographics[:thieves]).to eq('20%') expect(demographics[:officers]).to eq('50%') From 6fae050c7c2f6f341d93c3359ce3083e32716637 Mon Sep 17 00:00:00 2001 From: Mike Lustig Date: Thu, 10 Mar 2016 11:44:00 -0500 Subject: [PATCH 5/6] Factor out crime rate logic --- gem_city.rb | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/gem_city.rb b/gem_city.rb index 13ce6fc..8cf6f15 100644 --- a/gem_city.rb +++ b/gem_city.rb @@ -19,12 +19,16 @@ def happiness_of_town happiness / 100 end + def no_crimes_can_occur? + @thieves <= 0 || @officers > @thieves + end + + def crime_probability + (1 - @officers.to_f / @thieves.to_f) * 100 + end + def successful_crime_rate - if @thieves <= 0 || @officers > thieves - 0 - else - (1 - @officers.to_f / @thieves.to_f) * 100 - end + no_crimes_can_occur? ? 0 : crime_probability end def percentage_of_population(group) From 3e3b78e0518de83d51ee03e6dcee65b12caf8056 Mon Sep 17 00:00:00 2001 From: Mike Lustig Date: Mon, 14 Mar 2016 16:34:12 -0400 Subject: [PATCH 6/6] Change to using accessor methods instead of direct references to instance variables --- gem_city.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/gem_city.rb b/gem_city.rb index 8cf6f15..9ab1362 100644 --- a/gem_city.rb +++ b/gem_city.rb @@ -12,7 +12,7 @@ def initialize def happiness_of_town # happiness is random... people don't know what they want! - happiness_vals = (1..@population).collect do + happiness_vals = (1..population).collect do rand((100 - successful_crime_rate)..100) end happiness = happiness_vals.reduce(0, :+) @@ -20,11 +20,11 @@ def happiness_of_town end def no_crimes_can_occur? - @thieves <= 0 || @officers > @thieves + thieves <= 0 || officers > thieves end def crime_probability - (1 - @officers.to_f / @thieves.to_f) * 100 + (1 - officers.to_f / thieves.to_f) * 100 end def successful_crime_rate @@ -32,12 +32,12 @@ def successful_crime_rate end def percentage_of_population(group) - format('%.0f%', (group.to_f / @population.to_f) * 100.0) + format('%.0f%', (group.to_f / population.to_f) * 100.0) end def city_demographics - { thieves: percentage_of_population(@thieves), - officers: percentage_of_population(@officers), - civilians: percentage_of_population(@population - @thieves - @officers) } + { thieves: percentage_of_population(thieves), + officers: percentage_of_population(officers), + civilians: percentage_of_population(population - thieves - officers) } end end