diff --git a/06_breadth-first_search/coffeescript/01_breadth-first_search.coffee b/06_breadth-first_search/coffeescript/01_breadth-first_search.coffee new file mode 100644 index 00000000..2ea21827 --- /dev/null +++ b/06_breadth-first_search/coffeescript/01_breadth-first_search.coffee @@ -0,0 +1,32 @@ +graph = {} + +graph["you"] = ["alice", "claire", "bob"] +graph["bob"] = ["peggy", "anuj"] +graph["alice"] = ["peggy"] +graph["claire"] = ["jonny", "thom"] +graph["anuj"] = [] +graph["peggy"] = [] +graph["jonny"] = [] +graph["thom"] = [] + +bfs = (name) -> + deque = graph[name][..] + checked = [] + + while deque.length > 0 + pessoa = deque.shift() + + if pessoa not in checked + if personIsSeller(pessoa) + return true + else + deque = deque.concat(graph[pessoa]) + checked.push(pessoa) + + return false + + +personIsSeller = (name) -> + name[name.length - 1] is "m" + +console.log bfs("you") \ No newline at end of file diff --git a/06_breadth-first_search/coffeescript/README.md b/06_breadth-first_search/coffeescript/README.md new file mode 100644 index 00000000..1c21066a --- /dev/null +++ b/06_breadth-first_search/coffeescript/README.md @@ -0,0 +1,12 @@ +## How to Run the Algorithm + +``` +# Install CoffeeScript +npm install -g coffeescript + +# Run directly +coffee 01_breadth-first_search.coffee + +# Or compile and run +coffee -c 01_breadth-first_search.coffee && node 01_breadth-first_search.js +``` \ No newline at end of file