Created
September 23, 2016 17:12
-
-
Save anonymous/c1269931fa25381bde5fcb385317a43b to your computer and use it in GitHub Desktop.
JS Bin [add your bin description] // source https://jsbin.com/tulemun
This file contains hidden or 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> | |
<meta name="description" content="[add your bin description]"> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
/** | |
This is a solution for a generalization of a problem i was asked in an intereview. | |
The first question was for a given array of strings to print all possible pairs where order of pairs is not important. | |
for example, for a given array ["a","b","c"] the output shoult be : | |
"a","b", | |
"a","c", | |
"b","c" | |
I solve this queestion in atrivial way but two nested loops. | |
The the interviewer asked me how would i solve this for a more general question | |
so we have N combinaiton instead of 2 in the given original question. | |
I didn't manage to solve that in the interview but i picked my brain and 3 hours of the interview | |
i scatch the following solution on a piece of paper in the airport while waiting for a relative. | |
So now that i solved it, i can finally give my mind some rest. | |
i hope the solution is readable and understandable, although it is a bit complex. | |
*/ | |
"use strict"; | |
var arr = ["a", "b", "c", "d", "e"]; | |
//this function is pretty simple, it just console log all the | |
//current combination based on an array of indexers which tells me where we are. | |
function pairs(arr, n) { | |
//initializing the indexers array. | |
var indexers = []; | |
for (var i = 0; i < n; i++) { | |
indexers.push(i); | |
} | |
var stop = false; | |
while (!stop) { | |
console.log(indexers.map(function (index) { | |
return arr[index]; | |
})); | |
stop = moveIndexers(indexers, arr.length); | |
} | |
} | |
//this is the main function which more the indexers. | |
//it returns true in case there is no more options to run over. | |
function moveIndexers(indexers, len) { | |
var lastIndex = indexers.length - 1; | |
var current = lastIndex; | |
while (current >= 0) { | |
if (indexers[current] < len - 1 - (lastIndex - current)) { | |
for (var i = current; i <= lastIndex; i++) { | |
if (i === current) { | |
indexers[i]++; | |
} else { | |
indexers[i] = indexers[i - 1] + 1; | |
} | |
} | |
return; | |
} | |
current--; | |
if (current < 0) return true; | |
} | |
} | |
pairs(arr, 2); | |
pairs(arr, 3); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">/** | |
This is a solution for a generalization of a problem i was asked in an intereview. | |
The first question was for a given array of strings to print all possible pairs where order of pairs is not important. | |
for example, for a given array ["a","b","c"] the output shoult be : | |
"a","b", | |
"a","c", | |
"b","c" | |
I solve this queestion in atrivial way but two nested loops. | |
The the interviewer asked me how would i solve this for a more general question | |
so we have N combinaiton instead of 2 in the given original question. | |
I didn't manage to solve that in the interview but i picked my brain and 3 hours of the interview | |
i scatch the following solution on a piece of paper in the airport while waiting for a relative. | |
So now that i solved it, i can finally give my mind some rest. | |
i hope the solution is readable and understandable, although it is a bit complex. | |
*/ | |
var arr = ["a","b","c","d","e"]; | |
//this function is pretty simple, it just console log all the | |
//current combination based on an array of indexers which tells me where we are. | |
function pairs(arr, n){ | |
//initializing the indexers array. | |
var indexers = []; | |
for( var i=0; i<n; i++ ){ | |
indexers.push(i); | |
} | |
var stop = false; | |
while( !stop ){ | |
console.log(indexers.map((index) => arr[index])); | |
stop = moveIndexers(indexers, arr.length); | |
} | |
} | |
//this is the main function which more the indexers. | |
//it returns true in case there is no more options to run over. | |
function moveIndexers(indexers, len){ | |
var lastIndex = indexers.length-1; | |
var current = lastIndex; | |
while( current >= 0 ){ | |
if( indexers[current] < (len-1)-(lastIndex-current)){ | |
for( var i=current; i<=lastIndex; i++ ){ | |
if( i=== current ){ | |
indexers[i]++; | |
} | |
else{ | |
indexers[i] = indexers[i-1]+1; | |
} | |
} | |
return; | |
} | |
current--; | |
if( current < 0 ) return true; | |
} | |
} | |
pairs(arr,2) | |
pairs(arr,3);</script></body> | |
</html> |
This file contains hidden or 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 is a solution for a generalization of a problem i was asked in an intereview. | |
The first question was for a given array of strings to print all possible pairs where order of pairs is not important. | |
for example, for a given array ["a","b","c"] the output shoult be : | |
"a","b", | |
"a","c", | |
"b","c" | |
I solve this queestion in atrivial way but two nested loops. | |
The the interviewer asked me how would i solve this for a more general question | |
so we have N combinaiton instead of 2 in the given original question. | |
I didn't manage to solve that in the interview but i picked my brain and 3 hours of the interview | |
i scatch the following solution on a piece of paper in the airport while waiting for a relative. | |
So now that i solved it, i can finally give my mind some rest. | |
i hope the solution is readable and understandable, although it is a bit complex. | |
*/ | |
"use strict"; | |
var arr = ["a", "b", "c", "d", "e"]; | |
//this function is pretty simple, it just console log all the | |
//current combination based on an array of indexers which tells me where we are. | |
function pairs(arr, n) { | |
//initializing the indexers array. | |
var indexers = []; | |
for (var i = 0; i < n; i++) { | |
indexers.push(i); | |
} | |
var stop = false; | |
while (!stop) { | |
console.log(indexers.map(function (index) { | |
return arr[index]; | |
})); | |
stop = moveIndexers(indexers, arr.length); | |
} | |
} | |
//this is the main function which more the indexers. | |
//it returns true in case there is no more options to run over. | |
function moveIndexers(indexers, len) { | |
var lastIndex = indexers.length - 1; | |
var current = lastIndex; | |
while (current >= 0) { | |
if (indexers[current] < len - 1 - (lastIndex - current)) { | |
for (var i = current; i <= lastIndex; i++) { | |
if (i === current) { | |
indexers[i]++; | |
} else { | |
indexers[i] = indexers[i - 1] + 1; | |
} | |
} | |
return; | |
} | |
current--; | |
if (current < 0) return true; | |
} | |
} | |
pairs(arr, 2); | |
pairs(arr, 3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment