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.
fixes to Making Change in preparation for next lesson
- Loading branch information
1 parent
563cb0c
commit 38d2b7f
Showing
13 changed files
with
220 additions
and
167 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
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,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; | ||
}; |
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; | ||
}; |
Oops, something went wrong.