From 3a20f0bb04e52f48f4f9ee3854c92882d18731ff Mon Sep 17 00:00:00 2001 From: Aaron Friedlander Date: Tue, 12 Aug 2014 19:23:56 -0400 Subject: [PATCH 1/4] Added Javascript solution to 100 Doors Problem --- .../Javascript/aaronabf/100_Doors.js | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 100_Doors_Problem/Javascript/aaronabf/100_Doors.js diff --git a/100_Doors_Problem/Javascript/aaronabf/100_Doors.js b/100_Doors_Problem/Javascript/aaronabf/100_Doors.js new file mode 100644 index 00000000..ec7b23d2 --- /dev/null +++ b/100_Doors_Problem/Javascript/aaronabf/100_Doors.js @@ -0,0 +1,64 @@ + +// Author: Aaron Friedlander 2014 + +// Problem: You have 100 doors in a row that are all initially closed. You make +// 100 passes by the doors. The first time through, you visit every door and +// toggle the door (if the door is closed, you open it; if it is open, you +// close it). The second time you only visit every 2nd door (door #2, #4, #6, +// ...). The third time, every 3rd door (door #3, #6, #9, ...), etc, until you +// only visit the 100th door. + +// Question: What state are the doors in after the last pass? Which are open, +// which are closed? + +// Implemented in two ways: +// 1) Loop through array of "doors" opening respective doors in each iteration +// 2) The only doors that remain open are whose numbers are perfect squares. +// Using this knowledge, only doors with indices of a perfect square + 1 are +// opened. + +function run(numberOfDoors) { + // Array to represent the doors: false represents closed, true represents open + var doors = Array.apply(null, new Array(numberOfDoors)).map(Boolean.prototype.valueOf,false); + + // Run algorithms + var doorOutcome1 = iterateDoors(doors); + var doorOutcome2 = perfectSquareDoors(doors); + + // Print out results + if (doorOutcome1 === doorOutcome2) { + for (var i = 0; i < numberOfDoors; i++) { + var doorNum = i + 1; + var doorVal = doorOutcome1[i] ? 'Open' : 'Closed'; + console.log('Door #' + doorNum + ' is ' + doorVal); + } + } else { + console.log('There was a issue in the algorithm'); + } +} + +function iterateDoors(doors) { + for (var pass = 0; pass < doors.length; pass++) { + for (var i = pass; i < doors.length; i += (pass + 1)) { + doors[i] = !doors[i]; + } + } + + return doors; +} + +function isPerfectSquare(num) { + // Javascript trick for determining if a number is a perfect square + var sqrt = Math.sqrt(num); + return (sqrt === (sqrt | 0)); +} + +function perfectSquareDoors(doors) { + for (var i = 0; i < doors.length; i++) { + if (isPerfectSquare(i+1)) { + doors[i] = true; + } + } + + return doors; +} From d95ce9efb5388c92b38cfa7281c97a22e1fcdc94 Mon Sep 17 00:00:00 2001 From: Aaron Friedlander Date: Tue, 12 Aug 2014 19:29:56 -0400 Subject: [PATCH 2/4] Added Javascript solution to the memoization problem --- .../Javascript/aaronabf/100_Doors.js | 1 - Memoization/Javascript/aaronabf/memoize.js | 34 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 Memoization/Javascript/aaronabf/memoize.js diff --git a/100_Doors_Problem/Javascript/aaronabf/100_Doors.js b/100_Doors_Problem/Javascript/aaronabf/100_Doors.js index ec7b23d2..5c172762 100644 --- a/100_Doors_Problem/Javascript/aaronabf/100_Doors.js +++ b/100_Doors_Problem/Javascript/aaronabf/100_Doors.js @@ -1,4 +1,3 @@ - // Author: Aaron Friedlander 2014 // Problem: You have 100 doors in a row that are all initially closed. You make diff --git a/Memoization/Javascript/aaronabf/memoize.js b/Memoization/Javascript/aaronabf/memoize.js new file mode 100644 index 00000000..f9e82457 --- /dev/null +++ b/Memoization/Javascript/aaronabf/memoize.js @@ -0,0 +1,34 @@ +// Author: Aaron Friedlander 2014 + +// A function that memoizes a function +// Adapted from https://github.com/aaronabf/memoization/blob/master/memoizer.js + +// Stores cached results +var dict = {}; + +// Memoizes a function +function memoizer(f) { + function newFunction(n) { + if (dict[n]) { + return dict[n]; + } + + var newVal = f(newFunction, n); + dict[n] = newVal; + return newVal; + } + + return newFunction; +} + +// Test on fibonnaci +function fibMemo(g, n) { + if (n === 0) { + return 0; + } else if (n === 1) { + return 1; + } else { + return g(n-2) + g(n-1); + } +} +var fib = memoizer(fibMemo); From 0c2bfb71fbbb764bccabac0e50f58c1e0b644660 Mon Sep 17 00:00:00 2001 From: Aaron Friedlander Date: Tue, 12 Aug 2014 19:54:28 -0400 Subject: [PATCH 3/4] Added roman numal script taken from blog --- Roman_Numerals/Javascript/aaronabf/roman.js | 28 +++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Roman_Numerals/Javascript/aaronabf/roman.js diff --git a/Roman_Numerals/Javascript/aaronabf/roman.js b/Roman_Numerals/Javascript/aaronabf/roman.js new file mode 100644 index 00000000..71448fc1 --- /dev/null +++ b/Roman_Numerals/Javascript/aaronabf/roman.js @@ -0,0 +1,28 @@ +// ALL CREDIT GOES TO Steven Levithan @ http://blog.stevenlevithan.com/archives/javascript-roman-numeral-converter + +function romanize (num) { + if (!+num) + return false; + var digits = String(+num).split(""), + key = ["","C","CC","CCC","CD","D","DC","DCC","DCCC","CM", + "","X","XX","XXX","XL","L","LX","LXX","LXXX","XC", + "","I","II","III","IV","V","VI","VII","VIII","IX"], + roman = "", + i = 3; + while (i--) + roman = (key[+digits.pop() + (i * 10)] || "") + roman; + return Array(+digits.join("") + 1).join("M") + roman; +} + +function deromanize (str) { + var str = str.toUpperCase(), + validator = /^M*(?:D?C{0,3}|C[MD])(?:L?X{0,3}|X[CL])(?:V?I{0,3}|I[XV])$/, + token = /[MDLV]|C[MD]?|X[CL]?|I[XV]?/g, + key = {M:1000,CM:900,D:500,CD:400,C:100,XC:90,L:50,XL:40,X:10,IX:9,V:5,IV:4,I:1}, + num = 0, m; + if (!(str && validator.test(str))) + return false; + while (m = token.exec(str)) + num += key[m[0]]; + return num; +} From 8f927b25666d6096e35df5d4c58558fbbfdc52fc Mon Sep 17 00:00:00 2001 From: Aaron Friedlander Date: Tue, 12 Aug 2014 23:08:15 -0400 Subject: [PATCH 4/4] Added Javascript solution to the n-queen problem --- Memoization/Javascript/aaronabf/memoize.js | 2 +- N_Queens/Javascript/aaronabf/n-queens.js | 54 ++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 N_Queens/Javascript/aaronabf/n-queens.js diff --git a/Memoization/Javascript/aaronabf/memoize.js b/Memoization/Javascript/aaronabf/memoize.js index f9e82457..08f226e6 100644 --- a/Memoization/Javascript/aaronabf/memoize.js +++ b/Memoization/Javascript/aaronabf/memoize.js @@ -1,7 +1,7 @@ // Author: Aaron Friedlander 2014 // A function that memoizes a function -// Adapted from https://github.com/aaronabf/memoization/blob/master/memoizer.js +// Adapted from my code here https://github.com/aaronabf/memoization/blob/master/memoizer.js // Stores cached results var dict = {}; diff --git a/N_Queens/Javascript/aaronabf/n-queens.js b/N_Queens/Javascript/aaronabf/n-queens.js new file mode 100644 index 00000000..4c69c73f --- /dev/null +++ b/N_Queens/Javascript/aaronabf/n-queens.js @@ -0,0 +1,54 @@ +// Author: Aaron Friedlander 2014 + +// A function that memoizes a function +// Adapted from my code here https://github.com/aaronabf/n-queens/blob/master/n_queen.js + +// To test on 8 queens: +// console.log(attemp(8)); + +// Main function, abstracted helper function +function attemp(n) { + return try1(point(1,1), n, []); +} + +// Converts two numbers into a "point" object +function point(x,y) { + return { i : x, j : y }; +} + +// Tests that the point does not conflict with any other points on the board +function conflict(p1, board) { + return board.some(function(p2) { + return ((p1.i === p2.i) || + (p1.j === p2.j) || + (p1.i - p1.j === p2.i - p2.j) || + (p1.i + p1.j === p2.i + p2.j)); + }); +} + +// First mutual recursive function, helps construct array of points +function try1(p, n, board) { + var results = try2(p, n, board); + + if (results) { + return results; + } else if (p.j === n) { + return false; + } else { + return try1(point(p.i, p.j+1), n, board); + } +} + +// Second mutual recursive function, helps construct array of points +function try2(p, n, board) { + if (conflict(p, board)) { + return false; + } else if (p.i === n) { + board.unshift(p); + return board; + } else { + var boardCopy = board.slice(0); + boardCopy.unshift(p); + return try1(point(p.i+1, 1), n, boardCopy); + } +}