From 5a709ff8ad1e439271a3ed25ab537f7571eb7561 Mon Sep 17 00:00:00 2001 From: drammopo Date: Mon, 24 Feb 2014 22:07:15 +0200 Subject: [PATCH 01/16] Added changes to switch from stubs to doubles as per RSpec updates. --- lib/map.rb | 1 + lib/sales_person.rb | 1 + spec/map_spec.rb | 9 +++++---- spec/place_spec.rb | 7 ++++--- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/map.rb b/lib/map.rb index e9abfde..51b04d3 100644 --- a/lib/map.rb +++ b/lib/map.rb @@ -1,4 +1,5 @@ require 'geocoder' + class Map def self.search(terms) diff --git a/lib/sales_person.rb b/lib/sales_person.rb index d0c2890..dcaef7e 100644 --- a/lib/sales_person.rb +++ b/lib/sales_person.rb @@ -12,4 +12,5 @@ def schedule_city(city) def route CalculatesRoute.calculate(cities) end + end diff --git a/spec/map_spec.rb b/spec/map_spec.rb index 2e2f6f1..64d3952 100644 --- a/spec/map_spec.rb +++ b/spec/map_spec.rb @@ -1,5 +1,6 @@ require_relative "../lib/map" require 'geocoder' +require 'rspec' describe Map do @@ -10,8 +11,8 @@ end it "should use the first item in the array" do - austin = stub("Austin") - dallas = stub("Dallas") + austin = double("Austin") + dallas = double("Dallas") Geocoder.stub(:search) {[austin, dallas]} Map.search("austin, tx").should eq(austin) end @@ -19,8 +20,8 @@ describe ":distance" do it "should calculate distance between two sets of coordinates" do - alpha = stub - beta = stub + alpha = double + beta = double Geocoder::Calculations.should_receive(:distance_between).with(alpha, beta) Map.distance_between(alpha, beta) end diff --git a/spec/place_spec.rb b/spec/place_spec.rb index 7d48250..760361f 100644 --- a/spec/place_spec.rb +++ b/spec/place_spec.rb @@ -1,5 +1,6 @@ require_relative "../lib/place" require_relative "../lib/map" +require 'rspec' describe Place do @@ -14,16 +15,16 @@ describe ":build" do let(:name) { "El Paso, TX"} - let(:result) { stub("el paso", coordinates: [29, -95])} + let(:result) { double("el paso", coordinates: [29, -95])} it "should build from the map" do Map.should_receive(:search).with(name).and_return(result) Place.build(name) end - it "should be place" do + it "should be place" do Map.stub(:search).with(name).and_return(result) - Place.build(name).should be_a(Place) + Place.build(name).should be_a(Place) end end From 342494dee1eaf322f6f6923c4e0c392edf485857 Mon Sep 17 00:00:00 2001 From: drammopo Date: Tue, 25 Feb 2014 20:01:43 +0200 Subject: [PATCH 02/16] Added feature to tell Salesperson the starting point. --- lib/place.rb | 5 +++-- lib/sales_person.rb | 5 +++++ salesperson.rb | 2 +- spec/place_spec.rb | 13 +++++++++++++ spec/sales_person_spec.rb | 20 ++++++++++++++++---- 5 files changed, 38 insertions(+), 7 deletions(-) diff --git a/lib/place.rb b/lib/place.rb index 99eaadf..35f80d5 100644 --- a/lib/place.rb +++ b/lib/place.rb @@ -1,13 +1,14 @@ require_relative "./map" class Place - attr_accessor :name, :coordinates + attr_accessor :name, :coordinates, :starting_point - def self.build(name) + def self.build(name, starting_point = false) results = Map.search(name) Place.new.tap do |p| p.name = name p.coordinates = results.coordinates + p.starting_point ||= starting_point end end diff --git a/lib/sales_person.rb b/lib/sales_person.rb index dcaef7e..28635ca 100644 --- a/lib/sales_person.rb +++ b/lib/sales_person.rb @@ -7,6 +7,11 @@ def initialize def schedule_city(city) @cities << city unless @cities.include?(city) + @cities.each_with_index do |city, index| + if city.starting_point == true + @cities.insert(0, cities.delete_at(index)) + end + end end def route diff --git a/salesperson.rb b/salesperson.rb index 9cafbd7..b0d8f82 100644 --- a/salesperson.rb +++ b/salesperson.rb @@ -3,7 +3,7 @@ phil = SalesPerson.new phil.schedule_city(Place.build("Dallas, TX")) -phil.schedule_city(Place.build("El Paso, TX")) +phil.schedule_city(Place.build("El Paso, TX", true)) phil.schedule_city(Place.build("Austin, TX")) phil.schedule_city(Place.build("Lubbock, TX")) diff --git a/spec/place_spec.rb b/spec/place_spec.rb index 760361f..1f70982 100644 --- a/spec/place_spec.rb +++ b/spec/place_spec.rb @@ -12,6 +12,13 @@ subject.coordinates.should eq([29,-95]) end + it "should have a starting point property" do + subject.should respond_to(:starting_point) + end + it "should not be a starting point by default" do + subject.starting_point.should be_false + end + describe ":build" do let(:name) { "El Paso, TX"} @@ -26,6 +33,12 @@ Map.stub(:search).with(name).and_return(result) Place.build(name).should be_a(Place) end + + it "should be set to the starting point when asked" do + Map.stub(:search).with(name).and_return(result) + place = Place.build(name, true) + place.starting_point.should be_true + end end describe "#to_s" do diff --git a/spec/sales_person_spec.rb b/spec/sales_person_spec.rb index 08a6ce9..c897a89 100644 --- a/spec/sales_person_spec.rb +++ b/spec/sales_person_spec.rb @@ -1,30 +1,42 @@ +require 'rspec' require_relative "../lib/sales_person" require_relative "../lib/calculates_route" +require_relative "../lib/place" describe SalesPerson do - it "should have many cities" do + xit "should have many cities" do city = stub subject.schedule_city(city) subject.cities.should include(city) end - it "should keep the cities only scheduled once" do + xit "should keep the cities only scheduled once" do city = stub expect{ subject.schedule_city(city) subject.schedule_city(city) - }.to change(subject.cities,:count).by(1) + }.to change(subject.cities,:count).by(1) end it "should calculate a route via the CalculatesRoute" do cities = [stub, stub, stub] - subject.stub(:cities) { cities } + subject.stub(:cities) { cities } CalculatesRoute.should_receive(:calculate).with(cities) subject.route end + it "should returns the route from CalculatesRoute" do route_stub = [stub, stub] CalculatesRoute.stub(:calculate) { route_stub } subject.route.should eq(route_stub) end + + it 'should be able to choose his/her starting point' do + city = Place.build("Dallas, TX") + another_city = Place.build("El Paso, TX", true) + subject.schedule_city(city) + subject.schedule_city(another_city) + subject.cities.should == [another_city, city] + end + end From 00319fcc2fbf69ea41c92af0ea793c79a0f7658b Mon Sep 17 00:00:00 2001 From: drammopo Date: Tue, 25 Feb 2014 22:04:34 +0200 Subject: [PATCH 03/16] Tiger Level: Added Benchmarking code. --- salesperson_bm.rb | 52 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 salesperson_bm.rb diff --git a/salesperson_bm.rb b/salesperson_bm.rb new file mode 100644 index 0000000..a40e5c2 --- /dev/null +++ b/salesperson_bm.rb @@ -0,0 +1,52 @@ +require 'benchmark' +Dir["./lib/*.rb"].each {|file| require file } + +all_cities = ["Abilene", "Alamo", "Alamo Heights", "Alice", "Allen", "Alpine", "Alvarado", "Alvin", "Amarillo", "Andrews", "Angleton", "Argyle", "Arlington", "Austin", "Azle", "Balch Springs", "Bastrop", "Bay City", "Baytown", "Beaumont", "Bellaire", "Belton", "Benbrook", "Big Spring", "Boerne", "Brenham", "Bridgeport", "Brownfield", "Brownsville", "Brownwood", "Bryan", "Bulverde", "Burkburnett", "Burleson", "Burnet", "Canyon", "Carrollton", "Carthage", "Cedar Hill", "Cedar Park", "Cleburne", "Clute", "College Station", "Colleyville", "Columbus", "Conroe", "Converse", "Coppell", "Copperas Cove", "Corinth", "Corpus Christi", "Corsicana", "Crockett", "Crowley", "Dallas", "Deer Park", "Del Rio", "Denison", "Denton", "Desoto", "Devine", "Dickinson", "Donna", "Dumas", "Duncanville", "Eagle Pass", "Edinburg", "El Campo", "El Paso", "Ennis", "Euless", "Farmers Branch", "Flower Mound", "Fort Stockton", "Fort Worth", "Fredericksburg", "Freeport", "Friendswood", "Frisco", "Gainesville", "Galveston", "Garden Ridge", "Garland", "Gatesville", "Georgetown", "Gonzales", "Granbury", "Grand Prairie", "Granite Shoals", "Grapevine", "Greenville", "Haltom City", "Hamilton", "Harker Heights", "Harlingen", "Hearne", "Henderson", "Hewitt", "Highland Park", "Highland Village", "Houston", "Humble", "Hunters Creek Village", "Huntsville", "Hurst", "Irving", "Jacksonville", "Jasper", "Jersey Village", "Katy", "Keller", "Kemah", "Kennedale", "Kerrville", "Killeen", "Kingsville", "Krum", "Kyle", "La Grange", "La Porte", "Lacy Lakeview", "Lago Vista", "Lake Jackson", "Lakeway", "Lamesa", "Lampasas", "Lancaster", "Laredo", "League City", "Leander", "Leon Valley", "Levelland", "Lewisville", "Littlefield", "Live Oak", "Lockhart", "Longview", "Lowry Crossing", "Lubbock", "Lucas", "Lufkin", "Mansfield", "Manvel", "Marble Falls", "Marshall", "McAllen", "McKinney", "Mesquite", "Midland", "Midlothian", "Mission", "Missouri City", "Mount Pleasant", "Muleshoe", "Murphy", "Nacogdoches", "Nassau Bay", "New Braunfels", "Newark", "Newton", "North Richland Hills", "Oak Ridge North", "Odessa", "Orange", "Overton", "Palacios", "Palestine", "Pampa", "Paris", "Pasadena", "Pearland", "Perryton", "Pflugerville", "Plainview", "Plano", "Port Aransas", "Port Arthur", "Port Lavaca", "Portland", "Richardson", "Richland Hills", "Ridge City", "Rio Grande City", "River Oaks", "Rockport", "Rockwall", "Rosenberg", "Round Rock", "Rowlett", "Royse City", "Rusk", "Sachse", "Saginaw", "San Angelo", "San Antonio", "San Benito", "San Marcos", "San Saba", "Santa Fe", "Schertz", "Seabrook", "Sealy", "Seguin", "Selma", "Seymour", "Shenandoah", "Sherman", "Smithville", "Snyder", "Socorro", "Sonora", "Southlake", "Southside Place", "Spring Valley", "Stafford", "Stephenville", "Sugar Land", "Sulphur Springs", "Sweeny", "Tahoka", "Taylor", "Temple", "Terrell", "Texarkana", "Texas City", "The Colony", "The Woodlands", "Tomball", "Tyler", "Universal City", "University Park", "Victoria", "Waco", "Watauga", "Waxahachie", "Weatherford", "Webster", "Weslaco", "West Columbia", "West Lake Hills", "West Orange", "West University Place", "Weston", "Wharton", "White Settlement", "Wichita Falls", "Willow Park", "Windcrest", "Woodway", "Wylie", "Yoakum"] + +puts "---------Benchmarking 2 city route---------" +phil = SalesPerson.new +all_cities.shuffle.take(2).each do |city| + phil.schedule_city(Place.build("#{city}, TX")) +end + +Benchmark.bm do |x| + x.report do + puts phil.route + end +end + +puts "\n\n---------Benchmarking 10 city route---------" +bob = SalesPerson.new +all_cities.shuffle.take(10).each do |city| + bob.schedule_city(Place.build("#{city}, TX")) +end + +Benchmark.bm do |x| + x.report do + puts bob.route + end +end + +puts "\n\n---------Benchmarking 50 city route---------" +ted = SalesPerson.new +all_cities.shuffle.take(50).each do |city| + bob.schedule_city(Place.build("#{city}, TX")) +end + +Benchmark.bm do |x| + x.report do + puts ted.route + end +end + +puts "\n\n---------Benchmarking 200 city route---------" +chuck = SalesPerson.new +all_cities.shuffle.take(200).each do |city| + chuck.schedule_city(Place.build("#{city}, TX")) +end + +Benchmark.bm do |x| + x.report do + puts chuck.route + end +end From 6470ca04cb26adbed9db4c94f5f5190a3d61f904 Mon Sep 17 00:00:00 2001 From: drammopo Date: Tue, 25 Feb 2014 22:08:51 +0200 Subject: [PATCH 04/16] Removed PUTS output. --- salesperson_bm.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/salesperson_bm.rb b/salesperson_bm.rb index a40e5c2..a1700d6 100644 --- a/salesperson_bm.rb +++ b/salesperson_bm.rb @@ -11,7 +11,7 @@ Benchmark.bm do |x| x.report do - puts phil.route + phil.route end end @@ -23,7 +23,7 @@ Benchmark.bm do |x| x.report do - puts bob.route + bob.route end end @@ -35,7 +35,7 @@ Benchmark.bm do |x| x.report do - puts ted.route + ted.route end end @@ -47,6 +47,6 @@ Benchmark.bm do |x| x.report do - puts chuck.route + chuck.route end end From 75abbea1371d8b183511e6a39be1c29820ce2f28 Mon Sep 17 00:00:00 2001 From: drammopo Date: Wed, 26 Feb 2014 05:34:10 -0800 Subject: [PATCH 05/16] Refactored the code. DRY,DRY, DRY. --- salesperson_bm.rb | 49 +++++++++++------------------------------------ 1 file changed, 11 insertions(+), 38 deletions(-) diff --git a/salesperson_bm.rb b/salesperson_bm.rb index a1700d6..eef682a 100644 --- a/salesperson_bm.rb +++ b/salesperson_bm.rb @@ -1,52 +1,25 @@ require 'benchmark' + Dir["./lib/*.rb"].each {|file| require file } all_cities = ["Abilene", "Alamo", "Alamo Heights", "Alice", "Allen", "Alpine", "Alvarado", "Alvin", "Amarillo", "Andrews", "Angleton", "Argyle", "Arlington", "Austin", "Azle", "Balch Springs", "Bastrop", "Bay City", "Baytown", "Beaumont", "Bellaire", "Belton", "Benbrook", "Big Spring", "Boerne", "Brenham", "Bridgeport", "Brownfield", "Brownsville", "Brownwood", "Bryan", "Bulverde", "Burkburnett", "Burleson", "Burnet", "Canyon", "Carrollton", "Carthage", "Cedar Hill", "Cedar Park", "Cleburne", "Clute", "College Station", "Colleyville", "Columbus", "Conroe", "Converse", "Coppell", "Copperas Cove", "Corinth", "Corpus Christi", "Corsicana", "Crockett", "Crowley", "Dallas", "Deer Park", "Del Rio", "Denison", "Denton", "Desoto", "Devine", "Dickinson", "Donna", "Dumas", "Duncanville", "Eagle Pass", "Edinburg", "El Campo", "El Paso", "Ennis", "Euless", "Farmers Branch", "Flower Mound", "Fort Stockton", "Fort Worth", "Fredericksburg", "Freeport", "Friendswood", "Frisco", "Gainesville", "Galveston", "Garden Ridge", "Garland", "Gatesville", "Georgetown", "Gonzales", "Granbury", "Grand Prairie", "Granite Shoals", "Grapevine", "Greenville", "Haltom City", "Hamilton", "Harker Heights", "Harlingen", "Hearne", "Henderson", "Hewitt", "Highland Park", "Highland Village", "Houston", "Humble", "Hunters Creek Village", "Huntsville", "Hurst", "Irving", "Jacksonville", "Jasper", "Jersey Village", "Katy", "Keller", "Kemah", "Kennedale", "Kerrville", "Killeen", "Kingsville", "Krum", "Kyle", "La Grange", "La Porte", "Lacy Lakeview", "Lago Vista", "Lake Jackson", "Lakeway", "Lamesa", "Lampasas", "Lancaster", "Laredo", "League City", "Leander", "Leon Valley", "Levelland", "Lewisville", "Littlefield", "Live Oak", "Lockhart", "Longview", "Lowry Crossing", "Lubbock", "Lucas", "Lufkin", "Mansfield", "Manvel", "Marble Falls", "Marshall", "McAllen", "McKinney", "Mesquite", "Midland", "Midlothian", "Mission", "Missouri City", "Mount Pleasant", "Muleshoe", "Murphy", "Nacogdoches", "Nassau Bay", "New Braunfels", "Newark", "Newton", "North Richland Hills", "Oak Ridge North", "Odessa", "Orange", "Overton", "Palacios", "Palestine", "Pampa", "Paris", "Pasadena", "Pearland", "Perryton", "Pflugerville", "Plainview", "Plano", "Port Aransas", "Port Arthur", "Port Lavaca", "Portland", "Richardson", "Richland Hills", "Ridge City", "Rio Grande City", "River Oaks", "Rockport", "Rockwall", "Rosenberg", "Round Rock", "Rowlett", "Royse City", "Rusk", "Sachse", "Saginaw", "San Angelo", "San Antonio", "San Benito", "San Marcos", "San Saba", "Santa Fe", "Schertz", "Seabrook", "Sealy", "Seguin", "Selma", "Seymour", "Shenandoah", "Sherman", "Smithville", "Snyder", "Socorro", "Sonora", "Southlake", "Southside Place", "Spring Valley", "Stafford", "Stephenville", "Sugar Land", "Sulphur Springs", "Sweeny", "Tahoka", "Taylor", "Temple", "Terrell", "Texarkana", "Texas City", "The Colony", "The Woodlands", "Tomball", "Tyler", "Universal City", "University Park", "Victoria", "Waco", "Watauga", "Waxahachie", "Weatherford", "Webster", "Weslaco", "West Columbia", "West Lake Hills", "West Orange", "West University Place", "Weston", "Wharton", "White Settlement", "Wichita Falls", "Willow Park", "Windcrest", "Woodway", "Wylie", "Yoakum"] -puts "---------Benchmarking 2 city route---------" -phil = SalesPerson.new -all_cities.shuffle.take(2).each do |city| - phil.schedule_city(Place.build("#{city}, TX")) -end - -Benchmark.bm do |x| - x.report do - phil.route - end -end -puts "\n\n---------Benchmarking 10 city route---------" -bob = SalesPerson.new -all_cities.shuffle.take(10).each do |city| - bob.schedule_city(Place.build("#{city}, TX")) -end -Benchmark.bm do |x| - x.report do - bob.route - end -end +def run_route(n) +salesperson = SalesPerson.new -puts "\n\n---------Benchmarking 50 city route---------" -ted = SalesPerson.new -all_cities.shuffle.take(50).each do |city| - bob.schedule_city(Place.build("#{city}, TX")) -end +all_cities = ["Abilene", "Alamo", "Alamo Heights", "Alice", "Allen", "Alpine", "Alvarado", "Alvin", "Amarillo", "Andrews", "Angleton", "Argyle", "Arlington", "Austin", "Azle", "Balch Springs", "Bastrop", "Bay City", "Baytown", "Beaumont", "Bellaire", "Belton", "Benbrook", "Big Spring", "Boerne", "Brenham", "Bridgeport", "Brownfield", "Brownsville", "Brownwood", "Bryan", "Bulverde", "Burkburnett", "Burleson", "Burnet", "Canyon", "Carrollton", "Carthage", "Cedar Hill", "Cedar Park", "Cleburne", "Clute", "College Station", "Colleyville", "Columbus", "Conroe", "Converse", "Coppell", "Copperas Cove", "Corinth", "Corpus Christi", "Corsicana", "Crockett", "Crowley", "Dallas", "Deer Park", "Del Rio", "Denison", "Denton", "Desoto", "Devine", "Dickinson", "Donna", "Dumas", "Duncanville", "Eagle Pass", "Edinburg", "El Campo", "El Paso", "Ennis", "Euless", "Farmers Branch", "Flower Mound", "Fort Stockton", "Fort Worth", "Fredericksburg", "Freeport", "Friendswood", "Frisco", "Gainesville", "Galveston", "Garden Ridge", "Garland", "Gatesville", "Georgetown", "Gonzales", "Granbury", "Grand Prairie", "Granite Shoals", "Grapevine", "Greenville", "Haltom City", "Hamilton", "Harker Heights", "Harlingen", "Hearne", "Henderson", "Hewitt", "Highland Park", "Highland Village", "Houston", "Humble", "Hunters Creek Village", "Huntsville", "Hurst", "Irving", "Jacksonville", "Jasper", "Jersey Village", "Katy", "Keller", "Kemah", "Kennedale", "Kerrville", "Killeen", "Kingsville", "Krum", "Kyle", "La Grange", "La Porte", "Lacy Lakeview", "Lago Vista", "Lake Jackson", "Lakeway", "Lamesa", "Lampasas", "Lancaster", "Laredo", "League City", "Leander", "Leon Valley", "Levelland", "Lewisville", "Littlefield", "Live Oak", "Lockhart", "Longview", "Lowry Crossing", "Lubbock", "Lucas", "Lufkin", "Mansfield", "Manvel", "Marble Falls", "Marshall", "McAllen", "McKinney", "Mesquite", "Midland", "Midlothian", "Mission", "Missouri City", "Mount Pleasant", "Muleshoe", "Murphy", "Nacogdoches", "Nassau Bay", "New Braunfels", "Newark", "Newton", "North Richland Hills", "Oak Ridge North", "Odessa", "Orange", "Overton", "Palacios", "Palestine", "Pampa", "Paris", "Pasadena", "Pearland", "Perryton", "Pflugerville", "Plainview", "Plano", "Port Aransas", "Port Arthur", "Port Lavaca", "Portland", "Richardson", "Richland Hills", "Ridge City", "Rio Grande City", "River Oaks", "Rockport", "Rockwall", "Rosenberg", "Round Rock", "Rowlett", "Royse City", "Rusk", "Sachse", "Saginaw", "San Angelo", "San Antonio", "San Benito", "San Marcos", "San Saba", "Santa Fe", "Schertz", "Seabrook", "Sealy", "Seguin", "Selma", "Seymour", "Shenandoah", "Sherman", "Smithville", "Snyder", "Socorro", "Sonora", "Southlake", "Southside Place", "Spring Valley", "Stafford", "Stephenville", "Sugar Land", "Sulphur Springs", "Sweeny", "Tahoka", "Taylor", "Temple", "Terrell", "Texarkana", "Texas City", "The Colony", "The Woodlands", "Tomball", "Tyler", "Universal City", "University Park", "Victoria", "Waco", "Watauga", "Waxahachie", "Weatherford", "Webster", "Weslaco", "West Columbia", "West Lake Hills", "West Orange", "West University Place", "Weston", "Wharton", "White Settlement", "Wichita Falls", "Willow Park", "Windcrest", "Woodway", "Wylie", "Yoakum"] -Benchmark.bm do |x| - x.report do - ted.route + all_cities.shuffle.take(n).each do |city| + salesperson.schedule_city(Place.build("#{city}, TX")) end end -puts "\n\n---------Benchmarking 200 city route---------" -chuck = SalesPerson.new -all_cities.shuffle.take(200).each do |city| - chuck.schedule_city(Place.build("#{city}, TX")) -end Benchmark.bm do |x| - x.report do - chuck.route - end -end + x.report("Benchmarking 2 city route:") { run_route(2) } + x.report("Benchmarking 10 city route:") { } + x.report("Benchmarking 50 city route:") { } + x.report("Benchmarking 200 city route:") { } +end \ No newline at end of file From dfcfba710037ee130ae7fc30fa0f8bf3bb229daf Mon Sep 17 00:00:00 2001 From: drammopo Date: Wed, 26 Feb 2014 05:44:16 -0800 Subject: [PATCH 06/16] Uncommented the rest of the benchmark calls. --- salesperson_bm.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/salesperson_bm.rb b/salesperson_bm.rb index eef682a..0a9a692 100644 --- a/salesperson_bm.rb +++ b/salesperson_bm.rb @@ -19,7 +19,7 @@ def run_route(n) Benchmark.bm do |x| x.report("Benchmarking 2 city route:") { run_route(2) } - x.report("Benchmarking 10 city route:") { } - x.report("Benchmarking 50 city route:") { } - x.report("Benchmarking 200 city route:") { } + x.report("Benchmarking 10 city route:") { run_route(10) } + x.report("Benchmarking 50 city route:") { run_route(50) } + x.report("Benchmarking 200 city route:") { run_route(100) } end \ No newline at end of file From 2b173d5410054adc0d9ce9ba0ba85680b35416a9 Mon Sep 17 00:00:00 2001 From: drammopo Date: Wed, 26 Feb 2014 17:54:27 +0200 Subject: [PATCH 07/16] Removed duplicate cities --- salesperson_bm.rb | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/salesperson_bm.rb b/salesperson_bm.rb index 0a9a692..34d33dd 100644 --- a/salesperson_bm.rb +++ b/salesperson_bm.rb @@ -2,17 +2,13 @@ Dir["./lib/*.rb"].each {|file| require file } -all_cities = ["Abilene", "Alamo", "Alamo Heights", "Alice", "Allen", "Alpine", "Alvarado", "Alvin", "Amarillo", "Andrews", "Angleton", "Argyle", "Arlington", "Austin", "Azle", "Balch Springs", "Bastrop", "Bay City", "Baytown", "Beaumont", "Bellaire", "Belton", "Benbrook", "Big Spring", "Boerne", "Brenham", "Bridgeport", "Brownfield", "Brownsville", "Brownwood", "Bryan", "Bulverde", "Burkburnett", "Burleson", "Burnet", "Canyon", "Carrollton", "Carthage", "Cedar Hill", "Cedar Park", "Cleburne", "Clute", "College Station", "Colleyville", "Columbus", "Conroe", "Converse", "Coppell", "Copperas Cove", "Corinth", "Corpus Christi", "Corsicana", "Crockett", "Crowley", "Dallas", "Deer Park", "Del Rio", "Denison", "Denton", "Desoto", "Devine", "Dickinson", "Donna", "Dumas", "Duncanville", "Eagle Pass", "Edinburg", "El Campo", "El Paso", "Ennis", "Euless", "Farmers Branch", "Flower Mound", "Fort Stockton", "Fort Worth", "Fredericksburg", "Freeport", "Friendswood", "Frisco", "Gainesville", "Galveston", "Garden Ridge", "Garland", "Gatesville", "Georgetown", "Gonzales", "Granbury", "Grand Prairie", "Granite Shoals", "Grapevine", "Greenville", "Haltom City", "Hamilton", "Harker Heights", "Harlingen", "Hearne", "Henderson", "Hewitt", "Highland Park", "Highland Village", "Houston", "Humble", "Hunters Creek Village", "Huntsville", "Hurst", "Irving", "Jacksonville", "Jasper", "Jersey Village", "Katy", "Keller", "Kemah", "Kennedale", "Kerrville", "Killeen", "Kingsville", "Krum", "Kyle", "La Grange", "La Porte", "Lacy Lakeview", "Lago Vista", "Lake Jackson", "Lakeway", "Lamesa", "Lampasas", "Lancaster", "Laredo", "League City", "Leander", "Leon Valley", "Levelland", "Lewisville", "Littlefield", "Live Oak", "Lockhart", "Longview", "Lowry Crossing", "Lubbock", "Lucas", "Lufkin", "Mansfield", "Manvel", "Marble Falls", "Marshall", "McAllen", "McKinney", "Mesquite", "Midland", "Midlothian", "Mission", "Missouri City", "Mount Pleasant", "Muleshoe", "Murphy", "Nacogdoches", "Nassau Bay", "New Braunfels", "Newark", "Newton", "North Richland Hills", "Oak Ridge North", "Odessa", "Orange", "Overton", "Palacios", "Palestine", "Pampa", "Paris", "Pasadena", "Pearland", "Perryton", "Pflugerville", "Plainview", "Plano", "Port Aransas", "Port Arthur", "Port Lavaca", "Portland", "Richardson", "Richland Hills", "Ridge City", "Rio Grande City", "River Oaks", "Rockport", "Rockwall", "Rosenberg", "Round Rock", "Rowlett", "Royse City", "Rusk", "Sachse", "Saginaw", "San Angelo", "San Antonio", "San Benito", "San Marcos", "San Saba", "Santa Fe", "Schertz", "Seabrook", "Sealy", "Seguin", "Selma", "Seymour", "Shenandoah", "Sherman", "Smithville", "Snyder", "Socorro", "Sonora", "Southlake", "Southside Place", "Spring Valley", "Stafford", "Stephenville", "Sugar Land", "Sulphur Springs", "Sweeny", "Tahoka", "Taylor", "Temple", "Terrell", "Texarkana", "Texas City", "The Colony", "The Woodlands", "Tomball", "Tyler", "Universal City", "University Park", "Victoria", "Waco", "Watauga", "Waxahachie", "Weatherford", "Webster", "Weslaco", "West Columbia", "West Lake Hills", "West Orange", "West University Place", "Weston", "Wharton", "White Settlement", "Wichita Falls", "Willow Park", "Windcrest", "Woodway", "Wylie", "Yoakum"] - - - def run_route(n) -salesperson = SalesPerson.new + salesperson = SalesPerson.new -all_cities = ["Abilene", "Alamo", "Alamo Heights", "Alice", "Allen", "Alpine", "Alvarado", "Alvin", "Amarillo", "Andrews", "Angleton", "Argyle", "Arlington", "Austin", "Azle", "Balch Springs", "Bastrop", "Bay City", "Baytown", "Beaumont", "Bellaire", "Belton", "Benbrook", "Big Spring", "Boerne", "Brenham", "Bridgeport", "Brownfield", "Brownsville", "Brownwood", "Bryan", "Bulverde", "Burkburnett", "Burleson", "Burnet", "Canyon", "Carrollton", "Carthage", "Cedar Hill", "Cedar Park", "Cleburne", "Clute", "College Station", "Colleyville", "Columbus", "Conroe", "Converse", "Coppell", "Copperas Cove", "Corinth", "Corpus Christi", "Corsicana", "Crockett", "Crowley", "Dallas", "Deer Park", "Del Rio", "Denison", "Denton", "Desoto", "Devine", "Dickinson", "Donna", "Dumas", "Duncanville", "Eagle Pass", "Edinburg", "El Campo", "El Paso", "Ennis", "Euless", "Farmers Branch", "Flower Mound", "Fort Stockton", "Fort Worth", "Fredericksburg", "Freeport", "Friendswood", "Frisco", "Gainesville", "Galveston", "Garden Ridge", "Garland", "Gatesville", "Georgetown", "Gonzales", "Granbury", "Grand Prairie", "Granite Shoals", "Grapevine", "Greenville", "Haltom City", "Hamilton", "Harker Heights", "Harlingen", "Hearne", "Henderson", "Hewitt", "Highland Park", "Highland Village", "Houston", "Humble", "Hunters Creek Village", "Huntsville", "Hurst", "Irving", "Jacksonville", "Jasper", "Jersey Village", "Katy", "Keller", "Kemah", "Kennedale", "Kerrville", "Killeen", "Kingsville", "Krum", "Kyle", "La Grange", "La Porte", "Lacy Lakeview", "Lago Vista", "Lake Jackson", "Lakeway", "Lamesa", "Lampasas", "Lancaster", "Laredo", "League City", "Leander", "Leon Valley", "Levelland", "Lewisville", "Littlefield", "Live Oak", "Lockhart", "Longview", "Lowry Crossing", "Lubbock", "Lucas", "Lufkin", "Mansfield", "Manvel", "Marble Falls", "Marshall", "McAllen", "McKinney", "Mesquite", "Midland", "Midlothian", "Mission", "Missouri City", "Mount Pleasant", "Muleshoe", "Murphy", "Nacogdoches", "Nassau Bay", "New Braunfels", "Newark", "Newton", "North Richland Hills", "Oak Ridge North", "Odessa", "Orange", "Overton", "Palacios", "Palestine", "Pampa", "Paris", "Pasadena", "Pearland", "Perryton", "Pflugerville", "Plainview", "Plano", "Port Aransas", "Port Arthur", "Port Lavaca", "Portland", "Richardson", "Richland Hills", "Ridge City", "Rio Grande City", "River Oaks", "Rockport", "Rockwall", "Rosenberg", "Round Rock", "Rowlett", "Royse City", "Rusk", "Sachse", "Saginaw", "San Angelo", "San Antonio", "San Benito", "San Marcos", "San Saba", "Santa Fe", "Schertz", "Seabrook", "Sealy", "Seguin", "Selma", "Seymour", "Shenandoah", "Sherman", "Smithville", "Snyder", "Socorro", "Sonora", "Southlake", "Southside Place", "Spring Valley", "Stafford", "Stephenville", "Sugar Land", "Sulphur Springs", "Sweeny", "Tahoka", "Taylor", "Temple", "Terrell", "Texarkana", "Texas City", "The Colony", "The Woodlands", "Tomball", "Tyler", "Universal City", "University Park", "Victoria", "Waco", "Watauga", "Waxahachie", "Weatherford", "Webster", "Weslaco", "West Columbia", "West Lake Hills", "West Orange", "West University Place", "Weston", "Wharton", "White Settlement", "Wichita Falls", "Willow Park", "Windcrest", "Woodway", "Wylie", "Yoakum"] + all_cities = ["Abilene", "Alamo", "Alamo Heights", "Alice", "Allen", "Alpine", "Alvarado", "Alvin", "Amarillo", "Andrews", "Angleton", "Argyle", "Arlington", "Austin", "Azle", "Balch Springs", "Bastrop", "Bay City", "Baytown", "Beaumont", "Bellaire", "Belton", "Benbrook", "Big Spring", "Boerne", "Brenham", "Bridgeport", "Brownfield", "Brownsville", "Brownwood", "Bryan", "Bulverde", "Burkburnett", "Burleson", "Burnet", "Canyon", "Carrollton", "Carthage", "Cedar Hill", "Cedar Park", "Cleburne", "Clute", "College Station", "Colleyville", "Columbus", "Conroe", "Converse", "Coppell", "Copperas Cove", "Corinth", "Corpus Christi", "Corsicana", "Crockett", "Crowley", "Dallas", "Deer Park", "Del Rio", "Denison", "Denton", "Desoto", "Devine", "Dickinson", "Donna", "Dumas", "Duncanville", "Eagle Pass", "Edinburg", "El Campo", "El Paso", "Ennis", "Euless", "Farmers Branch", "Flower Mound", "Fort Stockton", "Fort Worth", "Fredericksburg", "Freeport", "Friendswood", "Frisco", "Gainesville", "Galveston", "Garden Ridge", "Garland", "Gatesville", "Georgetown", "Gonzales", "Granbury", "Grand Prairie", "Granite Shoals", "Grapevine", "Greenville", "Haltom City", "Hamilton", "Harker Heights", "Harlingen", "Hearne", "Henderson", "Hewitt", "Highland Park", "Highland Village", "Houston", "Humble", "Hunters Creek Village", "Huntsville", "Hurst", "Irving", "Jacksonville", "Jasper", "Jersey Village", "Katy", "Keller", "Kemah", "Kennedale", "Kerrville", "Killeen", "Kingsville", "Krum", "Kyle", "La Grange", "La Porte", "Lacy Lakeview", "Lago Vista", "Lake Jackson", "Lakeway", "Lamesa", "Lampasas", "Lancaster", "Laredo", "League City", "Leander", "Leon Valley", "Levelland", "Lewisville", "Littlefield", "Live Oak", "Lockhart", "Longview", "Lowry Crossing", "Lubbock", "Lucas", "Lufkin", "Mansfield", "Manvel", "Marble Falls", "Marshall", "McAllen", "McKinney", "Mesquite", "Midland", "Midlothian", "Mission", "Missouri City", "Mount Pleasant", "Muleshoe", "Murphy", "Nacogdoches", "Nassau Bay", "New Braunfels", "Newark", "Newton", "North Richland Hills", "Oak Ridge North", "Odessa", "Orange", "Overton", "Palacios", "Palestine", "Pampa", "Paris", "Pasadena", "Pearland", "Perryton", "Pflugerville", "Plainview", "Plano", "Port Aransas", "Port Arthur", "Port Lavaca", "Portland", "Richardson", "Richland Hills", "Ridge City", "Rio Grande City", "River Oaks", "Rockport", "Rockwall", "Rosenberg", "Round Rock", "Rowlett", "Royse City", "Rusk", "Sachse", "Saginaw", "San Angelo", "San Antonio", "San Benito", "San Marcos", "San Saba", "Santa Fe", "Schertz", "Seabrook", "Sealy", "Seguin", "Selma", "Seymour", "Shenandoah", "Sherman", "Smithville", "Snyder", "Socorro", "Sonora", "Southlake", "Southside Place", "Spring Valley", "Stafford", "Stephenville", "Sugar Land", "Sulphur Springs", "Sweeny", "Tahoka", "Taylor", "Temple", "Terrell", "Texarkana", "Texas City", "The Colony", "The Woodlands", "Tomball", "Tyler", "Universal City", "University Park", "Victoria", "Waco", "Watauga", "Waxahachie", "Weatherford", "Webster", "Weslaco", "West Columbia", "West Lake Hills", "West Orange", "West University Place", "Weston", "Wharton", "White Settlement", "Wichita Falls", "Willow Park", "Windcrest", "Woodway", "Wylie", "Yoakum"] all_cities.shuffle.take(n).each do |city| - salesperson.schedule_city(Place.build("#{city}, TX")) + salesperson.schedule_city(Place.build("#{city}, TX")) end end @@ -22,4 +18,4 @@ def run_route(n) x.report("Benchmarking 10 city route:") { run_route(10) } x.report("Benchmarking 50 city route:") { run_route(50) } x.report("Benchmarking 200 city route:") { run_route(100) } -end \ No newline at end of file +end From c919e668d670c4ff3d77550863c3b6730bfc5362 Mon Sep 17 00:00:00 2001 From: drammopo Date: Wed, 26 Feb 2014 23:07:06 +0200 Subject: [PATCH 08/16] Added distance of route calculation. --- lib/calculates_route.rb | 10 +++++++++- lib/sales_person.rb | 14 ++++++++++---- salesperson_bm.rb => sales_person_bm.rb | 0 salesperson.rb | 7 +++++-- spec/sales_person_spec.rb | 10 ++++++++++ 5 files changed, 34 insertions(+), 7 deletions(-) rename salesperson_bm.rb => sales_person_bm.rb (100%) diff --git a/lib/calculates_route.rb b/lib/calculates_route.rb index 4488393..8a478c2 100644 --- a/lib/calculates_route.rb +++ b/lib/calculates_route.rb @@ -5,7 +5,7 @@ def self.calculate(points) remaining_points = points route = [] route << remaining_points.slice!(0) - until remaining_points == [] do + until remaining_points == [] do next_point = shortest_distance(route.last, remaining_points) route << remaining_points.slice!(remaining_points.index(next_point)) end @@ -18,5 +18,13 @@ def self.shortest_distance(from, possible) end distances.sort{|a,b| a.fetch(:distance) <=> b.fetch(:distance)}.first.fetch(:point) end + + def self.distance(route) + total_distance = 0.0 + route.each_cons(2) do |route_combo| + total_distance += Map.distance_between(route.first,route.last) + end + return total_distance + end end diff --git a/lib/sales_person.rb b/lib/sales_person.rb index 28635ca..6a5b7d7 100644 --- a/lib/sales_person.rb +++ b/lib/sales_person.rb @@ -3,19 +3,25 @@ class SalesPerson attr_reader :cities def initialize @cities = [] + @routed_cities = [] end def schedule_city(city) @cities << city unless @cities.include?(city) - @cities.each_with_index do |city, index| - if city.starting_point == true - @cities.insert(0, cities.delete_at(index)) + @cities.each_index do |index| + if @cities[index].starting_point == true + @cities.insert(0, @cities.delete_at(index)) end end + @cities end def route - CalculatesRoute.calculate(cities) + @routed_cities = CalculatesRoute.calculate(cities) + end + + def distance_of_route + CalculatesRoute.distance(@routed_cities) end end diff --git a/salesperson_bm.rb b/sales_person_bm.rb similarity index 100% rename from salesperson_bm.rb rename to sales_person_bm.rb diff --git a/salesperson.rb b/salesperson.rb index b0d8f82..9e585af 100644 --- a/salesperson.rb +++ b/salesperson.rb @@ -3,8 +3,11 @@ phil = SalesPerson.new phil.schedule_city(Place.build("Dallas, TX")) -phil.schedule_city(Place.build("El Paso, TX", true)) -phil.schedule_city(Place.build("Austin, TX")) +phil.schedule_city(Place.build("El Paso, TX")) +phil.schedule_city(Place.build("Austin, TX", true)) phil.schedule_city(Place.build("Lubbock, TX")) +#puts phil.cities.inspect => [#, #, #, #] puts phil.route +puts phil.distance_of_route +#puts phil.cities.inspect => [] diff --git a/spec/sales_person_spec.rb b/spec/sales_person_spec.rb index c897a89..a8950c0 100644 --- a/spec/sales_person_spec.rb +++ b/spec/sales_person_spec.rb @@ -39,4 +39,14 @@ subject.cities.should == [another_city, city] end + it 'should log the total miles' do + city = Place.build("Dallas, TX") + another_city = Place.build("El Paso, TX", true) + subject.schedule_city(city) + subject.schedule_city(another_city) + subject.schedule_city(Place.build("Austin, TX")) + #subject.route_distance.should be_within(0.01).of(570.5181717964364) + puts subject.distance_of_route + end + end From 39e0a906f502760062f96cda82dfbdb9d36c5694 Mon Sep 17 00:00:00 2001 From: drammopo Date: Thu, 27 Feb 2014 23:25:44 +0200 Subject: [PATCH 09/16] Added travel time calculation. --- lib/calculates_route.rb | 7 +++++-- lib/sales_person.rb | 19 ++++++++++++------- salesperson.rb | 14 ++++++++++---- spec/sales_person_spec.rb | 39 ++++++++++++++++++++++++--------------- 4 files changed, 51 insertions(+), 28 deletions(-) diff --git a/lib/calculates_route.rb b/lib/calculates_route.rb index 8a478c2..98a093c 100644 --- a/lib/calculates_route.rb +++ b/lib/calculates_route.rb @@ -1,8 +1,7 @@ class CalculatesRoute def self.calculate(points) - - remaining_points = points + remaining_points = points.clone route = [] route << remaining_points.slice!(0) until remaining_points == [] do @@ -26,5 +25,9 @@ def self.distance(route) end return total_distance end + + def self.time(route,speed) + (distance(route) / speed).to_f + end end diff --git a/lib/sales_person.rb b/lib/sales_person.rb index 6a5b7d7..d610cee 100644 --- a/lib/sales_person.rb +++ b/lib/sales_person.rb @@ -1,19 +1,20 @@ class SalesPerson - attr_reader :cities + MILES_PER_HOUR = 55 + attr_reader :cities, :routed_cities def initialize @cities = [] @routed_cities = [] end def schedule_city(city) - @cities << city unless @cities.include?(city) - @cities.each_index do |index| - if @cities[index].starting_point == true - @cities.insert(0, @cities.delete_at(index)) + cities << city unless cities.include?(city) + cities.each_index do |index| + if cities[index].starting_point == true + cities.insert(0, cities.delete_at(index)) end end - @cities + cities end def route @@ -21,7 +22,11 @@ def route end def distance_of_route - CalculatesRoute.distance(@routed_cities) + CalculatesRoute.distance(routed_cities) + end + + def travel_time + CalculatesRoute.time(routed_cities, MILES_PER_HOUR) end end diff --git a/salesperson.rb b/salesperson.rb index 9e585af..444464a 100644 --- a/salesperson.rb +++ b/salesperson.rb @@ -7,7 +7,13 @@ phil.schedule_city(Place.build("Austin, TX", true)) phil.schedule_city(Place.build("Lubbock, TX")) -#puts phil.cities.inspect => [#, #, #, #] -puts phil.route -puts phil.distance_of_route -#puts phil.cities.inspect => [] +puts "The original route was:" +phil.cities.each do |city| + puts city +end +puts "\nThe new route is:" +phil.route.each do |city| + puts city +end +puts "There are a total of #{phil.distance_of_route.round(2)} miles on the route." +puts "The total travel time is #{phil.travel_time.round(2)} hours" diff --git a/spec/sales_person_spec.rb b/spec/sales_person_spec.rb index a8950c0..eab5ea5 100644 --- a/spec/sales_person_spec.rb +++ b/spec/sales_person_spec.rb @@ -4,6 +4,7 @@ require_relative "../lib/place" describe SalesPerson do + xit "should have many cities" do city = stub subject.schedule_city(city) @@ -31,22 +32,30 @@ subject.route.should eq(route_stub) end - it 'should be able to choose his/her starting point' do - city = Place.build("Dallas, TX") - another_city = Place.build("El Paso, TX", true) - subject.schedule_city(city) - subject.schedule_city(another_city) - subject.cities.should == [another_city, city] - end +context "working with live datasets" do - it 'should log the total miles' do - city = Place.build("Dallas, TX") - another_city = Place.build("El Paso, TX", true) - subject.schedule_city(city) - subject.schedule_city(another_city) - subject.schedule_city(Place.build("Austin, TX")) - #subject.route_distance.should be_within(0.01).of(570.5181717964364) - puts subject.distance_of_route + let (:city){Place.build("Dallas, TX")} + let (:another_city){Place.build("El Paso, TX", true)} + let (:yet_another_city){Place.build("El Paso, TX")} + + before :each do + subject.schedule_city(city) + subject.schedule_city(another_city) + subject.schedule_city(yet_another_city) + subject.route + end + + it 'should be able to choose his/her starting point' do + subject.cities.should == [another_city, city, yet_another_city] + end + + it 'should log the total miles for the route' do + subject.distance_of_route.should be_within(201.1).of(1100) #=> 1141.036 + end + + it 'should output the total traveling time (assuming 55 mph)' do + subject.travel_time.should be_within(1).of(20) + end end end From cacd4c51ab33b13f2e7fc04624243cb8850a2296 Mon Sep 17 00:00:00 2001 From: drammopo Date: Fri, 28 Feb 2014 05:00:25 -0800 Subject: [PATCH 10/16] Added general housekeeping. --- lib/sales_person.rb | 8 +++----- sales_person_bm.rb | 8 ++++---- salesperson.rb | 4 ++-- spec/sales_person_spec.rb | 12 ++++++------ 4 files changed, 15 insertions(+), 17 deletions(-) diff --git a/lib/sales_person.rb b/lib/sales_person.rb index d610cee..a11414a 100644 --- a/lib/sales_person.rb +++ b/lib/sales_person.rb @@ -10,9 +10,7 @@ def initialize def schedule_city(city) cities << city unless cities.include?(city) cities.each_index do |index| - if cities[index].starting_point == true - cities.insert(0, cities.delete_at(index)) - end + cities.insert(0, cities.delete_at(index)) if cities[index].starting_point == true end cities end @@ -21,11 +19,11 @@ def route @routed_cities = CalculatesRoute.calculate(cities) end - def distance_of_route + def total_miles CalculatesRoute.distance(routed_cities) end - def travel_time + def total_travel_time CalculatesRoute.time(routed_cities, MILES_PER_HOUR) end diff --git a/sales_person_bm.rb b/sales_person_bm.rb index 34d33dd..0defb64 100644 --- a/sales_person_bm.rb +++ b/sales_person_bm.rb @@ -14,8 +14,8 @@ def run_route(n) Benchmark.bm do |x| - x.report("Benchmarking 2 city route:") { run_route(2) } - x.report("Benchmarking 10 city route:") { run_route(10) } - x.report("Benchmarking 50 city route:") { run_route(50) } - x.report("Benchmarking 200 city route:") { run_route(100) } + x.report("Benchmarking 2 city route:"){ run_route(2)} + x.report("Benchmarking 10 city route:"){ run_route(10)} + x.report("Benchmarking 50 city route:"){ run_route(50)} + x.report("Benchmarking 200 city route:"){ run_route(100)} end diff --git a/salesperson.rb b/salesperson.rb index 444464a..d00f4e5 100644 --- a/salesperson.rb +++ b/salesperson.rb @@ -15,5 +15,5 @@ phil.route.each do |city| puts city end -puts "There are a total of #{phil.distance_of_route.round(2)} miles on the route." -puts "The total travel time is #{phil.travel_time.round(2)} hours" +puts "There are a total of #{phil.total_miles.round(2)} miles on the route." +puts "The total travel time is #{phil.total_travel_time.round(2)} hours" diff --git a/spec/sales_person_spec.rb b/spec/sales_person_spec.rb index eab5ea5..a0d05a0 100644 --- a/spec/sales_person_spec.rb +++ b/spec/sales_person_spec.rb @@ -1,7 +1,7 @@ -require 'rspec' require_relative "../lib/sales_person" require_relative "../lib/calculates_route" require_relative "../lib/place" +require 'rspec' describe SalesPerson do @@ -12,7 +12,7 @@ end xit "should keep the cities only scheduled once" do - city = stub + city = double expect{ subject.schedule_city(city) subject.schedule_city(city) @@ -20,14 +20,14 @@ end it "should calculate a route via the CalculatesRoute" do - cities = [stub, stub, stub] + cities = [double, double, double] subject.stub(:cities) { cities } CalculatesRoute.should_receive(:calculate).with(cities) subject.route end it "should returns the route from CalculatesRoute" do - route_stub = [stub, stub] + route_stub = [double, double] CalculatesRoute.stub(:calculate) { route_stub } subject.route.should eq(route_stub) end @@ -50,11 +50,11 @@ end it 'should log the total miles for the route' do - subject.distance_of_route.should be_within(201.1).of(1100) #=> 1141.036 + subject.total_miles.should be_within(201.1).of(1100) end it 'should output the total traveling time (assuming 55 mph)' do - subject.travel_time.should be_within(1).of(20) + subject.total_travel_time.should be_within(1).of(20) end end From 4ab9bc20d25ccc4895d3033217e7f4fb78c6632c Mon Sep 17 00:00:00 2001 From: drammopo Date: Sun, 9 Mar 2014 07:30:35 -0700 Subject: [PATCH 11/16] Fixed stub to double calls with #starting point. --- spec/sales_person_spec.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/spec/sales_person_spec.rb b/spec/sales_person_spec.rb index a0d05a0..220b79b 100644 --- a/spec/sales_person_spec.rb +++ b/spec/sales_person_spec.rb @@ -5,14 +5,14 @@ describe SalesPerson do - xit "should have many cities" do - city = stub + it "should have many cities" do + city = double(:city, starting_point: false) subject.schedule_city(city) subject.cities.should include(city) end - xit "should keep the cities only scheduled once" do - city = double + it "should keep the cities only scheduled once" do + city = double(:city, starting_point: false) expect{ subject.schedule_city(city) subject.schedule_city(city) From 4b6f58a62bcfde0cbdc24d449e537fa256747991 Mon Sep 17 00:00:00 2001 From: drammopo Date: Mon, 10 Mar 2014 11:14:13 -0700 Subject: [PATCH 12/16] Added friendlier travel time output. --- lib/sales_person.rb | 6 +++++- salesperson.rb | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/sales_person.rb b/lib/sales_person.rb index a11414a..0fabf47 100644 --- a/lib/sales_person.rb +++ b/lib/sales_person.rb @@ -24,7 +24,11 @@ def total_miles end def total_travel_time - CalculatesRoute.time(routed_cities, MILES_PER_HOUR) + t = CalculatesRoute.time(routed_cities, MILES_PER_HOUR) * 60 * 60 + mm, ss = t.divmod(60) + hh, mm = mm.divmod(60) + dd, hh = hh.divmod(24) + "%d days, %d hours, %d minutes and %d seconds" % [dd, hh, mm, ss] end end diff --git a/salesperson.rb b/salesperson.rb index d00f4e5..ebf5bcc 100644 --- a/salesperson.rb +++ b/salesperson.rb @@ -16,4 +16,4 @@ puts city end puts "There are a total of #{phil.total_miles.round(2)} miles on the route." -puts "The total travel time is #{phil.total_travel_time.round(2)} hours" +puts "The total travel time is #{phil.total_travel_time}" \ No newline at end of file From 7df577c5f29c2afceed9273131b8f583774e2a78 Mon Sep 17 00:00:00 2001 From: drammopo Date: Mon, 10 Mar 2014 11:27:36 -0700 Subject: [PATCH 13/16] Added moved formatting to SalesPerson#formatted_total_travel_time. --- lib/sales_person.rb | 7 +++++-- sales_person_bm.rb | 8 ++++---- salesperson.rb | 2 +- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/sales_person.rb b/lib/sales_person.rb index 0fabf47..152bf8c 100644 --- a/lib/sales_person.rb +++ b/lib/sales_person.rb @@ -24,11 +24,14 @@ def total_miles end def total_travel_time - t = CalculatesRoute.time(routed_cities, MILES_PER_HOUR) * 60 * 60 + CalculatesRoute.time(routed_cities, MILES_PER_HOUR) + end + + def formatted_total_travel_time + t = total_travel_time * 60 * 60 mm, ss = t.divmod(60) hh, mm = mm.divmod(60) dd, hh = hh.divmod(24) "%d days, %d hours, %d minutes and %d seconds" % [dd, hh, mm, ss] end - end diff --git a/sales_person_bm.rb b/sales_person_bm.rb index 0defb64..e581698 100644 --- a/sales_person_bm.rb +++ b/sales_person_bm.rb @@ -14,8 +14,8 @@ def run_route(n) Benchmark.bm do |x| - x.report("Benchmarking 2 city route:"){ run_route(2)} - x.report("Benchmarking 10 city route:"){ run_route(10)} - x.report("Benchmarking 50 city route:"){ run_route(50)} - x.report("Benchmarking 200 city route:"){ run_route(100)} + x.report("Benchmarking 2 city route:"){ run_route(2) } + x.report("Benchmarking 10 city route:"){ run_route(10) } + x.report("Benchmarking 50 city route:"){ run_route(50) } + x.report("Benchmarking 200 city route:"){ run_route(100) } end diff --git a/salesperson.rb b/salesperson.rb index ebf5bcc..62e7528 100644 --- a/salesperson.rb +++ b/salesperson.rb @@ -16,4 +16,4 @@ puts city end puts "There are a total of #{phil.total_miles.round(2)} miles on the route." -puts "The total travel time is #{phil.total_travel_time}" \ No newline at end of file +puts "The total travel time is #{phil.formatted_total_travel_time}" \ No newline at end of file From c54332b3569ced6362ddb91932bfa42bdfbff3e8 Mon Sep 17 00:00:00 2001 From: drammopo Date: Tue, 11 Mar 2014 23:57:56 +0200 Subject: [PATCH 14/16] Refactored SalesPerson#schedule_city --- lib/sales_person.rb | 13 +++++++------ salesperson.rb | 4 ++-- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/sales_person.rb b/lib/sales_person.rb index 152bf8c..a86daec 100644 --- a/lib/sales_person.rb +++ b/lib/sales_person.rb @@ -1,19 +1,20 @@ class SalesPerson MILES_PER_HOUR = 55 - attr_reader :cities, :routed_cities + attr_reader :cities, :routed_cities, :unrouted_cities def initialize @cities = [] + @unrouted_cities = [] @routed_cities = [] end def schedule_city(city) + unrouted_cities << city unless unrouted_cities.include?(city) cities << city unless cities.include?(city) - cities.each_index do |index| - cities.insert(0, cities.delete_at(index)) if cities[index].starting_point == true - end - cities - end + starting_city = city if city.starting_point + cities.reject{|c| c.starting_point} + cities.unshift(starting_city) unless (cities.include?(starting_city) || starting_city.nil?) + end def route @routed_cities = CalculatesRoute.calculate(cities) diff --git a/salesperson.rb b/salesperson.rb index 62e7528..41c2ce3 100644 --- a/salesperson.rb +++ b/salesperson.rb @@ -8,7 +8,7 @@ phil.schedule_city(Place.build("Lubbock, TX")) puts "The original route was:" -phil.cities.each do |city| +phil.unrouted_cities.each do |city| puts city end puts "\nThe new route is:" @@ -16,4 +16,4 @@ puts city end puts "There are a total of #{phil.total_miles.round(2)} miles on the route." -puts "The total travel time is #{phil.formatted_total_travel_time}" \ No newline at end of file +puts "The total travel time is #{phil.formatted_total_travel_time}" From c52a92ed1bff03216e9f3cc1de86fa6cdfa9d2e3 Mon Sep 17 00:00:00 2001 From: drammopo Date: Wed, 12 Mar 2014 00:01:23 +0200 Subject: [PATCH 15/16] Fixed spacing. --- lib/sales_person.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/sales_person.rb b/lib/sales_person.rb index a86daec..0398321 100644 --- a/lib/sales_person.rb +++ b/lib/sales_person.rb @@ -14,7 +14,7 @@ def schedule_city(city) starting_city = city if city.starting_point cities.reject{|c| c.starting_point} cities.unshift(starting_city) unless (cities.include?(starting_city) || starting_city.nil?) - end + end def route @routed_cities = CalculatesRoute.calculate(cities) From fd25e10ba85ea4b3a4962dcc928b1cea66259f98 Mon Sep 17 00:00:00 2001 From: drammopo Date: Wed, 12 Mar 2014 07:16:04 -0700 Subject: [PATCH 16/16] Added code to send an item to two arrays --- lib/sales_person.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/sales_person.rb b/lib/sales_person.rb index 0398321..b5bb7f2 100644 --- a/lib/sales_person.rb +++ b/lib/sales_person.rb @@ -9,8 +9,7 @@ def initialize end def schedule_city(city) - unrouted_cities << city unless unrouted_cities.include?(city) - cities << city unless cities.include?(city) + [unrouted_cities, cities].each {|a| a << city} unless cities.include?(city) starting_city = city if city.starting_point cities.reject{|c| c.starting_point} cities.unshift(starting_city) unless (cities.include?(starting_city) || starting_city.nil?)