Fixed formatting problems, added JSDoc, added example for JS, fixed example for JS
This commit is contained in:
@@ -1,18 +1,27 @@
|
||||
const personIsSeller = name => name[name.length - 1] === 'm';
|
||||
|
||||
const graph = {};
|
||||
graph.you = ['alice', 'bob', 'claire'];
|
||||
graph.bob = ['anuj', 'peggy'];
|
||||
graph.alice = ['peggy'];
|
||||
graph.claire = ['thom', 'jonny'];
|
||||
graph.you = ["alice", "bob", "claire"];
|
||||
graph.bob = ["anuj", "peggy"];
|
||||
graph.alice = ["peggy"];
|
||||
graph.claire = ["thom", "jonny"];
|
||||
graph.anuj = [];
|
||||
graph.peggy = [];
|
||||
graph.thom = [];
|
||||
graph.jonny = [];
|
||||
|
||||
const search = (name) => {
|
||||
let searchQueue = [];
|
||||
searchQueue = searchQueue.concat(graph[name]);
|
||||
/**
|
||||
* Determine whether a person is a seller
|
||||
* @param {string} name Friend's name
|
||||
* @returns {boolean} Result of checking
|
||||
*/
|
||||
const personIsSeller = name => name[name.length - 1] === "m";
|
||||
|
||||
/**
|
||||
* Find a mango seller
|
||||
* @param {string} name Friend's name
|
||||
* @returns {boolean} Search results
|
||||
*/
|
||||
const search = name => {
|
||||
let searchQueue = [...graph[name]];
|
||||
// This array is how you keep track of which people you've searched before.
|
||||
const searched = [];
|
||||
while (searchQueue.length) {
|
||||
@@ -31,4 +40,4 @@ const search = (name) => {
|
||||
return false;
|
||||
};
|
||||
|
||||
search('you'); // thom is a mango seller!
|
||||
search("you"); // thom is a mango seller!
|
||||
|
||||
@@ -1,23 +1,35 @@
|
||||
const graph = {};
|
||||
graph.you = ['alice', 'bob', 'claire'];
|
||||
graph.bob = ['anuj', 'peggy'];
|
||||
graph.alice = ['peggy'];
|
||||
graph.claire = ['thom', 'jonny'];
|
||||
graph.you = ["alice", "bob", "claire"];
|
||||
graph.bob = ["anuj", "peggy"];
|
||||
graph.alice = ["peggy"];
|
||||
graph.claire = ["thom", "jonny"];
|
||||
graph.anuj = [];
|
||||
graph.peggy = [];
|
||||
graph.thom = [];
|
||||
|
||||
const isSeller = name => name[name.length - 1] === 'm';
|
||||
/**
|
||||
* Determine whether a person is a seller
|
||||
* @param {string} name Friend's name
|
||||
* @returns {boolean} Result of checking
|
||||
*/
|
||||
const isSeller = name => name[name.length - 1] === "m";
|
||||
|
||||
const search = (name, graph) => {
|
||||
const iter = (waited, visited) => {
|
||||
if (waited.length === 0) {
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* Find a mango seller
|
||||
* @param {string} name Friend's name
|
||||
* @param {Object} graph Hash table
|
||||
* @returns {boolean} Search results
|
||||
*/
|
||||
const search = (name, graph = {}) => {
|
||||
/**
|
||||
* Recursive function to test people
|
||||
* @param {Array} waited List of people you need to check
|
||||
* @param {Set} visited List of checked people
|
||||
*/
|
||||
const iter = (waited = [], visited) => {
|
||||
if (waited.length === 0) return false;
|
||||
const [current, ...rest] = waited;
|
||||
if (visited.has(current)) {
|
||||
return iter(rest, visited);
|
||||
}
|
||||
if (visited.has(current)) return iter(rest, visited);
|
||||
if (isSeller(current)) {
|
||||
console.log(`${current} is a mango seller!`);
|
||||
return true;
|
||||
@@ -29,4 +41,4 @@ const search = (name, graph) => {
|
||||
return iter(graph[name], new Set());
|
||||
};
|
||||
|
||||
search('you');
|
||||
search("you", graph);
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
function person_is_seller(name) {
|
||||
return name[name.length - 1] === "m";
|
||||
}
|
||||
|
||||
const graph = {};
|
||||
graph["you"] = ["alice", "bob", "claire"];
|
||||
graph["bob"] = ["anuj", "peggy"];
|
||||
@@ -12,6 +8,20 @@ graph["peggy"] = [];
|
||||
graph["thom"] = [];
|
||||
graph["jonny"] = [];
|
||||
|
||||
/**
|
||||
* Determine whether a person is a seller
|
||||
* @param {string} name Friend's name
|
||||
* @returns {boolean} Result of checking
|
||||
*/
|
||||
function personIsSeller(name) {
|
||||
return name[name.length - 1] === "m";
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a mango seller
|
||||
* @param {string} name Friend's name
|
||||
* @returns {boolean} Search results
|
||||
*/
|
||||
function search(name) {
|
||||
let search_queue = [];
|
||||
search_queue = search_queue.concat(graph[name]);
|
||||
@@ -21,7 +31,7 @@ function search(name) {
|
||||
let person = search_queue.shift();
|
||||
// Only search this person if you haven't already searched them
|
||||
if (searched.indexOf(person) === -1) {
|
||||
if (person_is_seller(person)) {
|
||||
if (personIsSeller(person)) {
|
||||
console.log(person + " is a mango seller!");
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
const graph = {};
|
||||
graph["you"] = ["alice", "bob", "claire"];
|
||||
graph["bob"] = ["anuj", "peggy"];
|
||||
graph["alice"] = ["peggy"];
|
||||
graph["claire"] = ["thom", "jonny"];
|
||||
graph["anuj"] = [];
|
||||
graph["peggy"] = [];
|
||||
graph["thom"] = [];
|
||||
graph["jonny"] = [];
|
||||
|
||||
/**
|
||||
* Determine whether a person is a seller
|
||||
* @param {string} name Friend's name
|
||||
* @returns {boolean} Result of checking
|
||||
*/
|
||||
function personIsSeller(name) {
|
||||
return name[name.length - 1] === "m";
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a mango seller
|
||||
* @param {string} name Friend's name
|
||||
* @param {Object} graph Hash table
|
||||
* @returns {boolean} Search results
|
||||
*/
|
||||
function search(name, graph) {
|
||||
graph = graph || {};
|
||||
/**
|
||||
* Recursive function to check people
|
||||
* @param {Array} waited List of people you need to check
|
||||
* @param {Array} visited List of checked people
|
||||
*/
|
||||
function inner(waited, visited) {
|
||||
waited = waited || [];
|
||||
if (waited.length === 0) return false;
|
||||
const person = waited[0];
|
||||
const waitedCloned = waited.slice(1);
|
||||
if (visited.indexOf(person) !== -1) return inner(waitedCloned, visited);
|
||||
if (personIsSeller(person)) {
|
||||
console.log(person + " is a mango seller!");
|
||||
return true;
|
||||
}
|
||||
return inner(waitedCloned.concat(graph[person]), visited.concat(person));
|
||||
}
|
||||
return inner(graph[name], []);
|
||||
}
|
||||
|
||||
search("you", graph);
|
||||
Reference in New Issue
Block a user