Last active
August 29, 2015 14:01
-
-
Save hysios/bf4c6aa976127b42f0c2 to your computer and use it in GitHub Desktop.
path-distance 1
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
def distance_between v,w | |
dist = 0 | |
adj[v].each do |e| | |
dist = e.weight if e.to == w | |
end | |
return dist | |
end | |
#receive a path str like "A-B-C", return length of that path | |
def path_distance str | |
pair, dist_sum, no_path = [], 0, false | |
str.split('-').reduce do |from, to| | |
pair << [from,to] | |
to | |
end | |
for v,w in pair | |
dist = distance_between(v,w) | |
if dist == 0 | |
no_path = true | |
break | |
else | |
dist_sum += dist | |
end | |
end | |
no_path ? "NO SUCH ROUTE" : dist_sum | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment