forked from AlgorithmsMeetup/PreviousAlgorithms
-
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.
- Loading branch information
Showing
12 changed files
with
141 additions
and
95 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 |
---|---|---|
@@ -1,6 +1,3 @@ | ||
// Note: The metric used here is Damerau–Levenshtein distance. | ||
// This metric imposes a uniform cost for insertion, deletion, | ||
// substitutions, and transposition, as these compose the most | ||
// common human typing mistakes. | ||
var editDistance = function(str1, str2) { | ||
|
||
}; |
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,16 @@ | ||
.viewport { | ||
position: relative; | ||
width: 512px; | ||
height: 512px; | ||
margin: 50px auto; | ||
background-color: #eee; | ||
border: 1px solid #ccc; | ||
} | ||
|
||
.point { | ||
position: absolute; | ||
width:0; | ||
height:0; | ||
border:2px solid black; | ||
border-radius: 2px; | ||
} |
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,16 @@ | ||
<html> | ||
<head> | ||
<title>Graphics!</title> | ||
<link rel="stylesheet" href="css/app.css" /> | ||
<script src='js/lib/jquery.js'></script> | ||
<script src='js/lib/underscore.js'></script> | ||
<script src='js/point2D.js'></script> | ||
<script src='js/point3D.js'></script> | ||
<script src='js/shape.js'></script> | ||
<script src='js/init.js'></script> | ||
</head> | ||
<body> | ||
<div class='viewport'> | ||
</div> | ||
</body> | ||
</html> |
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,6 @@ | ||
$(document).ready(function() { | ||
var cube = new Cube(new Point3D(0, 0, 0), 100); | ||
var rendered = cube.points.map(function(p) { | ||
return new Point2D(p.x, p.y); | ||
}); | ||
}); |
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,21 @@ | ||
var Point2D = function(x, y) { | ||
this.move(x, y); | ||
}; | ||
|
||
Point2D.prototype.remove = function() { | ||
if (this.el) this.el.remove(); | ||
}; | ||
|
||
Point2D.prototype.render = function() { | ||
this.remove(); | ||
var newEl = $("<div class='point'></div>"); | ||
newEl.css({top: 256 - this.y, left: this.x + 256}); | ||
$('.viewport').append(newEl); | ||
this.el = newEl; | ||
}; | ||
|
||
Point2D.prototype.move = function(x, y) { | ||
this.x = x; | ||
this.y = y; | ||
this.render(); | ||
}; |
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,5 @@ | ||
var Point3D = function(x, y, z) { | ||
this.x = x; | ||
this.y = y; | ||
this.z = z; | ||
}; |
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,43 @@ | ||
var Shape = function() { | ||
this.points = []; | ||
}; | ||
|
||
var Cube = function(center, radius) { | ||
var cx = center.x; | ||
var cxp = center.x + radius; | ||
var cxm = center.x - radius; | ||
var cy = center.y; | ||
var cyp = center.y + radius; | ||
var cym = center.y - radius; | ||
var cz = center.z; | ||
var czp = center.z + radius; | ||
var czm = center.z - radius; | ||
|
||
this.points = [ | ||
new Point3D(cxp, cyp, czp), | ||
new Point3D(cxm, cyp, czp), | ||
new Point3D(cxp, cym, czp), | ||
new Point3D(cxp, cyp, czm), | ||
new Point3D(cxm, cym, czp), | ||
new Point3D(cxm, cyp, czm), | ||
new Point3D(cxp, cym, czm), | ||
new Point3D(cxm, cym, czm), | ||
|
||
new Point3D(cx, cyp, czp), | ||
new Point3D(cx, cym, czp), | ||
new Point3D(cx, cyp, czm), | ||
new Point3D(cx, cym, czm), | ||
|
||
new Point3D(cxp, cy, czp), | ||
new Point3D(cxm, cy, czp), | ||
new Point3D(cxp, cy, czm), | ||
new Point3D(cxm, cy, czm), | ||
|
||
new Point3D(cxp, cyp, cz), | ||
new Point3D(cxm, cyp, cz), | ||
new Point3D(cxp, cym, cz), | ||
new Point3D(cxm, cym, cz), | ||
]; | ||
}; | ||
|
||
Cube.prototype = new Shape; |
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
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 |
---|---|---|
@@ -1,48 +1,39 @@ | ||
(function() { | ||
var cache, myCoinValues, maxAmount, generateAnswers, yourAnswer, valueInDollars, count, value, _i, _len, generateTest, answers; | ||
|
||
cache = [[]]; | ||
for (_i = 0, _len = coinValues.length; _i < _len; _i++) { | ||
value = coinValues[_i]; | ||
cache.push([]); | ||
var cache = [[]]; | ||
var answers = []; | ||
|
||
var coins = coinValues.sort(function(a, b) {return b - a;}); | ||
var coinsLength = coins.length; | ||
var maxAmount = amountsToTest[amountsToTest.length - 1]; | ||
|
||
// Fill in all values using dynamic programming. | ||
for (var i = 0; i < coinsLength; i++) { | ||
cache[0].push(1); | ||
} | ||
|
||
count = function(amount, coins) { | ||
var result = 0; | ||
if (cache[coins.length][amount]) { | ||
result = cache[coins.length][amount]; | ||
} else if (amount === 0) { | ||
result = 1; | ||
} else if (amount < 0) { | ||
result = 0; | ||
} else if (coins.length === 0) { | ||
result = 0; | ||
} else { | ||
result = count(amount, coins.slice(1)) + count(amount - coins[0], coins) | ||
for (var row = 1; row <= maxAmount; row++) { | ||
cache[row] = []; | ||
for (var col = 0; col < coinsLength; col++) { | ||
var left = col > 0 ? cache[row][col-1] : 0; | ||
var above = row - coins[col] >= 0 ? cache[row - coins[col]][col] : 0; | ||
cache[row][col] = left + above; | ||
} | ||
cache[coins.length][amount] = result; | ||
return result | ||
}; | ||
|
||
answers = []; | ||
myCoinValues = coinValues.sort(function(a, b) {return b - a;}); | ||
maxAmount = amountsToTest[amountsToTest.length - 1]; | ||
for (var i = 0; i <= maxAmount; i++) { | ||
answers[i] = count(i, myCoinValues) | ||
} | ||
|
||
amountInDollars = function(amount) { | ||
var amountInDollars = function(amount) { | ||
return (amount / 100).toFixed(2); | ||
}; | ||
|
||
generateTest = window.generateTest = function(amount) { | ||
var generateTest = window.generateTest = function(amount) { | ||
it("Change for $" + amountInDollars(amount), function() { | ||
expect(makeChange(amount)).to.equal(answers[amount]); | ||
var answer = cache[amount][coinsLength - 1]; | ||
expect(makeChange(amount)).to.equal(answer); | ||
}); | ||
}; | ||
|
||
for (var i = 0; i < amountsToTest.length; i++) { | ||
generateTest(amountsToTest[i]); | ||
}; | ||
} | ||
|
||
})(); |
Oops, something went wrong.