From d953b08d81f61106f9c4ee739a2577cbe8d80f17 Mon Sep 17 00:00:00 2001 From: Patil Varvarian Date: Wed, 27 Nov 2013 17:13:26 -0800 Subject: [PATCH 1/6] Panda --- lib/calculates_route.rb | 17 ++++----- lib/map.rb | 9 ++--- lib/place.rb | 12 ++++--- lib/sales_person.rb | 15 +++++--- salesperson.rb | 16 +++++++-- spec/calculates_route_spec.rb | 22 ++++++------ spec/map_spec.rb | 25 ++++++------- spec/place_spec.rb | 54 +++++++++++++++------------- spec/sales_person_spec.rb | 67 +++++++++++++++++++++-------------- 9 files changed, 141 insertions(+), 96 deletions(-) diff --git a/lib/calculates_route.rb b/lib/calculates_route.rb index 4488393..f2475cf 100644 --- a/lib/calculates_route.rb +++ b/lib/calculates_route.rb @@ -1,22 +1,23 @@ class CalculatesRoute - def self.calculate(points) - + def self.calculate(points, starting_point) + remaining_points = points route = [] - route << remaining_points.slice!(0) - until remaining_points == [] do + route << remaining_points.slice!(remaining_points.index(starting_point)) + until remaining_points == [] do next_point = shortest_distance(route.last, remaining_points) + puts "from: #{route.last} ... to: #{next_point}" route << remaining_points.slice!(remaining_points.index(next_point)) + end - route + route end def self.shortest_distance(from, possible) distances = possible.map do |point| {point: point, distance: Map.distance_between(from, point)} end - distances.sort{|a,b| a.fetch(:distance) <=> b.fetch(:distance)}.first.fetch(:point) + distances.sort{|a,b| a.fetch(:distance) <=> b.fetch(:distance)}.first.fetch(:point) end -end - +end \ No newline at end of file diff --git a/lib/map.rb b/lib/map.rb index e9abfde..db1e1a6 100644 --- a/lib/map.rb +++ b/lib/map.rb @@ -3,9 +3,10 @@ class Map def self.search(terms) Array(Geocoder.search(terms)).first - end + end - def self.distance_between(first, second) - Geocoder::Calculations.distance_between(first, second) - end + def self.distance_between(first, second) + Geocoder::Calculations.distance_between(first, second) #wrapping geocoder again + end end + diff --git a/lib/place.rb b/lib/place.rb index 99eaadf..7c3c103 100644 --- a/lib/place.rb +++ b/lib/place.rb @@ -1,21 +1,23 @@ require_relative "./map" class Place - attr_accessor :name, :coordinates + attr_accessor :name, :coordinates def self.build(name) + results = Map.search(name) Place.new.tap do |p| p.name = name - p.coordinates = results.coordinates + p.coordinates = results.coordinates end end - def to_s + def to_s name end - + def to_coordinates coordinates end -end + +end \ No newline at end of file diff --git a/lib/sales_person.rb b/lib/sales_person.rb index d0c2890..e24c9e0 100644 --- a/lib/sales_person.rb +++ b/lib/sales_person.rb @@ -1,15 +1,22 @@ class SalesPerson attr_reader :cities + def initialize - @cities = [] + @cities = [] end def schedule_city(city) @cities << city unless @cities.include?(city) end - def route - CalculatesRoute.calculate(cities) + def find_point(start_name) + start = @cities.select {|city| city if city.name == start_name } + start.first + + end + + def route(starting_point) + CalculatesRoute.calculate(cities, find_point(starting_point)) end -end +end \ No newline at end of file diff --git a/salesperson.rb b/salesperson.rb index 9cafbd7..913ea0c 100644 --- a/salesperson.rb +++ b/salesperson.rb @@ -1,10 +1,22 @@ Dir["./lib/*.rb"].each {|file| require file } - phil = SalesPerson.new phil.schedule_city(Place.build("Dallas, TX")) phil.schedule_city(Place.build("El Paso, TX")) +phil.schedule_city(Place.build("Burbank, CA")) phil.schedule_city(Place.build("Austin, TX")) phil.schedule_city(Place.build("Lubbock, TX")) +phil.schedule_city(Place.build("Brooklyn, NY")) +phil.schedule_city(Place.build("Los Angeles, CA")) +phil.schedule_city(Place.build("San Diego, CA")) + +starting_point = "Austin, TX" +puts phil.route(starting_point) + + + + + + + -puts phil.route diff --git a/spec/calculates_route_spec.rb b/spec/calculates_route_spec.rb index 47679d6..c029210 100644 --- a/spec/calculates_route_spec.rb +++ b/spec/calculates_route_spec.rb @@ -1,15 +1,17 @@ -require_relative "../lib/calculates_route" -require_relative "../lib/place" +require_relative "../lib/calculates_route.rb" +require_relative "../lib/place.rb" describe CalculatesRoute do - let(:dallas) {Place.build("Dallas, TX") } - let(:austin ) { Place.build("Austin, TX")} - let(:lubbock ) { Place.build("Lubbock, TX")} - let(:el_paso ) { Place.build("El Paso, TX")} + let(:dallas) { Place.build("Dallas, TX")} + let(:austin) { Place.build("Austin, TX")} + let(:lubbock) { Place.build("Lubbock, TX")} + let(:el_paso) { Place.build("El Paso, TX")} it "should calculate the route" do - points = [dallas, el_paso, austin, lubbock] - expected = [dallas, austin, lubbock, el_paso] - CalculatesRoute.calculate(points).should eq(expected) + starting_point = austin + inputs = [austin, dallas, el_paso, lubbock] + expected = [austin, dallas, lubbock, el_paso] + CalculatesRoute.calculate(inputs, starting_point).should eq(expected) + end -end +end \ No newline at end of file diff --git a/spec/map_spec.rb b/spec/map_spec.rb index 2e2f6f1..0348a9b 100644 --- a/spec/map_spec.rb +++ b/spec/map_spec.rb @@ -1,28 +1,29 @@ -require_relative "../lib/map" -require 'geocoder' + require_relative "../lib/map" + require 'geocoder' -describe Map do - - describe ":search" do + describe Map do + + describe ":search" do it "should delegate search to geocoder" do Geocoder.should_receive(:search).with("austin, tx") - Map.search("austin, tx") + Map.search("austin, tx") #wraping geocoder end it "should use the first item in the array" do - austin = stub("Austin") + austin = stub("Austin") dallas = stub("Dallas") Geocoder.stub(:search) {[austin, dallas]} Map.search("austin, tx").should eq(austin) + end end - - describe ":distance" do + + describe ":distance" do it "should calculate distance between two sets of coordinates" do alpha = stub beta = stub Geocoder::Calculations.should_receive(:distance_between).with(alpha, beta) - Map.distance_between(alpha, beta) + Map.distance_between(alpha, beta) end - end -end + end + end \ No newline at end of file diff --git a/spec/place_spec.rb b/spec/place_spec.rb index 7d48250..1cb059f 100644 --- a/spec/place_spec.rb +++ b/spec/place_spec.rb @@ -1,43 +1,47 @@ require_relative "../lib/place" require_relative "../lib/map" + describe Place do - it "should have a name" do + it "should have a name" do subject.should respond_to(:name) + end - it "should have a coordinates" do - subject.coordinates = [29,-95] - subject.coordinates.should eq([29,-95]) + it "should have a coordinate" do + subject.coordinates = [29, -95] + subject.coordinates.should eq([29, -95]) end - describe ":build" do let(:name) { "El Paso, TX"} let(:result) { stub("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 - Map.stub(:search).with(name).and_return(result) - Place.build(name).should be_a(Place) - end + it "should build from the map" do + Map.should_receive(:search).with(name).and_return(result) + Place.build(name) end - - describe "#to_s" do - it "should use the city as the to_s" do - subject.stub(:name) { "Boston" } - subject.to_s.should eq("Boston") - end + + it "should be place" do + Map.stub(:search).with(name).and_return(result) + Place.build(name).should be_a(Place) + end + end + +describe "#to_s" do + it "should use the city as the to_s" do + subject.stub(:name) { "Boston" } + subject.to_s.should eq("Boston") end +end + +describe "#to_coordinates" do + it "should use the coordinates as the to_s" do + subject.stub(:coordinates) {[29, -95]} + subject.to_coordinates.should eq([29, -95]) - describe "#to_coordinates" do - it "should delegate to_coorinates to coordinates" do - subject.stub(:coordinates) { [5,5]} - subject.to_coordinates.should eq ([5,5]) - end end end + + +end diff --git a/spec/sales_person_spec.rb b/spec/sales_person_spec.rb index 08a6ce9..f5b2686 100644 --- a/spec/sales_person_spec.rb +++ b/spec/sales_person_spec.rb @@ -1,30 +1,45 @@ -require_relative "../lib/sales_person" -require_relative "../lib/calculates_route" +require_relative '../lib/sales_person' +require_relative '../lib/calculates_route' describe SalesPerson do - it "should have many cities" do - city = stub +it "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 + city = stub + expect{ subject.schedule_city(city) - subject.cities.should include(city) - end - - it "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) - end - - it "should calculate a route via the CalculatesRoute" do - cities = [stub, stub, stub] - 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 + subject.schedule_city(city) + }.to change(subject.cities,:count).by(1) + +end + +it "should find the starting_point" do + cities = [stub, stub, stub] + starting_point = [stub] + subject.find_point(starting_point).should eq(starting_point) + end + +it "should calculate route via the CalculatesRoute" do + start = stub("Austin, TX") + point = stub("Los Angeles, CA") + cities = [start, point] + subject.stub(:cities) { cities } + CalculatesRoute.should_receive(:calculate).with(cities, start) + #subject.route(start) +end + +it "should returns the route from CalculatesRoute" do + route_stub = [stub, stub] + start = [stub] + CalculatesRoute.stub(:calculate) { route_stub } + subject.route.should eq(route_stub) +end + +end + + \ No newline at end of file From 854b12215ec7f704ecc1324fdc1e9d77eb3c3b02 Mon Sep 17 00:00:00 2001 From: Patil Varvarian Date: Mon, 2 Dec 2013 12:03:51 -0800 Subject: [PATCH 2/6] Panda --- lib/calculates_route.rb | 5 ++--- lib/place.rb | 5 +++-- lib/sales_person.rb | 8 +++++--- salesperson.rb | 8 ++++---- spec/sales_person_spec.rb | 1 - 5 files changed, 14 insertions(+), 13 deletions(-) diff --git a/lib/calculates_route.rb b/lib/calculates_route.rb index f2475cf..61b4e50 100644 --- a/lib/calculates_route.rb +++ b/lib/calculates_route.rb @@ -8,10 +8,9 @@ def self.calculate(points, starting_point) until remaining_points == [] do next_point = shortest_distance(route.last, remaining_points) puts "from: #{route.last} ... to: #{next_point}" - route << remaining_points.slice!(remaining_points.index(next_point)) - + route << remaining_points.slice!(remaining_points.index(next_point)) end - route + # p route end def self.shortest_distance(from, possible) diff --git a/lib/place.rb b/lib/place.rb index 7c3c103..ea37a8b 100644 --- a/lib/place.rb +++ b/lib/place.rb @@ -1,6 +1,6 @@ require_relative "./map" -class Place +class Place attr_accessor :name, :coordinates def self.build(name) @@ -8,7 +8,8 @@ def self.build(name) results = Map.search(name) Place.new.tap do |p| p.name = name - p.coordinates = results.coordinates + #puts name + p.coordinates = results.coordinates end end diff --git a/lib/sales_person.rb b/lib/sales_person.rb index e24c9e0..0266747 100644 --- a/lib/sales_person.rb +++ b/lib/sales_person.rb @@ -1,6 +1,6 @@ class SalesPerson - attr_reader :cities + attr_reader :cities def initialize @cities = [] @@ -8,15 +8,17 @@ def initialize def schedule_city(city) @cities << city unless @cities.include?(city) + p @cities + end def find_point(start_name) - start = @cities.select {|city| city if city.name == start_name } - start.first + @cities.find{|city| city if city.name == start_name } end def route(starting_point) CalculatesRoute.calculate(cities, find_point(starting_point)) end + end \ No newline at end of file diff --git a/salesperson.rb b/salesperson.rb index 913ea0c..d02209c 100644 --- a/salesperson.rb +++ b/salesperson.rb @@ -6,12 +6,12 @@ phil.schedule_city(Place.build("Burbank, CA")) phil.schedule_city(Place.build("Austin, TX")) phil.schedule_city(Place.build("Lubbock, TX")) -phil.schedule_city(Place.build("Brooklyn, NY")) -phil.schedule_city(Place.build("Los Angeles, CA")) -phil.schedule_city(Place.build("San Diego, CA")) +#phil.schedule_city(Place.build("Brooklyn, NY")) +#phil.schedule_city(Place.build("Los Angeles, CA")) +#phil.schedule_city(Place.build("San Diego, CA")) starting_point = "Austin, TX" -puts phil.route(starting_point) +#puts phil.route(starting_point) diff --git a/spec/sales_person_spec.rb b/spec/sales_person_spec.rb index f5b2686..ff2dcf1 100644 --- a/spec/sales_person_spec.rb +++ b/spec/sales_person_spec.rb @@ -30,7 +30,6 @@ cities = [start, point] subject.stub(:cities) { cities } CalculatesRoute.should_receive(:calculate).with(cities, start) - #subject.route(start) end it "should returns the route from CalculatesRoute" do From c3f5030b1024864daffb06d0b43f4a52eff5128e Mon Sep 17 00:00:00 2001 From: Patil Varvarian Date: Tue, 3 Dec 2013 17:46:12 -0800 Subject: [PATCH 3/6] Eagle --- LICENSE | 7 --- lib/calculates_route.rb | 24 ++++++---- lib/map.rb | 10 ++--- lib/place.rb | 17 +++---- lib/sales_person.rb | 32 ++++++++----- salesperson.rb | 37 ++++++++++++--- spec/calculates_route_spec.rb | 18 ++++---- spec/map_spec.rb | 36 ++++++++------- spec/place_spec.rb | 46 +++++++++---------- spec/sales_person_spec.rb | 85 ++++++++++++++++++----------------- 10 files changed, 173 insertions(+), 139 deletions(-) delete mode 100644 LICENSE diff --git a/LICENSE b/LICENSE deleted file mode 100644 index f3e4a68..0000000 --- a/LICENSE +++ /dev/null @@ -1,7 +0,0 @@ -Copyright (c) 2012 Jesse Wolgamott - -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/lib/calculates_route.rb b/lib/calculates_route.rb index 61b4e50..6bd126a 100644 --- a/lib/calculates_route.rb +++ b/lib/calculates_route.rb @@ -1,22 +1,28 @@ +require_relative "./map" class CalculatesRoute - def self.calculate(points, starting_point) - + attr_reader :total_distance, :total_time, :display + + def self.calculate(points, start_point) remaining_points = points route = [] - route << remaining_points.slice!(remaining_points.index(starting_point)) + route << remaining_points.slice!(remaining_points.index(start_point)) until remaining_points == [] do + @total_distance = 0 next_point = shortest_distance(route.last, remaining_points) - puts "from: #{route.last} ... to: #{next_point}" - route << remaining_points.slice!(remaining_points.index(next_point)) + route << remaining_points.slice!(remaining_points.index(next_point.fetch(:point))) + @total_distance += next_point.fetch(:distance) end - # p route + route + #@total_time = (@total_distance/55) + display = {route: route, distance: @total_distance} end - def self.shortest_distance(from, possible) + def self.shortest_distance(form, possible) distances = possible.map do |point| - {point: point, distance: Map.distance_between(from, point)} + {point: point, distance: Map.distance_between(form, point)} end - distances.sort{|a,b| a.fetch(:distance) <=> b.fetch(:distance)}.first.fetch(:point) + point = distances.sort{|a,b| a.fetch(:distance) <=> b.fetch(:distance)}.first end + end \ No newline at end of file diff --git a/lib/map.rb b/lib/map.rb index db1e1a6..e02317f 100644 --- a/lib/map.rb +++ b/lib/map.rb @@ -3,10 +3,10 @@ class Map def self.search(terms) Array(Geocoder.search(terms)).first - end + end - def self.distance_between(first, second) - Geocoder::Calculations.distance_between(first, second) #wrapping geocoder again - end -end + def self.distance_between(first, second) + Geocoder::Calculations.distance_between(first, second) + end +end \ No newline at end of file diff --git a/lib/place.rb b/lib/place.rb index ea37a8b..59e0df9 100644 --- a/lib/place.rb +++ b/lib/place.rb @@ -1,24 +1,21 @@ require_relative "./map" +class Place -class Place - attr_accessor :name, :coordinates - +attr_accessor :name, :coordinates def self.build(name) - results = Map.search(name) Place.new.tap do |p| p.name = name - #puts name - p.coordinates = results.coordinates + p.coordinates = results.coordinates end end - def to_s + def to_s name end - + def to_coordinates coordinates end - -end \ No newline at end of file + +end \ No newline at end of file diff --git a/lib/sales_person.rb b/lib/sales_person.rb index 0266747..50f99c4 100644 --- a/lib/sales_person.rb +++ b/lib/sales_person.rb @@ -1,24 +1,36 @@ +require_relative "./calculates_route" + class SalesPerson - attr_reader :cities + attr_reader :cities def initialize - @cities = [] + @cities = [] end def schedule_city(city) @cities << city unless @cities.include?(city) - p @cities - + end + + def find_city(start_name) + @cities.find{|city| city if city.name == start_name} end - def find_point(start_name) - @cities.find{|city| city if city.name == start_name } - + def route(start) + display = CalculatesRoute.calculate(cities, find_city(start)) + results = {route: display.fetch(:route), time: traveling_time(display.fetch(:distance))} + z = [] + x = results.fetch(:route) + y = results.fetch(:time) + z = [x, y] + return z + + + end - def route(starting_point) - CalculatesRoute.calculate(cities, find_point(starting_point)) + def traveling_time(distance) + time = distance/55 end -end \ No newline at end of file +end diff --git a/salesperson.rb b/salesperson.rb index d02209c..2cd80e3 100644 --- a/salesperson.rb +++ b/salesperson.rb @@ -1,22 +1,47 @@ Dir["./lib/*.rb"].each {|file| require file } +require "benchmark" +require 'nokogiri' +require 'open-uri' + phil = SalesPerson.new phil.schedule_city(Place.build("Dallas, TX")) phil.schedule_city(Place.build("El Paso, TX")) -phil.schedule_city(Place.build("Burbank, CA")) phil.schedule_city(Place.build("Austin, TX")) phil.schedule_city(Place.build("Lubbock, TX")) -#phil.schedule_city(Place.build("Brooklyn, NY")) -#phil.schedule_city(Place.build("Los Angeles, CA")) -#phil.schedule_city(Place.build("San Diego, CA")) - +phil.schedule_city(Place.build("Burbank, CA")) +phil.schedule_city(Place.build("Brooklyn, NY")) +phil.schedule_city(Place.build("Los Angeles, CA")) +phil.schedule_city(Place.build("San Diego, CA")) starting_point = "Austin, TX" -#puts phil.route(starting_point) +puts phil.route(starting_point) +texas_cities = [] +texas = Nokogiri::HTML(open('http://www.texas.gov/en/discover/Pages/topic.aspx?topicid=/government/localgov')) +texas.css(" .topic-subcategory-link a").map do |node| + texas_cities << node.content +end +puts "Count for cities in Texas #{texas_cities.count}" +puts texas_cities +#bench_vals = [2, 10, 50, 200] +#bench_vals.each do |val| + #current = SalesPerson.new + #texas_cities.shuffle.take(val).each do |city| + # current.schedule_city(Place.build(city)) + # end +#the_route = [] +#puts "\nBenchmarking results for #{val} cities:" + #Benchmark.bm do |x| + # x.report do + # the_route = current.route("Austin, TX") + #end + # puts "The route:" + # puts the_route + #end diff --git a/spec/calculates_route_spec.rb b/spec/calculates_route_spec.rb index c029210..807a634 100644 --- a/spec/calculates_route_spec.rb +++ b/spec/calculates_route_spec.rb @@ -1,17 +1,17 @@ -require_relative "../lib/calculates_route.rb" +require_relative "../lib/calculates_route" require_relative "../lib/place.rb" -describe CalculatesRoute do - let(:dallas) { Place.build("Dallas, TX")} - let(:austin) { Place.build("Austin, TX")} - let(:lubbock) { Place.build("Lubbock, TX")} - let(:el_paso) { Place.build("El Paso, TX")} + describe CalculatesRoute do + let(:austin) {Place.build("Austin, TX")} + let(:dallas) {Place.build("Dallas, TX")} + let(:el_paso) {Place.build("El Paso, TX")} + let(:lubbock) {Place.build("Lubbock, TX")} - it "should calculate the route" do - starting_point = austin + it "should be able to calculate the route" do + start = austin inputs = [austin, dallas, el_paso, lubbock] expected = [austin, dallas, lubbock, el_paso] - CalculatesRoute.calculate(inputs, starting_point).should eq(expected) + CalculatesRoute.calculate(inputs, start).should eq(expected) end end \ No newline at end of file diff --git a/spec/map_spec.rb b/spec/map_spec.rb index 0348a9b..b86d40f 100644 --- a/spec/map_spec.rb +++ b/spec/map_spec.rb @@ -1,29 +1,31 @@ - require_relative "../lib/map" - require 'geocoder' +require_relative "../lib/place" +require_relative "../lib/map" +require 'geocoder' - describe Map do - - describe ":search" do - it "should delegate search to geocoder" do + +describe Map do + describe ":search" do + it "should be able to search in geocoder" do Geocoder.should_receive(:search).with("austin, tx") - Map.search("austin, tx") #wraping geocoder + Map.search("austin, tx") end it "should use the first item in the array" do - austin = stub("Austin") + austin = stub("Austin") dallas = stub("Dallas") Geocoder.stub(:search) {[austin, dallas]} Map.search("austin, tx").should eq(austin) - end end - - describe ":distance" do - it "should calculate distance between two sets of coordinates" do - alpha = stub - beta = stub + + describe ":distance" do + it "should measure the accurate distance between points" do + alpha = stub + beta = stub Geocoder::Calculations.should_receive(:distance_between).with(alpha, beta) - Map.distance_between(alpha, beta) + Map.distance_between(alpha, beta) end - end - end \ No newline at end of file + end + + +end \ No newline at end of file diff --git a/spec/place_spec.rb b/spec/place_spec.rb index 1cb059f..a0c8251 100644 --- a/spec/place_spec.rb +++ b/spec/place_spec.rb @@ -1,47 +1,45 @@ -require_relative "../lib/place" -require_relative "../lib/map" +require_relative '../lib/place' +require_relative '../lib/map' - -describe Place do - - it "should have a name" do +describe Place do + + it "should have a name" do subject.should respond_to(:name) - end + it "should have a coordinate" do - subject.coordinates = [29, -95] - subject.coordinates.should eq([29, -95]) + subject.coordinates = [-29, -95] + subject.coordinates.should eq([-29, -95]) end describe ":build" do - let(:name) { "El Paso, TX"} + let(:name) {"El Paso, TX"} let(:result) { stub("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 Map.stub(:search).with(name).and_return(result) Place.build(name).should be_a(Place) end - end - -describe "#to_s" do - it "should use the city as the to_s" do - subject.stub(:name) { "Boston" } - subject.to_s.should eq("Boston") - end end -describe "#to_coordinates" do - it "should use the coordinates as the to_s" do - subject.stub(:coordinates) {[29, -95]} - subject.to_coordinates.should eq([29, -95]) + describe "#to_s" do + it "should use the city as the to_s" do + subject.stub(:name) {"Boston"} + subject.to_s.should eq("Boston") + end + end + describe "#to_coordinates" do + it "should use the coordinates as the to_s" do + subject.stub(:coordinates) {[29, -95]} + subject.to_coordinates.should eq([29, -95]) + end end -end -end +end \ No newline at end of file diff --git a/spec/sales_person_spec.rb b/spec/sales_person_spec.rb index ff2dcf1..c4723a6 100644 --- a/spec/sales_person_spec.rb +++ b/spec/sales_person_spec.rb @@ -1,44 +1,45 @@ require_relative '../lib/sales_person' require_relative '../lib/calculates_route' - -describe SalesPerson do -it "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 - city = stub - expect{ - subject.schedule_city(city) - subject.schedule_city(city) - }.to change(subject.cities,:count).by(1) - -end - -it "should find the starting_point" do - cities = [stub, stub, stub] - starting_point = [stub] - subject.find_point(starting_point).should eq(starting_point) - -end - -it "should calculate route via the CalculatesRoute" do - start = stub("Austin, TX") - point = stub("Los Angeles, CA") - cities = [start, point] - subject.stub(:cities) { cities } - CalculatesRoute.should_receive(:calculate).with(cities, start) -end - -it "should returns the route from CalculatesRoute" do - route_stub = [stub, stub] - start = [stub] - CalculatesRoute.stub(:calculate) { route_stub } - subject.route.should eq(route_stub) -end - -end - - \ No newline at end of file +require 'rspec' + + describe SalesPerson do + it "should be able to schedule many cities" do + city = stub + subject.schedule_city(city) + subject.cities.should include(city) + end + + it "should not include the same city" do + city = stub + city = stub + subject.schedule_city(city) + subject.schedule_city(city) + subject.cities.count.should eq(1) + end + + it "should find a starting point" do + cities = [stub, stub, stub] + start = stub + subject.find_city(start).should eq(start) + end +# route needs to use CalcuatesRoute.calculate method to return the correct +# arrangement of cities + it "should be able to calcuate the route via CalculatesRoute" do + start = stub("Austin, TX") + point = stub("Los Angeles, CA") + cities = [start, point] + subject.stub(:cities) {cities} + CalculatesRoute.should_receive(:calculate).with(cities, start) + #subject.route + end + + it "should return the route from CalcuatesRoute" do + start = [stub] + route_stub = [stub, stub] + cities = [stub, stub, stub] + CalculatesRoute.stub(:calculate) {[start, route_stub]} + subject.route.should eq([stub, stub, stub]) + + + end + end From 7108892e0275c3b1bc38ca1e5fb4e995323ddd55 Mon Sep 17 00:00:00 2001 From: Patil Varvarian Date: Tue, 3 Dec 2013 18:30:33 -0800 Subject: [PATCH 4/6] eagle --- lib/calculates_route.rb | 12 +++++------- lib/sales_person.rb | 3 --- salesperson.rb | 37 ++++++++++++++++++++----------------- 3 files changed, 25 insertions(+), 27 deletions(-) diff --git a/lib/calculates_route.rb b/lib/calculates_route.rb index 6bd126a..4a31aa9 100644 --- a/lib/calculates_route.rb +++ b/lib/calculates_route.rb @@ -1,20 +1,18 @@ require_relative "./map" class CalculatesRoute - attr_reader :total_distance, :total_time, :display + attr_reader :total_distance, :total_time def self.calculate(points, start_point) + @total_distance = 0 remaining_points = points route = [] route << remaining_points.slice!(remaining_points.index(start_point)) - until remaining_points == [] do - @total_distance = 0 + until remaining_points == [] do next_point = shortest_distance(route.last, remaining_points) - route << remaining_points.slice!(remaining_points.index(next_point.fetch(:point))) @total_distance += next_point.fetch(:distance) - end - route - #@total_time = (@total_distance/55) + route << remaining_points.slice!(remaining_points.index(next_point.fetch(:point))) + end display = {route: route, distance: @total_distance} end diff --git a/lib/sales_person.rb b/lib/sales_person.rb index 50f99c4..f6c6bc6 100644 --- a/lib/sales_person.rb +++ b/lib/sales_person.rb @@ -24,9 +24,6 @@ def route(start) y = results.fetch(:time) z = [x, y] return z - - - end def traveling_time(distance) diff --git a/salesperson.rb b/salesperson.rb index 2cd80e3..bed7f88 100644 --- a/salesperson.rb +++ b/salesperson.rb @@ -5,12 +5,14 @@ require 'open-uri' phil = SalesPerson.new +phil.schedule_city(Place.build("Burbank, CA")) phil.schedule_city(Place.build("Dallas, TX")) phil.schedule_city(Place.build("El Paso, TX")) phil.schedule_city(Place.build("Austin, TX")) phil.schedule_city(Place.build("Lubbock, TX")) -phil.schedule_city(Place.build("Burbank, CA")) phil.schedule_city(Place.build("Brooklyn, NY")) +phil.schedule_city(Place.build("Griffith, Park")) +phil.schedule_city(Place.build("Berkeley, San Francisco")) phil.schedule_city(Place.build("Los Angeles, CA")) phil.schedule_city(Place.build("San Diego, CA")) starting_point = "Austin, TX" @@ -24,24 +26,25 @@ end puts "Count for cities in Texas #{texas_cities.count}" -puts texas_cities +#puts texas_cities -#bench_vals = [2, 10, 50, 200] -#bench_vals.each do |val| - #current = SalesPerson.new - #texas_cities.shuffle.take(val).each do |city| - # current.schedule_city(Place.build(city)) - # end +bench_vals = [2] +bench_vals.each do |val| + current = SalesPerson.new + texas_cities.shuffle.take(val).each do |city| + current.schedule_city(Place.build(city)) -#the_route = [] -#puts "\nBenchmarking results for #{val} cities:" - #Benchmark.bm do |x| - # x.report do - # the_route = current.route("Austin, TX") - #end - # puts "The route:" - # puts the_route +the_route = [] +puts "\nBenchmarking results for #{val} cities:" + Benchmark.bm do |x| + x.report do + the_route = current.route("Austin, TX") + end +end + puts "The route:" + puts the_route - #end + end +end From c61c6f56f45dda3b8239c53707d4ca1ac2f30566 Mon Sep 17 00:00:00 2001 From: Patil Varvarian Date: Wed, 4 Dec 2013 10:55:27 -0800 Subject: [PATCH 5/6] panda, tiger, eagle --- lib/calculates_route.rb | 10 ++++++++-- lib/sales_person.rb | 6 ++++-- salesperson.rb | 34 +++++++++++++++++----------------- 3 files changed, 29 insertions(+), 21 deletions(-) diff --git a/lib/calculates_route.rb b/lib/calculates_route.rb index 4a31aa9..375a074 100644 --- a/lib/calculates_route.rb +++ b/lib/calculates_route.rb @@ -7,11 +7,17 @@ def self.calculate(points, start_point) @total_distance = 0 remaining_points = points route = [] - route << remaining_points.slice!(remaining_points.index(start_point)) + conf = remaining_points.index(start_point) + + if conf.nil? + route << remaining_points.slice!(0) + else + route << remaining_points.slice!(conf) + end until remaining_points == [] do next_point = shortest_distance(route.last, remaining_points) - @total_distance += next_point.fetch(:distance) route << remaining_points.slice!(remaining_points.index(next_point.fetch(:point))) + @total_distance += next_point.fetch(:distance) end display = {route: route, distance: @total_distance} end diff --git a/lib/sales_person.rb b/lib/sales_person.rb index f6c6bc6..d84a503 100644 --- a/lib/sales_person.rb +++ b/lib/sales_person.rb @@ -13,11 +13,13 @@ def schedule_city(city) end def find_city(start_name) - @cities.find{|city| city if city.name == start_name} + found = cities.select{|city| city if city.name == start_name} + found.first end def route(start) - display = CalculatesRoute.calculate(cities, find_city(start)) + city = find_city(start) + display = CalculatesRoute.calculate(cities, city) results = {route: display.fetch(:route), time: traveling_time(display.fetch(:distance))} z = [] x = results.fetch(:route) diff --git a/salesperson.rb b/salesperson.rb index bed7f88..905b115 100644 --- a/salesperson.rb +++ b/salesperson.rb @@ -16,35 +16,35 @@ phil.schedule_city(Place.build("Los Angeles, CA")) phil.schedule_city(Place.build("San Diego, CA")) starting_point = "Austin, TX" +puts phil.cities +puts '------------------------' puts phil.route(starting_point) + texas_cities = [] texas = Nokogiri::HTML(open('http://www.texas.gov/en/discover/Pages/topic.aspx?topicid=/government/localgov')) texas.css(" .topic-subcategory-link a").map do |node| texas_cities << node.content + #puts node.content end -puts "Count for cities in Texas #{texas_cities.count}" -#puts texas_cities - - -bench_vals = [2] -bench_vals.each do |val| - current = SalesPerson.new - texas_cities.shuffle.take(val).each do |city| - current.schedule_city(Place.build(city)) - -the_route = [] -puts "\nBenchmarking results for #{val} cities:" +bench_var = [2, 3] + bench_var.each do |var| + content = SalesPerson.new + texas_cities.shuffle.take(var).each do |city| + content.schedule_city(Place.build(city)) + end + Benchmark.bm do |x| x.report do - the_route = current.route("Austin, TX") + starting_point = "Austin, Texas" + y = content.route(starting_point) + puts y + end end end - puts "The route:" - puts the_route - end -end + + From d84ba13d0d090aa5ea8aeabb01b4660c823d9e23 Mon Sep 17 00:00:00 2001 From: Patil Varvarian Date: Wed, 4 Dec 2013 13:24:57 -0800 Subject: [PATCH 6/6] Panda, Tiger, Eagle --- lib/calculates_route.rb | 8 +++++--- lib/sales_person.rb | 7 ++----- salesperson.rb | 6 +++--- spec/calculates_route_spec.rb | 18 ++++++++++++------ spec/sales_person_spec.rb | 24 ++++++++++++------------ 5 files changed, 34 insertions(+), 29 deletions(-) diff --git a/lib/calculates_route.rb b/lib/calculates_route.rb index 375a074..5f16ca1 100644 --- a/lib/calculates_route.rb +++ b/lib/calculates_route.rb @@ -5,7 +5,9 @@ class CalculatesRoute def self.calculate(points, start_point) @total_distance = 0 + remaining_points = points + route = [] conf = remaining_points.index(start_point) @@ -16,15 +18,15 @@ def self.calculate(points, start_point) end until remaining_points == [] do next_point = shortest_distance(route.last, remaining_points) - route << remaining_points.slice!(remaining_points.index(next_point.fetch(:point))) + route << remaining_points.slice!(remaining_points.index(next_point.fetch(:point))) @total_distance += next_point.fetch(:distance) end display = {route: route, distance: @total_distance} end - def self.shortest_distance(form, possible) + def self.shortest_distance(from, possible) distances = possible.map do |point| - {point: point, distance: Map.distance_between(form, point)} + {point: point, distance: Map.distance_between(from, point)} end point = distances.sort{|a,b| a.fetch(:distance) <=> b.fetch(:distance)}.first end diff --git a/lib/sales_person.rb b/lib/sales_person.rb index d84a503..7e8ffb1 100644 --- a/lib/sales_person.rb +++ b/lib/sales_person.rb @@ -21,11 +21,8 @@ def route(start) city = find_city(start) display = CalculatesRoute.calculate(cities, city) results = {route: display.fetch(:route), time: traveling_time(display.fetch(:distance))} - z = [] - x = results.fetch(:route) - y = results.fetch(:time) - z = [x, y] - return z + return [results.fetch(:route), results.fetch(:time)] + end def traveling_time(distance) diff --git a/salesperson.rb b/salesperson.rb index 905b115..d8f3f4c 100644 --- a/salesperson.rb +++ b/salesperson.rb @@ -21,11 +21,11 @@ puts phil.route(starting_point) - texas_cities = [] texas = Nokogiri::HTML(open('http://www.texas.gov/en/discover/Pages/topic.aspx?topicid=/government/localgov')) texas.css(" .topic-subcategory-link a").map do |node| - texas_cities << node.content + texas_cities << node.content + #puts node.content end @@ -36,7 +36,7 @@ texas_cities.shuffle.take(var).each do |city| content.schedule_city(Place.build(city)) end - + Benchmark.bm do |x| x.report do starting_point = "Austin, Texas" diff --git a/spec/calculates_route_spec.rb b/spec/calculates_route_spec.rb index 807a634..f17240e 100644 --- a/spec/calculates_route_spec.rb +++ b/spec/calculates_route_spec.rb @@ -7,11 +7,17 @@ let(:el_paso) {Place.build("El Paso, TX")} let(:lubbock) {Place.build("Lubbock, TX")} - it "should be able to calculate the route" do - start = austin - inputs = [austin, dallas, el_paso, lubbock] - expected = [austin, dallas, lubbock, el_paso] - CalculatesRoute.calculate(inputs, start).should eq(expected) + it "should be able to show the correct route" do + start = austin + inputs = [austin, dallas, el_paso, lubbock] + expected = [austin, dallas, lubbock, el_paso] + CalculatesRoute.calculate(inputs, start).fetch(:route).should eq(expected) + end - end + it "should be able to calculate the total distance" do + start = austin + inputs = [austin, dallas, el_paso, lubbock] + CalculatesRoute.calculate(inputs, start).fetch(:distance).should_not be(0) + + end end \ No newline at end of file diff --git a/spec/sales_person_spec.rb b/spec/sales_person_spec.rb index c4723a6..6a00326 100644 --- a/spec/sales_person_spec.rb +++ b/spec/sales_person_spec.rb @@ -18,27 +18,27 @@ end it "should find a starting point" do - cities = [stub, stub, stub] - start = stub - subject.find_city(start).should eq(start) + city = stub + city.stub(name: "Austin, TX") + subject.schedule_city(city) + subject.find_city("Austin, TX").should eq(city) end -# route needs to use CalcuatesRoute.calculate method to return the correct -# arrangement of cities it "should be able to calcuate the route via CalculatesRoute" do - start = stub("Austin, TX") - point = stub("Los Angeles, CA") + start = stub(name: "Austin, TX") + point = stub(name: "Los Angeles, CA") cities = [start, point] subject.stub(:cities) {cities} CalculatesRoute.should_receive(:calculate).with(cities, start) - #subject.route + subject.route("Austin, TX") end it "should return the route from CalcuatesRoute" do - start = [stub] - route_stub = [stub, stub] + route = [stub("Austin, Tx"), stub("Los Angeles, CA")] + start = stub("Austin, TX") cities = [stub, stub, stub] - CalculatesRoute.stub(:calculate) {[start, route_stub]} - subject.route.should eq([stub, stub, stub]) + CalculatesRoute.stub(:calculate) {route} + CalculatesRoute.stub(:route).and_return(route) + subject.route.(start).should eq(route) end