forked from irisyuan/Algorithm-Implementations
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request kennyledet#402 from aaronabf/master
Added Javascript implemented for 4 problems
- Loading branch information
Showing
4 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// 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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Author: Aaron Friedlander 2014 | ||
|
||
// A function that memoizes a function | ||
// Adapted from my code here 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |