- 
                Notifications
    
You must be signed in to change notification settings  - Fork 15
 
Panda,tiger and eagle #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,6 +1,9 @@ | ||
| require_relative 'place' | ||
| 
     | 
||
| class SalesPerson | ||
| 
     | 
||
| attr_reader :cities | ||
| 
     | 
||
| def initialize | ||
| @cities = [] | ||
| end | ||
| 
        
          
        
         | 
    @@ -9,7 +12,27 @@ def schedule_city(city) | |
| @cities << city unless @cities.include?(city) | ||
| end | ||
| 
     | 
||
| def route | ||
| def route(starting_point_name = nil) | ||
| move_starting_point(starting_point_name) if starting_point_name | ||
| CalculatesRoute.calculate(cities) | ||
| end | ||
| 
     | 
||
| def calculate_total_miles(route) | ||
| route.map {|leg| leg.fetch(:distance)}.reduce(:+) | ||
| 
         There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. excellent use of reduce 🤘  | 
||
| end | ||
| 
     | 
||
| def calculate_traveling_time(total_miles, speed = 55) | ||
| total_miles/speed | ||
| 
         There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if you want to handle if speed is 0? It all depends on how you want this to blow up (or return infinite traveling time).  | 
||
| end | ||
| 
     | 
||
| private | ||
| 
     | 
||
| def find_starting_point(starting_point_name) | ||
| @cities.bsearch {|place| place.name.downcase == starting_point_name.downcase } | ||
| 
         There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. binary search, fancy! Why use that here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was me looking at docs trying to find a way to search an array, quickly. Is there a better way? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Try the exact same syntax for the block, but use "find" Pretty much the same thing, but less confusion for future readers. On Mon, Jan 27, 2014 at 12:58 PM, noahpatterson [email protected] 
  | 
||
| end | ||
| 
     | 
||
| def move_starting_point(starting_point_name) | ||
| starting_point = find_starting_point(starting_point_name) | ||
| @cities.unshift(@cities.delete(starting_point)) | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,10 +1,59 @@ | ||
| 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"] | ||
| 
     | 
||
| phil = SalesPerson.new | ||
| def personal_route(phil) | ||
| phil.schedule_city(Place.build("3900 16th st nw, DC", name: "Home")) | ||
| phil.schedule_city(Place.build("3505 Connecticut Ave, DC", name: "Petco")) | ||
| phil.schedule_city(Place.build("1250 24th st nw, DC", name: "Work")) | ||
| phil.schedule_city(Place.build("3100 14th St NW, DC", name: "Target")) | ||
| phil.schedule_city(Place.build("1440 P St NW, DC", name: "Whole Foods")) | ||
| phil.route("Work") | ||
| end | ||
| 
     | 
||
| # puts "This is the route from home: #{phil.route}" | ||
| 
     | 
||
| def load_cities(num_cities, sales_person) | ||
| times = 0 | ||
| until times == num_cities do | ||
| @all_cities.shuffle.take(1).each do |city| | ||
| sales_person.schedule_city(Place.build("#{city}, TX")) | ||
| end | ||
| times +=1 | ||
| sleep(2) | ||
| end | ||
| end | ||
| 
     | 
||
| def Benchmark(num_cities) | ||
| phil = SalesPerson.new | ||
| load_cities(num_cities,phil) | ||
| Benchmark.bm do |x| | ||
| x.report do | ||
| phil.route | ||
| end | ||
| end | ||
| end | ||
| 
     | 
||
| def pretty_time(seconds) | ||
| mm, ss = seconds.divmod(60) #=> [4515, 21] | ||
| hh, mm = mm.divmod(60) #=> [75, 15] | ||
| dd, hh = hh.divmod(24) #=> [3, 3] | ||
| "%d days, %d hours, %d minutes and %d seconds" % [dd, hh, mm, ss] | ||
| end | ||
| 
     | 
||
| route = personal_route(phil) | ||
| puts route | ||
| puts "This is my personal route: #{route.map {|leg| leg.fetch(:point).name}}" | ||
| miles = phil.calculate_total_miles(route) | ||
| puts "Phil traveled #{miles} miles" | ||
| time = phil.calculate_traveling_time(miles, 25)*60*60 | ||
| puts "It took phil #{pretty_time(time)}" | ||
| # puts "This is the benchmark for 2 cities:\n#{Benchmark(2)}" | ||
| # puts "This is the benchmark for 10 cities:\n#{Benchmark(10)}" | ||
| # puts "This is the benchmark for 50 cities:\n#{Benchmark(50)}" | ||
| # puts "This is the benchmark for 2 cities:\n#{Benchmark(200)}" | ||
| 
     | 
||
| phil = SalesPerson.new | ||
| 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")) | ||
| 
     | 
||
| puts phil.route | 
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,15 +1,22 @@ | ||
| require_relative "spec_helper" | ||
| require_relative "../lib/calculates_route" | ||
| require_relative "../lib/place" | ||
| 
     | 
||
| 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") } | ||
| let(:points ) { [dallas, el_paso, austin, lubbock] } | ||
| 
     | 
||
| it "should calculate the route" do | ||
| points = [dallas, el_paso, austin, lubbock] | ||
| expected = [dallas, austin, lubbock, el_paso] | ||
| CalculatesRoute.calculate(points).should eq(expected) | ||
| route = CalculatesRoute.calculate(points) | ||
| route.map {|leg| leg.fetch(:point)}.should eq(expected) | ||
| end | ||
| 
     | 
||
| it "should also output the distance of each leg" do | ||
| CalculatesRoute.calculate(points)[0].fetch(:distance).should eq(0) | ||
| end | ||
| 
     | 
||
| end | 
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,30 +1,70 @@ | ||
| require_relative "spec_helper" | ||
| require_relative "../lib/sales_person" | ||
| require_relative "../lib/calculates_route" | ||
| require_relative "../lib/place" | ||
| 
     | 
||
| describe SalesPerson do | ||
| 
     | 
||
| let(:city1) {Place.build("3505 Connecticut Ave, DC", name: "Petco")} | ||
| let(:city2) {Place.build("3900 16th st nw, DC", name: "Home")} | ||
| let(:city) {double} | ||
| let(:scheduled) { subject.schedule_city(city) } | ||
| 
     | 
||
| 
     | 
||
| it "should have many cities" do | ||
| city = stub | ||
| subject.schedule_city(city) | ||
| scheduled | ||
| 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) | ||
| scheduled | ||
| scheduled | ||
| }.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 | ||
| describe "#route" do | ||
| it "should calculate a route via the CalculatesRoute" do | ||
| cities = [Place, 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 = [double, double] | ||
| CalculatesRoute.stub(:calculate) { route_stub } | ||
| subject.route.should eq(route_stub) | ||
| end | ||
| 
     | 
||
| it "should be able to add a starting city by name" do | ||
| subject.schedule_city(city1) | ||
| subject.schedule_city(city2) | ||
| starting_point = "home" | ||
| first_route_point = subject.route(starting_point)[0] | ||
| expect(first_route_point.fetch(:point)).to eq(city2) | ||
| end | ||
| 
     | 
||
| it "should use first city if a starting city wasn't specified" do | ||
| subject.schedule_city(city1) | ||
| subject.schedule_city(city2) | ||
| first_route_point = subject.route[0] | ||
| expect(first_route_point.fetch(:point)).to eq(city1) | ||
| end | ||
| end | ||
| 
     | 
||
| describe "#calculate_total_miles" do | ||
| it "should report the total miles traveled on the route" do | ||
| route = [{point: city1, distance: 0}, {point: city2, distance: 1.1}] | ||
| expect(subject.calculate_total_miles(route)).to eq(1.1) | ||
| end | ||
| end | ||
| it "should returns the route from CalculatesRoute" do | ||
| route_stub = [stub, stub] | ||
| CalculatesRoute.stub(:calculate) { route_stub } | ||
| subject.route.should eq(route_stub) | ||
| 
     | 
||
| describe "#calculate_traveling_time" do | ||
| it "shoudld report the traveling time of the route" do | ||
| total_miles = 200 | ||
| expect(subject.calculate_traveling_time(total_miles)).to eq(200/55) | ||
| end | ||
| end | ||
| 
     | 
||
| end | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can set a default value for a hash.fetch -- so you could do the following. If there's a name, it'll use it. Otherwise, use an address.