Advertisement
Guest User

Untitled

a guest
Oct 18th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.96 KB | None | 0 0
  1. class Node
  2.  
  3.     attr_accessor :name, :film_actor_hash
  4.  
  5.     def initialize(name)
  6.         # Pass in film name and returns all actors in that film
  7.         @film_actor_hash = Hash.new
  8.         @name = name
  9.     end
  10. end
  11.  
  12. def bacon_links(node, counter)
  13.     counter =|| 0
  14.     node.film_actor_hash.each do |film, actors|
  15.         actors.each do |actor|
  16.             if actor.name == "Kevin Bacon"
  17.                 return film
  18.             else
  19.                 counter += 1
  20.                 return if counter > 5
  21.                 bacon_links(actor, counter)
  22.             end
  23.         end
  24.     end
  25. end
  26.  
  27. def find_kevin_bacon(start_node)
  28.     bacon_list = []
  29.     start_node.film_actor_hash.each do |film, actors|
  30.         actors.each do |actor|
  31.             if actor.name == "Kevin Bacon"
  32.                 bacon_list << film
  33.             else
  34.                 link = bacon_links(actor)
  35.                 bacon_list << link
  36.             end
  37.         end
  38.     end
  39.     bacon_list
  40. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement