Last active
April 10, 2016 04:44
-
-
Save pinkopaque22/7717b7da42d5ac5e0f1c5214bb4d8976 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//////Problem///// | |
Tube strike options calculator | |
The sweaty bus ride | |
There is a tube strike today so instead of getting the London Underground home you have decided to take the bus. It's a hot day and you have been sitting on the bus for over an hour, but the bus is hardly moving. Your arm is sticking to the window and sweat drips off your nose as you try to read your neighbour's book when you say to yourself, "This is ridiculous. I could have walked faster than this!" Suddenly you have an idea for an app that helps people decide whether to walk or take the bus home when the London Underground is on strike. | |
You rush home (relatively speaking) and begin to define the function that will underpin your app. | |
Function specification | |
You must create a function which takes three parameters; walking distance home, distance the bus must travel, and the combined distance of walking from the office to the bus stop and from the bus stop to your house. All distances are in kilometres. | |
So for example, if your home is 5km away by foot, and the bus that takes you home travels 6km, but you have to walk 500 metres to the bus stop to catch it and 500 metres to your house once the bus arrives (i.e. 1km in total), which is faster, walking or taking the bus? | |
Example - Which of these is faster?: | |
Start---Walk 5km--->End | |
Start---Walk 500m---Bus 6km---Walk 500m--->End | |
Walking speed and bus driving speed have been given to you as two pre-loaded variables ($global_variables in Ruby). | |
walk = 5 (km/hr) bus = 8 (km/hr) | |
The function must return the fastest option, either "Walk" or "Bus". If the walk is going to be over 2 hours, the function should recommend that you take the bus. If the walk is going to be under 10 minutes, the function should recommend that you walk. If both options are going to take the same amount of time, the function should recommend that you walk | |
/////Test Cases///// | |
Test.describe('calculator') | |
Test.it('works for some examples') | |
Test.assert_equals(calculator(5, 6, 1),"Bus","The bus should win this time!"); | |
Test.assert_equals(calculator(4, 5, 1),"Walk","Come on, you can walk this!"); | |
Test.assert_equals(calculator(5, 8, 0),"Walk","If the time is exactly the time, you should walk it!"); | |
Test.assert_equals(calculator(5, 4, 3),"Walk","There's no point taking the bus if it drops you in the middle of nowhere!"); | |
Test.assert_equals(calculator(11, 15, 2),"Bus","Don't be crazy! You'll destroy your lovely shoes!"); | |
Test.assert_equals(calculator(0.6, 0.4, 0),"Walk","Wow. Seriously? How lazy are you?"); | |
Test.assert_equals(calculator(10, 0.4, 0),"Bus","You wouldn't want to walk in this case!"); | |
////Code//// | |
def calculator(distance, bus_drive, bus_walk): | |
walk_time = float(distance/5.0*60) | |
bus_time = float(bus_drive/8.0*60) | |
bus_walk_time = float(bus_walk/5.0*60) | |
total_bus_walk_time = float(bus_time + bus_walk_time) | |
if walk_time > 120: | |
return "Bus" | |
if walk_time < 10: | |
return "Walk" | |
if walk_time == total_bus_walk_time: | |
return "Walk" | |
if (walk_time < total_bus_walk_time): | |
return "Walk" | |
if walk_time > total_bus_walk_time: | |
return "Bus" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I used the concept of Dijkstra's Algorithm to write pseudo code for my variables and to "catch" the exceptions in this codewar. This resource was where I started.
http://www.thedatagent.com/dijkstras-algorithm-python/