Created
April 5, 2011 15:08
-
-
Save elrasguno/903797 to your computer and use it in GitHub Desktop.
InterviewHello.js
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<script type="text/javascript" src="./interview_hello.js"></script> | |
</head> | |
<body> | |
</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
var InterviewHello = function() { | |
Array.prototype.sum = function() { | |
var sum = 0; | |
this.forEach(function(v) { | |
sum += v; | |
}); | |
return sum; | |
}; | |
/*** | |
* do N integers in an array add up to X? | |
* @param {int|null} rX integer to look for | |
* @param {int|null} rN number of integers to add up to rX | |
* @param {int|null} rListSize length of array to be generated | |
* @param {array|null} rList list to be operated on | |
**/ | |
var nSumToX = function(rX, rN, rListSize, rList) { | |
var vN = rN || 2, | |
vX = rX || 42, | |
vListSize = rListSize || 50, | |
vIterations = 0, | |
vResultsList = [], | |
vDebug = (console && false), | |
i, l; | |
// Generate a random list of 50 integers. | |
vList = rList || (function() { | |
var vRand, vNeg, i, vResult = []; | |
for (i=0; i < vListSize; i++) { | |
vNeg = Math.round(Math.random()); | |
vRand = Math.floor(Math.random() * 100); | |
vRand = (vNeg ? vRand * -1 : vRand); | |
vResult.push(vRand); | |
} | |
return vResult; | |
})(); | |
console.log(vList); | |
/*** | |
* TODO: this is working if the length of vList is big enough, but | |
* it's not resetting vSkipIdxHash or vResultsList properly | |
* when it fails to find what it's looking for during an interation. | |
* I've been looking at it for too long and can't figure it out. | |
**/ | |
var vFindIntsThatSumsToX = function vFindIntsThatSumsToX(rValueToFind, rNum, rSkipIdxHash) { | |
vDebug && console.log('hey', arguments); | |
var vSkipIdxHash = rSkipIdxHash || {}, | |
vHash = {}, | |
i, l, vCurrVal, vDiffVal; | |
for (i=0, l=vList.length; i < l; i++) { | |
// Count interations for after the fact | |
vIterations++; | |
vCurrVal = vList[i]; | |
vDiffVal = rValueToFind - vCurrVal; | |
// Don't look at the index of the current i value. | |
if (rSkipIdxHash && rSkipIdxHash[i] !== undefined) { | |
vDebug && console.log('skipping index:', i, vCurrVal); | |
continue; | |
} | |
if (rNum === 2) { | |
// Check hash for "value" from above. | |
if (vHash[vDiffVal] !== undefined) { | |
vResultsList.push(vCurrVal); | |
vResultsList.push(vDiffVal); | |
console.log('vResultsList: ' + vResultsList, vResultsList.sum()); | |
console.log('vIterations', vIterations); | |
return true; | |
} | |
// Populate vHash | |
vHash[vCurrVal] = vDiffVal; | |
} else { | |
vSkipIdxHash[i] = vCurrVal; | |
vResultsList.push(vCurrVal); | |
if (vFindIntsThatSumsToX(vDiffVal, rNum - 1, vSkipIdxHash)) { | |
return true; | |
} else { | |
var vTmp = vResultsList.pop(); | |
vSkipIdxHash[i] = undefined; | |
vDebug && console.log("FAIL, popped: ", vTmp, i, vCurrVal); | |
} | |
} | |
} | |
return false; | |
}; | |
return vFindIntsThatSumsToX(vX, vN); | |
}; | |
// hellonnna | |
var getMaxConsecutiveChar = function(rStr) { | |
var vChars = rStr.split(''), | |
vMaxConsecutive = 1, | |
vResult = 1, | |
vLastChar, | |
i, l; | |
for (i = 0, l = vChars.length; i < l; i++) { | |
if (i === 0) { | |
vLastChar = vChars[i]; | |
} else { | |
if (vLastChar === vChars[i]) { | |
vMaxConsecutive++; | |
} else { | |
vMaxConsecutive = 1; | |
} | |
vResult = Math.max(vMaxConsecutive, vResult); | |
vLastChar = vChars[i]; | |
} | |
} | |
return vResult; | |
}; | |
// random, weighted selection | |
var getRandomWeightedSelection = function(rListOfWeightedObjects) { | |
var vWeightsSum = 0, | |
vRand, | |
i, j, l; | |
for (i = 0, l = rListOfWeightedObjects.length; i < l; i++) | |
{ | |
vWeightsSum += rListOfWeightedObjects[i].weight; | |
} | |
vRand = Math.floor(Math.random() * vWeightsSum); | |
for (i = 0, j = 0, l = rListOfWeightedObjects.length; i < l; i++) | |
{ | |
j += rListOfWeightedObjects[i].weight; | |
if (j > vRand) | |
{ | |
return rListOfWeightedObjects[i]; | |
} | |
} | |
}; | |
// test getRandomWeightedSelection | |
var test_getRandomWeightedSelection = function() { | |
var vData = [{name : 'A', weight: 2}, {name : 'B', weight: 6}], | |
vResults = [], | |
vIterations = 100, | |
i, j; | |
for (i = 0; i < vIterations; i++) | |
{ | |
for (j = 0; j < 8; j++) | |
{ | |
if (j === 0) vResults.push({'A' : 0, 'B' : 0}); | |
vResults[i][getRandomWeightedSelection(vData).name]++; | |
} | |
} | |
var vNumA = 0, vNumB = 0; | |
for (i = 0, l = vResults.length; i < l; i++) | |
{ | |
vNumA += vResults[i]['A']; | |
vNumB += vResults[i]['B']; | |
} | |
return [Math.round(vNumA / vIterations), Math.round(vNumB / vIterations)]; | |
}; | |
// sub / pub | |
var _handlers = {}; | |
var subscribe = function(rTopic, rCallback) | |
{ | |
if (!_handlers[rTopic]) _handlers[rTopic] = []; | |
_handlers[rTopic].push(rCallback); | |
}; | |
var publish = function(rTopic) | |
{ | |
var i; | |
if (!_handlers[rTopic]) return; | |
for (i = 0; i < _handlers[rTopic].length; i++) | |
{ | |
_handlers[rTopic][i](Array.prototype.slice.call(arguments).slice(1)); | |
} | |
}; | |
// bind | |
var bind = function(rScope, rFuncName) | |
{ | |
return function() { | |
return rScope[rFuncName].apply(rScope, arguments); | |
}; | |
}; | |
// parse, replace tokens | |
// fibonacci | |
return { | |
getMaxConsecutiveChar : getMaxConsecutiveChar, | |
getRandomWeightedSelection : getRandomWeightedSelection, | |
test_getRandomWeightedSelection : test_getRandomWeightedSelection, | |
subscribe : subscribe, | |
publish : publish, | |
nSumToX : nSumToX | |
} | |
} | |
this.interview_hello = new InterviewHello(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment