shysta
BAN USER/*
A = [1, 2, 3, 3]
B = [2, 3, 3, 4]
C = [1, 2, 2, 2]
target = 7
*/
function getCombinations(a, b, c) {
var size = a.length;
var tuples = [];
for (var i = 0; i < size; i++) {
if (i >= 0 && a[i - 1] === a[i]) {
continue;
}
for (var j = 0; j < size; j++) {
if (j >= 0 && b[j - 1] === b[j]) {
continue;
}
tuples.push([a[i], b[j]]);
}
}
for (var k = 0, kk = tuples.length; k < kk; k++) {
var tuple = tuples[k];
var difference = 7 - tuple[0] - tuple[1];
if (c.indexOf(difference) >= 0) {
tuples[k].push(difference)
} else {
tuples[k] = null;
}
}
return tuples;
}
function Node(parent, left, right) {
this.parent = parent;
this.left = left;
this.right = right;
}
Node.prototype.getHeight = function() {
var counter = 0,
currNode = this;
while (currNode.parent !== undefined) { //root is then depth 0
currNode = currNode.parent;
counter++;
}
return counter;
}
Node.prototype.getAncestor = function(height) {
var node;
while (height > 0) {
node = this.parent;
height--;
}
return node;
}
function closestCommonAncestor(n1, n2) {
var d1 = n1.getHeight(),
d2 = n2.getHeight(),
delta = Math.abs(d1 - d2);
if (delta > 0) {
if (d1 < d2) {
n2 = n2.getAncestor(delta);
} else {
n1 = n1.getAncestor(delta);
}
}
while (n1 && n2) {
if (n1 === n2) {
return n1;
}
n1 = n1.getAncestor(1);
n2 = n2.getAncestor(1);
}
function inPlaceReverse(str) {
function reverse(str, i, j) {
j--;
while (i < j) {
tmp = str[i];
str[i] = str[j];
str[j] = tmp;
i++;
j--;
}
}
str = str.split('');
var tmp, i = 0, j = str.length;
//Completely reverse string first
reverse(str, i, j);
i = 0, j = 0;
//The reverse words
while (i < str.length) {
while (/\w/.test(str[j]) === true && j < str.length) {
j++;
};
reverse(str, i, j);
j++;
i = j;
}
return str.join('');
}
function getPattern(str) {
str = str.split('');
var hash = {},
currentChar = str[0],
currentSequenceLength = 1,
maximumSequenceLength = 0,
result = [];
for (i = 0; i < str.length; i++) {
if (currentChar !== str[i + 1]) {
if (/\w/.test(currentChar) === true) {
hash[currentSequenceLength] = hash[currentSequenceLength] || [];
hash[currentSequenceLength].push(currentChar);
maximumSequenceLength = Math.max(maximumSequenceLength, currentSequenceLength);
}
currentChar = str[i + 1];
currentSequenceLength = 1;
} else {
currentSequenceLength++;
}
}
return hash[maximumSequenceLength];
}
function getLongestConsecutiveChar(str) {
str = str.split('');
var maxSequenceLength = 0,
currentSequenceLength = 1,
currentChar = str[0],
maxChar, i;
for (i = 0; i < str.length; i++) {
if (currentChar !== str[i + 1]) {
if (maxSequenceLength < currentSequenceLength) {
maxChar = currentChar;
maxSequenceLength = currentSequenceLength;
}
currentChar = str[i + 1];
currentSequenceLength = 1;
} else {
currentSequenceLength++;
}
}
return maxChar;
}
Assumes a string input of something similar to "5 1 2 + 4 * + 3 -".
function reversePolishNotation(expr) {
expr = expr.split(" ");
var valStack = [], i = 0, args, re = /\d+/;
while (i < expr.length) {
if (re.test(expr[i]) === true) {
valStack.push(expr[i]);
} else {
args = valStack.splice(valStack.length - 2, 2);
valStack.push(eval(String(args[0] + expr[i] + args[1])));
}
i++;
}
return valStack[0];
}
Actually the above doesn't handle the case "aabbcba", but this should:
function smallestSubstring(set, str) {
var hash = {},
j = set.length,
result = [0, str.length],
start = 0, isFound = false;
for (var i = 0; i < set.length; i++) {
hash[set[i]] = 1;
}
str = str.split("");
for (i = 0; i < str.length; i++) {
if (hash.hasOwnProperty(str[i]) === true && --hash[str[i]] === 0) {
if (--j === 0) {
while (++hash[str[start++]] < 1) {
}
if ((i - start + 2) < (result[1] - result[0])) {
result = [start - 1, i + 1];
isFound = true;
}
j++;
}
}
}
if (isFound === true) {
return str.slice(result[0], result[1]).join("");
}
}
Repangelafiecke, Blockchain Developer at ASU
I am a Data processor at DGS VolMAX. I will also be a controller for the data I use for ...
- shysta July 04, 2016