diff --git a/AStar/participantSolutions/bendra.js b/AStar/participantSolutions/bendra.js new file mode 100644 index 0000000..0098768 --- /dev/null +++ b/AStar/participantSolutions/bendra.js @@ -0,0 +1,86 @@ +/* + Hello, algorithmicists! Today we're going to implement the A* search algorithm. + + The fundamental unit of this algorithm is the `node`. Nodes represent spaces on the + board, and have the following properties and methods. + + Properties: + f: // estimated path length through this node. + g: // shortest path found (so far) from start to this node. + + Methods: + indexIn(set): // returns the index of the node in the set, or -1 if not present. + isGoal(): // returns whether the node is the goal. + neighbors(): // returns a list of nodes adjacent to the current one. + calcHeuristic(): // returns the heuristic distance from this node to the goal. + visit(): // tells the visualizer that you've visited the current node. (good for debugging) + + You should also note that we've given you the first two lines, defining the closed + and open sets. The closed set is a list of nodes that you've visited already, and + the open set is a list of nodes "on the boundary" - that is, you've visited one of + their neighbors, but you haven't yet processed them. You don't have to mess with + the sets directly - you can also use Node.indexIn(set). + + Finally, we've done a little magic with the "neighbors" function, which will allow + the visualizer to retrace your steps and draw the path. Don't worry about this part! + + Just be sure to return the goal node once you've found it. + + */ + +//trampoline - we will use this to recurse +function trampoline(n) { + while (n && n instanceof Function) { + n = n.apply(n.context, n.args); + } + return n; +} + +window.solve = function(startNode) { + var open = [ startNode ]; + var closed = []; + startNode.g = 0; + startNode.f = startNode.g + startNode.calcHeuristic(); + var current = startNode; + // recursively find path + // this is the actual work... + function findNextStepInPath(current, open, closed) { + + // look at open nodes for shortest path + var shortestIdx = 0; + for ( var i = 0; i < open.length; i++) { + if (open[i].f < open[shortestIdx].f) { + shortestIdx = i; + } + } + + // remove shortest from open set, add to closed set + current = open.splice(shortestIdx, 1).pop(); + closed.push(current); + + // tell visualizer we have visited current node + current.visit(); + + // are we done? + if (current.isGoal()) { + return current; + } + + // assign values to neighbors + var neighbors = current.neighbors(); + for ( var i = 0; i < neighbors.length; i++) { + var nextNode = neighbors[i]; + //only add process if we haven't looked at nextnode yet... + if (nextNode.indexIn(closed) === -1 && nextNode.indexIn(open) === -1) { + nextNode.g = current.g + 1; + nextNode.f = nextNode.g + nextNode.calcHeuristic(); + open.push(nextNode); + } + } + //recursively call function again (note bind to keep stack from growing) + return findNextStepInPath.bind(null, current, open, closed); + } + //recursively call function again (note bind to keep stack from growing) + return trampoline(findNextStepInPath.bind(null, current, open, closed)); + +} diff --git a/MakingChange/participantSolutions/bendra.js b/MakingChange/participantSolutions/bendra.js new file mode 100644 index 0000000..ec42cda --- /dev/null +++ b/MakingChange/participantSolutions/bendra.js @@ -0,0 +1,52 @@ +/* + Greetings, algorithmics! + + Today, your challenge is to figure out the number of ways to make change for a given amount of money. + More precisely, given a number of cents and a set of coins, determine how many combinations of those coins + sum to that number of cents. + This is a classic algorithms problem that's common in interviews! + + Your only helper is a list of common US currency denominations. + I didn't include half-dollars, two-dollar bills, and the like, but if you'd prefer, you can adjust + the list of coin-values and the tests will auto-update. + + Enjoy! +*/ + +var coinValues = [10000, 5000, 2000, 1000, 500, 100, 50, 25, 10, 5, 1]; + +var cache = [[]]; + for (_i = 0, _len = coinValues.length; _i < _len; _i++) { + value = coinValues[_i]; + cache.push([]); + } + +var makeChange = function(amount) { + return makeSomeChange(amount).length; +}; + +//returns array of array-of-possible-combinations +var makeSomeChange = function(amount, coins) { + coins = coins || coinValues; + if (cache[coins.length][amount]) { + result = cache[coins.length][amount]; + } + var output = []; + for(var i = 0; i 0){ + for(var j = 0; j< smallerCoinChange.length; j++){ + output.push([coinValue].concat(smallerCoinChange[j])); + } + } + } + } + cache[coins.length][amount] = output; + return output; +}; diff --git a/TreeSearch/participantSolutions/bendra/BFselect.js b/TreeSearch/participantSolutions/bendra/BFselect.js new file mode 100644 index 0000000..a0b90c5 --- /dev/null +++ b/TreeSearch/participantSolutions/bendra/BFselect.js @@ -0,0 +1,25 @@ +// Write your code here + +// Write your code here +Tree.prototype.BFSelect = function(fn){ + var out = []; + var depth=0; + bfSearch([this], depth, out, fn); + return out; +}; + + +function bfSearch(trees, depth, out, fn){ + var nextRow = []; + for( var i= 0; i< trees.length; i++){ + var nextTree=trees[i]; + if(fn(nextTree.value, depth)){ + out.push(nextTree.value); + } + //NOTE: concat does not mute the target array, instead it produces a new one + nextRow = nextRow.concat(nextTree.children); + } + if(nextRow.length){ + bfSearch(nextRow, depth+1, out, fn); + } +} diff --git a/TreeSearch/participantSolutions/bendra/DFselect.js b/TreeSearch/participantSolutions/bendra/DFselect.js new file mode 100644 index 0000000..e67ee7f --- /dev/null +++ b/TreeSearch/participantSolutions/bendra/DFselect.js @@ -0,0 +1,17 @@ +// Write your code here +Tree.prototype.DFSelect = function(fn){ + var out = []; + var depth=0; + dfSearch(this, depth, out, fn); + return out; +}; + + +var dfSearch = function(tree, depth, out, fn){ + if(fn(tree.value, depth)){ + out.push(tree.value); + } + for(var i=0; i< tree.children.length; i++){ + dfSearch(tree.children[i], depth + 1, out, fn); + }; +}; diff --git a/TreeSearch/participantSolutions/bendra/testBF.html b/TreeSearch/participantSolutions/bendra/testBF.html new file mode 100644 index 0000000..b33d6ae --- /dev/null +++ b/TreeSearch/participantSolutions/bendra/testBF.html @@ -0,0 +1,25 @@ + + + + + + + +

hi mom

+ + + + diff --git a/TreeSearch/participantSolutions/bendra/testDF.html b/TreeSearch/participantSolutions/bendra/testDF.html new file mode 100644 index 0000000..bb404ec --- /dev/null +++ b/TreeSearch/participantSolutions/bendra/testDF.html @@ -0,0 +1,26 @@ + + + + + + +Test Depth First + + +

hi mom

+ + +