Skip to content

Commit

Permalink
fixes to Making Change in preparation for next lesson
Browse files Browse the repository at this point in the history
  • Loading branch information
peterkhayes committed Oct 20, 2014
1 parent 563cb0c commit 38d2b7f
Show file tree
Hide file tree
Showing 13 changed files with 220 additions and 167 deletions.
43 changes: 17 additions & 26 deletions EditDistance/src/editDistance.js
Original file line number Diff line number Diff line change
@@ -1,7 +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) {
if (str1 === str2) {
return 0;
Expand All @@ -11,47 +7,42 @@ var editDistance = function(str1, str2) {
return str1.length || str2.length;
}

if (str2.length > str1.length) {
var temp = str1;
str1 = str2;
str2 = temp;
}

var len1 = str1.length;
var len2 = str2.length;

var prevPrevRow;
var prevRow = [];
// Set up the matrix.
var matrix = [];
for (var row = 0; row <= len1; row++) {
var arr = new Array(len2 + 1);
matrix.push(arr);
}

for (var row = 0; row <= len1; row++) {
matrix[row][0] = row;
}

// Fill in previous row with empty subsequences.
for (var col = 0; col <= len2; col++) {
prevRow[col] = col;
matrix[0][col] = col;
}
var currRow = [1];

// Fake iterating through the whole matrix,
// but remembering only two rows at a time.
for (var row = 1; row <= len1; row++) {
for (var col = 1; col <= len2; col++) {

// If two strings match at their final character, their LCS
// is the LCS of their prefixes, plus that final character.
if (str1[row-1] === str2[col-1]) {
currRow[col] = prevRow[col-1];
matrix[row][col] = matrix[row-1][col-1];

// Otherwise, their LCS is the best LCS that results from
// trimming either of their final characters, plus one.
} else {
var del = prevRow[col] + 1;
var ins = currRow[col-1] + 1;
var sub = prevRow[col-1] + 1;
currRow[col] = Math.min(del, ins, sub);
var del = matrix[row-1][col];
var ins = matrix[row][col-1];
var sub = matrix[row-1][col-1];
matrix[row][col] = Math.min(del, ins, sub) + 1;
}
}
prevPrevRow = prevRow;
prevRow = currRow;
currRow = [row + 1];
}

return prevRow[len2];
return matrix[len1][len2];
};
56 changes: 32 additions & 24 deletions EditDistance/src/fuzzyStringMatch.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,49 @@
var fuzzyStringMatch = function(text, query) {
if (query === text) {
return 0;
}

if (!query.length || !text.length) {
return query.length || text.length;
}

var qLen = query.length;
var tLen = text.length;
var queryLen = query.length;
var textLen = text.length;

var prevPrevRow;
var prevRow = [];
// Fill in previous row with empty subsequences.
for (var col = 0; col <= tLen; col++) {
prevRow[col] = 0;
// Set up the matrix.
var matrix = [];
for (var row = 0; row <= queryLen; row++) {
var newRow = new Array(textLen + 1);
newRow[0] = row;
if (row === 0) {
for (var col = 1; col <= textLen; col++) {
newRow[col] = 0;
}
}
matrix.push(newRow);
}
var currRow = [1];

// Fake iterating through the whole matrix,
// but remembering only two rows at a time.
for (var row = 1; row <= qLen; row++) {
for (var col = 1; col <= tLen; col++) {
for (var row = 1; row <= queryLen; row++) {
for (var col = 1; col <= textLen; col++) {

// If two strings match at their final character, their LCS
// is the LCS of their prefixes, plus that final character.
if (query[row-1] === text[col-1]) {
currRow[col] = prevRow[col-1];
matrix[row][col] = matrix[row-1][col-1];


// Otherwise, their LCS is the best LCS that results from
// trimming either of their final characters.
// trimming either of their final characters, plus one.
} else {
var del = prevRow[col] + 1;
var ins = currRow[col-1] + 1;
var sub = prevRow[col-1] + 1;
currRow[col] = Math.min(del, ins, sub);
};
var del = matrix[row-1][col] + 1;
var ins = matrix[row][col-1] + 1;
var sub = matrix[row-1][col-1] + 1;
matrix[row][col] = Math.min(del, ins, sub);
}
}
prevPrevRow = prevRow;
prevRow = currRow;
currRow = [row + 1];
}

var min = Math.min.apply(null, prevRow);
console.log(min, qLen);
return (qLen-min)/qLen;
var minDistance = Math.min.apply(null, matrix[queryLen]);
return 1 - minDistance/queryLen;
};
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;
};
Loading

0 comments on commit 38d2b7f

Please sign in to comment.