Skip to content

Instantly share code, notes, and snippets.

@RickGriff
Last active January 7, 2018 22:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RickGriff/ff779f1455249d067827f83fce64bc59 to your computer and use it in GitHub Desktop.
Save RickGriff/ff779f1455249d067827f83fce64bc59 to your computer and use it in GitHub Desktop.
Codewars Challenge: Tube Strike Options Calculator
# 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
-----
#My Solution:
def calculator(distance, bus_drive, bus_walk)
#convert speeds to km per minute
walk = $walk.to_f/60
bus = $bus.to_f/60
#calculate times for each mode of transport using time = distance/speed
time_bus = bus_walk/walk + bus_drive/bus
time_walk = distance/walk
if time_walk < 10 && time_bus < 10
return "Walk"
elsif time_walk > 120 && time_bus > 120
return "Bus"
end
time_walk <= time_bus ? "Walk" : "Bus"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment