Created
December 24, 2013 04:42
-
-
Save marcmartino/8108863 to your computer and use it in GitHub Desktop.
Given a large hash table whose keys are movie names and whose values are a list of actors in those movies, write a function to determine the Bacon number of a particular actor.
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
var assert = require("assert"), | |
_ = require("underscore"), | |
testData = { | |
"Iron Man": ["Robert Downey Jr", "Gwyneth Paltrow", "Terrence Howard"], | |
"Iron Man 3": ["Robert Downey Jr", "Guy Pearce", "Gwyneth Paltrow"], | |
"Super Troopers": ["Jay Chandrasekhar", "Kevin Heffernan"], | |
"The Day After Tomorrow": ["Dennis Quaid", "Jake Gyllenhaal", "Emmy Rossum"], | |
"The Phantom of the Opera": ["Gerard Butler", "Emmy Rossum", "Patrick Wilson"], | |
"How I Met Your Mother": ['Josh Radnor', "Jason Segel", "Cobie Smulders", "Kevin Heffernan"], | |
"Despicable Me": ['Steve Carell', 'Jason Segel', 'Russel Brand', "Michael Fassbender"], | |
"The Nightmare Before Christmas": ["Danny Elfman", "Chris Sarandon", "Catherine O'Hara"], | |
"Homeland": ["Claire Danes", "Damian Lewis", "Morena Baccarin", "Mandy Patinkin"], | |
"Memento": ["Guy Pearce", "Carrie-Anne Moss", "Joe Pantoliano"], | |
"The Matrix": ["Keanu Reeves", "Laurence Fishburne", "Carrie-Anne Moss"], | |
"The Fugitive": ["Harrison Ford", "Tommy Lee Jones", "Sela Ward"], | |
"The Lord of the Rings: The Fellowship of the Ring": ["Elijah Wood", "Ian McKellen", "Orlando Bloom"], | |
"The Curious Case of Benjamin Button": ["Brad Pitt", "Cate Blanchett", "Tilda Swinton"], | |
"Se7en": ["Morgan Freeman", "Brad Pitt", "Gwyneth Paltrow"], | |
"X-Men: First Class": ["James McAvoy", "Michael Fassbender", "Jennifer Lawrence", "Kevin Bacon"], | |
"The Hunger Games": ["Jennifer Lawrence", "Josh Hutcherson", "Liam Hemsworth"], | |
"The Expendables": ["Sylvester Stalone", "Arnold Swartzenhigger"], | |
"The Expendables 2": ["Sylvester Stalone", "Liam Hemsworth", "Randy Couture"], | |
"Paranoia": ["Liam Hemsworth", "Gary Oldman", "Harrison Ford"], | |
// "Test Movie": ["Sela Ward", "Gerard Butler", "Terrence Howard"], | |
// "Test Two": ["Morgan Freeman", "Ian McKellen"] | |
} | |
function generateTree(data, rootName) { | |
var tree = [rootName, []]; | |
return generateSubTree([tree], [], data, tree); | |
} | |
function generateSubTree(currentQueue, backupQueue, data, treeRoot) { | |
var childrenArr = [], thisNode; | |
if (currentQueue.length > 0) { | |
thisNode = currentQueue.pop(); | |
childrenArr = _.reject(levelOneActors(data, thisNode[0]),//take out the names that are elsewhere (higher) in the tree | |
function (name) { | |
return findDepthInTree(treeRoot, name) > -1; | |
}); | |
for (var m = 0; m < childrenArr.length; m++) { | |
var childNode = [childrenArr[m], []]; | |
thisNode[1].push(childNode); | |
backupQueue.push(childNode); | |
} | |
return generateSubTree(currentQueue, backupQueue, data, treeRoot); | |
} else if (backupQueue.length > 0) { | |
return generateSubTree(backupQueue, [], data, treeRoot); | |
} else { | |
return treeRoot; | |
} | |
} | |
function findDepthInTree(tree, name, depth) { | |
//returns numerical depth in tree. root is 0 | |
var depth = depth || 0; | |
if (tree[0] === name) { | |
return depth; | |
} else { | |
for(var m = 0; m < tree[1].length; m++) { | |
var branchFind = findDepthInTree(tree[1][m], name, depth + 1); | |
if (branchFind != -1) { | |
return branchFind; | |
} | |
} | |
return -1; | |
} | |
} | |
function levelOneActors(data, actorName) { | |
return _.reject( | |
_.reduce(data, function (accum, actorsArr, movieName) { | |
// console.log([actorName, actorsArr.indexOf(actorName), actorsArr].join(" ")); | |
if (actorsArr.indexOf(actorName) > -1) { | |
return accum.concat(actorsArr); | |
} | |
return accum; | |
}, []), rejectActor(actorName)) || []; | |
} | |
function rejectActor(rejectName) { | |
return function (name) { | |
return name === rejectName; | |
}; | |
} | |
var kevinTree = generateTree(testData, "Kevin Bacon"); | |
console.log(JSON.stringify(kevinTree)); | |
var person = "Elijah Wood"; | |
console.log("Bacon Number of " + findDepthInTree(kevinTree, person) + " for " + person); | |
var testTree = ["Kevin Bacon", [ | |
["Marc Martino", [ | |
["Scott Miller", []], | |
["Christian P", []] ]], | |
["Kevin Stratton", []] ]]; | |
assert.equal(findDepthInTree(testTree, "Kevin Bacon"), 0, "root found"); | |
assert.equal(findDepthInTree(testTree, "Marc Martino"), 1, "one level down found"); | |
assert.equal(findDepthInTree(testTree, "Christian P"), 2, "two level down on the right found"); | |
assert.equal(findDepthInTree(testTree, "Broccoli Rob"), -1, "no elment found"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment