From b4b5161ff99e8c072bd8787f8f8317b6342bb6ad Mon Sep 17 00:00:00 2001 From: Alexandrshy Date: Wed, 24 Jul 2019 22:38:14 +0400 Subject: [PATCH] Fixed formatting problems, added JSDoc, added example for JS, fixed example for JS --- .../ES6/01_breadth-first_search.js | 29 +++++++---- ...breadth-first_search_by_Rytikov_Dmitrii.js | 40 ++++++++++------ .../javascript/01_breadth-first_search.js | 20 ++++++-- .../02_breadth-first_search_recursion.js | 48 +++++++++++++++++++ 4 files changed, 108 insertions(+), 29 deletions(-) create mode 100644 06_breadth-first_search/javascript/02_breadth-first_search_recursion.js diff --git a/06_breadth-first_search/ES6/01_breadth-first_search.js b/06_breadth-first_search/ES6/01_breadth-first_search.js index f140373..1767cbe 100644 --- a/06_breadth-first_search/ES6/01_breadth-first_search.js +++ b/06_breadth-first_search/ES6/01_breadth-first_search.js @@ -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! diff --git a/06_breadth-first_search/ES6/02_breadth-first_search_by_Rytikov_Dmitrii.js b/06_breadth-first_search/ES6/02_breadth-first_search_by_Rytikov_Dmitrii.js index 5f3836c..75f5405 100644 --- a/06_breadth-first_search/ES6/02_breadth-first_search_by_Rytikov_Dmitrii.js +++ b/06_breadth-first_search/ES6/02_breadth-first_search_by_Rytikov_Dmitrii.js @@ -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); diff --git a/06_breadth-first_search/javascript/01_breadth-first_search.js b/06_breadth-first_search/javascript/01_breadth-first_search.js index c3f478f..a470065 100644 --- a/06_breadth-first_search/javascript/01_breadth-first_search.js +++ b/06_breadth-first_search/javascript/01_breadth-first_search.js @@ -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; } diff --git a/06_breadth-first_search/javascript/02_breadth-first_search_recursion.js b/06_breadth-first_search/javascript/02_breadth-first_search_recursion.js new file mode 100644 index 0000000..c08be6c --- /dev/null +++ b/06_breadth-first_search/javascript/02_breadth-first_search_recursion.js @@ -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);