Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kevin Bacon #151

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions 01-data-structures/07-graphs/films.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require_relative 'graphs'
require_relative 'node'

Kevin_Bacon = Actor.new("Kevin Bacon")
Daniel_Radcliffe = Actor.new("Daniel Radcliffe")
Emma_Watson = Actor.new("Emma Watson")
John_Cleese = Actor.new("John Cleese")
Brendan_Gleeson = Actor.new("Brendan Gleeson")

The_Big_Picture = Film.new("The Big Picture")

Kevin_Bacon.add_to_film(The_Big_Picture)
John_Cleese.add_to_film(The_Big_Picture)

Harry_Potter_and_the_Sorcerers_Stone = Film.new("Harry Potter and the Sorcerer's Stone")

Daniel_Radcliffe.add_to_film(Harry_Potter_and_the_Sorcerers_Stone)
Emma_Watson.add_to_film(Harry_Potter_and_the_Sorcerers_Stone)
John_Cleese.add_to_film(Harry_Potter_and_the_Sorcerers_Stone)

Harry_Potter_and_the_Goblet_of_Fire = Film.new("Harry Potter and the Goblet of Fire")

Daniel_Radcliffe.add_to_film(Harry_Potter_and_the_Goblet_of_Fire)
Emma_Watson.add_to_film(Harry_Potter_and_the_Goblet_of_Fire)
Brendan_Gleeson.add_to_film(Harry_Potter_and_the_Goblet_of_Fire)

Brendan_Gleeson.find_kevin_bacon

52 changes: 52 additions & 0 deletions 01-data-structures/07-graphs/graphs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
require_relative 'node'

class Actor
attr_accessor :name
attr_accessor :filmography
attr_accessor :checked

def initialize(name)
@name = name
@filmography = Array.new
@checked = false
end

def add_to_film(film)
film.cast.push(self)
@filmography.push(film)
end

def film_actor_hash(film)
if film.cast.include?(self)
film.cast.each do |i|
unless i == self
puts i.name
end
end
else
puts "That actor was not in that film. Try again."
end
end

def find_kevin_bacon(films=[], currentActor=self)
if currentActor == Kevin_Bacon
return 0
else
currentActor.filmography.each do |film|
unless films.include?(film.name)
films.push(film.name)
end
if film.cast.include?(Kevin_Bacon)
puts films
else
currentActor.checked = true
film.cast.each do |actor|
if actor.checked == false
actor.find_kevin_bacon(films, currentActor=actor)
end
end
end
end
end
end
end
9 changes: 9 additions & 0 deletions 01-data-structures/07-graphs/node.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Film
attr_accessor :name
attr_accessor :cast

def initialize(name)
@name = name
@cast = Array.new
end
end