Skip to content

Commit

Permalink
fixed merge
Browse files Browse the repository at this point in the history
  • Loading branch information
peterkhayes committed Oct 21, 2014
2 parents de73306 + f99e9b7 commit f6f8c19
Show file tree
Hide file tree
Showing 12 changed files with 141 additions and 95 deletions.
5 changes: 1 addition & 4 deletions EditDistance/src/editDistance.js
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) {

};
16 changes: 16 additions & 0 deletions Graphics/css/app.css
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;
}
16 changes: 16 additions & 0 deletions Graphics/index.html
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>
6 changes: 6 additions & 0 deletions Graphics/js/init.js
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);
});
});
4 changes: 4 additions & 0 deletions Graphics/js/lib/jquery.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Graphics/js/lib/underscore.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions Graphics/js/point2D.js
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();
};
5 changes: 5 additions & 0 deletions Graphics/js/point3D.js
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;
};
43 changes: 43 additions & 0 deletions Graphics/js/shape.js
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;
12 changes: 6 additions & 6 deletions MakingChange/spec/amountsToTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@


// Easy difficulty.
var amountsToTest = [1, 2, 3, 4, 5, 6, 7, 8, 9,10, 12, 14, 16, 18, 20, 23, 26, 29, 32, 35, 38,42, 46, 50]
var amountsToTest = [1, 2, 3, 4, 5, 6, 7, 8, 9,10, 12, 14, 16, 18, 20, 23, 26, 29, 32, 35, 38, 42, 46, 50]

// Medium difficulty.
.concat([55, 60, 70,75, 80, 85, 90, 95, 100, 110, 120, 130, 140, 150, 175, 200, 225, 250, 300])
// .concat([55, 60, 70,75, 80, 85, 90, 95, 100, 110, 120, 130, 140, 150, 175, 200, 225, 250, 300])

// Hard difficulty!
.concat([500, 750, 1000, 1500, 2000, 3000, 5000, 10000])
// // Hard difficulty!
// .concat([500, 750, 1000, 1500, 2000, 3000, 5000, 10000])

// Hardest difficulty - see if you can get these in under a few seconds!
.concat([20000, 50000, 100000, 200000, 500000])
// // Hardest difficulty - see if you can get these in under a few seconds!
// .concat([20000, 50000, 100000, 200000, 500000])
51 changes: 21 additions & 30 deletions MakingChange/spec/spec.js
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]);
};
}

})();
Loading

0 comments on commit f6f8c19

Please sign in to comment.