Skip to content

Instantly share code, notes, and snippets.

@hysios
Last active August 29, 2015 14:01
Show Gist options
  • Save hysios/bf4c6aa976127b42f0c2 to your computer and use it in GitHub Desktop.
Save hysios/bf4c6aa976127b42f0c2 to your computer and use it in GitHub Desktop.
path-distance 1
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