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
var q = require('q'); | |
var Schema = require('jugglingdb').Schema; | |
var schema = new Schema('postgres', { | |
database: 'woonketwong', | |
username: 'woonketwong' | |
}); | |
// The first argument to schema.define is the table | |
// and schema name. Do not use any capital letter | |
// in the table name because the database create table |
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
var selectionRank = function(array, rank){ | |
// randomly select a pivot | |
var pivotIndex = Math.floor( (Math.random() * array.length) ); | |
var pivot = array[pivotIndex]; | |
// left array stores the smallest <rank> elements in the array | |
var leftArr = []; | |
var rightArr = []; | |
// partition into left and right arrays | |
for (var i = 0; i < array.length; i++){ |
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
var sortAndGroupAnagram = function(array){ | |
var result = {}; | |
var sortedResult = []; | |
var currentKey; | |
var allKey; | |
for (var i = 0; i < array.length; i++){ | |
currentKey = array[i].split('').sort().join(''); | |
if ( result.hasOwnProperty(currentKey) ){ | |
result[currentKey].push(array[i]); |
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
var mergeTwoSortedArray = function(array1, array2){ | |
var array1IndexEnd = array1.length - array2.length - 1; | |
var arrayExtraIndexEnd = array1.length - 1; | |
var array2IndexEnd = array2.length - 1; | |
while( array2IndexEnd >= 0){ | |
if ( array1[array1IndexEnd] > array2[array2IndexEnd]){ | |
array1[arrayExtraIndexEnd] = array1[array1IndexEnd]; | |
arrayExtraIndexEnd--; | |
array1IndexEnd-- |
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
var findLocInInterspersedSortedArr = function(strArr, target, first, last){ | |
var mid = Math.floor( last - first / 2); | |
if (strArr[mid] === ''){ | |
var left = mid - 1; | |
var right = mid + 1; | |
while (true){ | |
if (left < first && right > last){ | |
return -1; |
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
var quickSort = function(array, left, right){ | |
var leftIndex = partition(array, left, right); | |
if (left < leftIndex - 1){ | |
quickSort(array, left, leftIndex-1); | |
} | |
if (right > leftIndex){ | |
quickSort(array, leftIndex, right); |
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
var insertionSort = function(array){ | |
var target; | |
var sortedIndex = 0; | |
var targetIndex; | |
for(var i = 0; i < array.length; i++){ | |
target = array[i]; | |
targetIndex = i; | |
for(var j = i; j >= sortedIndex; j--){ |
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
var breathFirstSearch = function(node){ | |
var queue = []; | |
// visit root | |
console.log("Node key, value", node.key, node.value); | |
root.visit=true; | |
queue.push(root); | |
while(queue.length !== 0){ | |
currentNode = queue.pop(); |
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
var isBalanced = function(node){ | |
var maxDepth = function(node){ | |
if (node === undefined){ | |
return 0; | |
} | |
return 1 + Math.max(maxDepth(node.next[0]), maxDepth(node.next[1])); | |
} | |
var minDepth = function(node){ |
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
var search = function(startNode, endNode){ | |
//visit startNode | |
console.log("Visting:", startNode.key); | |
if (startNode.key === endNode.key){ | |
return true; | |
} | |
startNode.visit = true; | |
OlderNewer