Created
December 4, 2012 17:32
-
-
Save itchy/4206610 to your computer and use it in GitHub Desktop.
Best 2012 (b)
This file contains hidden or 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
| valid_routes = add_segments(current_stops, starting_routes(start), stop) { |current, open_routes, matching_routes| current >= max_stops } | |
| def add_segments(current_stops, routes, stop, matching_routes = [], &block ) | |
| # This method expects a block that receives the parameters current_stops, open_routes and matching_routes | |
| # When the passed block evaluates to true | |
| # it returns all of the routes found matching the start and stop locations | |
| matching_routes << routes.select { |route| route.last_stop == stop } | |
| matching_routes.compress! | |
| open_routes = routes.select { |route| route.last_stop != stop } | |
| open_routes.compress! | |
| # Return all matches once we have satisfied our block | |
| if open_routes && !open_routes.empty? && matching_routes && !matching_routes.empty? | |
| return matching_routes if yield current_stops, open_routes, matching_routes | |
| end | |
| # recursive call | |
| add_segments(current_stops + 1, next_routes(routes), stop, matching_routes, &block) | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Again, I like this snippet of code because it represents a transition in my thinking as it is the first time I created real code that accepts a block as a parameter