Combinations of elements of multiple arrays - javascript

I have this type of data
var arr = [
["A", "AA"],
["B"],
["C", "CC", "CCC"]
];
I want to get combinations of all the elements within each array. for e.g.
A B
A B C
A B CC
A B CCC
A C
A CC
A CCC
...
AA B CCC
Note the sequence of the words are same, like this should not be one of the combination B A C.
I tried a couple of logics but can't get what I am looking for. I can obtain all permutations and combinations of all the words, but that's not what I am looking for.
Please suggest

You basically want to permute across multiple lists:
function permute(input)
{
var out = [];
(function permute_r(input, current) {
if (input.length === 0) {
out.push(current);
return;
}
var next = input.slice(1);
for (var i = 0, n = input[0].length; i != n; ++i) {
permute_r(next, current.concat([input[0][i]]));
}
}(input, []));
return out;
}
permute(arr);

The problem can be solved recursively. That is: for the first array, you have, for each of the elements, the result of the combinations formed with the two other arrays.
Something like this could work:
function arrayCombine ( array ) {
if (array.length > 1) {
var result = new Array();
//This combines all the arrays except the first
var otherCombs = arrayCombine ( array.slice(1) );
for ( var n = 0; n < array[0].length; n++ )
for ( var i = 0; i < otherCombs.length; i++ )
result.push ( array[0][n] + otherCombs[i] );
return result;
}
//If we have only one array, the result is the array itself, for it contains in itself all the combinations of one element that can be made
else return array[0];
}

Make an array of indices (idx), each element corresponding to each row. Initial value 0.
Start with index i = 0
Do what you do with the current combination.
Increment idx[i]. If it's less than the length of the row, go to 2
Set idx[i] to zero
Increment i. If it's greater than the number of rows, terminate algorithm, otherwise go to 2

Related

String Match challenge in Javascript

if anybody can point or just give a clue what I did wrong, would be very much appreciated. So the task is :
Given 2 strings, a and b, return the number of the positions where
they contain the same length 2 substring. So "xxcaazz" and "xxbaaz"
yields 3, since the "xx" "xx", "aa", and "az" substrings appear in the
same place in both strings.
function('xxcaazz', 'xxbaaz') should return 3
function('abc', 'abc') should return 2
function('abc', 'axc') should return 0
My code:
function stringMatch(a, b){
// convert both strings to arrays with split method
let arrA = a.split("")
let arrB = b.split("")
// create 2 empty arrays to feel in with symbol combinations
let arrOne = [];
let arrTwo = [];
// loop through the first array arrA and push elements to empty arrayOne
for ( let i = 0; i < arrA.length ; i++ ) {
arrOne.push(arrA[i]+arrA[i+1])
}
// loop through the first array arrB and push elements to empty arrayTwo
for ( let i = 0; i < arrB.length ; i++ ) {
arrTwo.push(arrB[i]+arrB[i+1])
}
// create a new array of the matching elements from arrOne and arrTwo
let newArray = arrOne.filter(value => arrTwo.includes(value))
// return the length 0f the newArray - that's supposed to be the answer
return newArray.length
}
Thanks for help!
On the last iteration of your loops, there won't be "next" character, arrB[i+1] will be undefined. The easiest way to solve that is to only loop until the second to last character, or until i < arrB.length - 1.
for ( let i = 0; i < arrB.length - 1; i++ ) {
arrTwo.push(arrB[i]+arrB[i+1])
}
e.g...
console.log(stringMatch('xxcaazz', 'xxbaaz')); //should return 3
console.log(stringMatch('abc', 'abc')); // should return 2
console.log(stringMatch('abc', 'axc')); //should return 0
function stringMatch(a, b){
// convert both strings to arrays with split method
let arrA = a.split("")
let arrB = b.split("")
// create 2 empty arrays to feel in with symbol combinations
let arrOne = [];
let arrTwo = [];
// loop through the first array arrA and push elements to empty arrayOne
for ( let i = 0; i < arrA.length -1 ; i++ ) {
arrOne.push(arrA[i]+arrA[i+1])
}
// loop through the first array arrB and push elements to empty arrayTwo
for ( let i = 0; i < arrB.length - 1; i++ ) {
arrTwo.push(arrB[i]+arrB[i+1])
}
// create a new array of the matching elements from arrOne and arrTwo
let newArray = arrOne.filter(value => arrTwo.includes(value))
// return the length 0f the newArray - that's supposed to be the answer
return newArray.length
}
As a bonus, here's my own solution...
console.log(stringMatch('xxcaazz', 'xxbaaz')); //should return 3
console.log(stringMatch('abc', 'abc')); // should return 2
console.log(stringMatch('abc', 'axc')); //should return 0
function stringMatch(a, b){
var matches = 0;
for(let i=a.length-1; i--;){
let s1 = a.substring(i, i+2);
let s2 = b.substring(i, i+2);
if(s1 == s2) matches++;
}
return matches;
}

Function to create array of arrays in javascript

Given an array with characters such as ["A","P","P","L","E","S","A","R","E"], I'm trying to create a function that will loop over the elements in the array, and create an array for each character, which will then be put into a master array. The master array will end up looking like[["A"],["P","P"],["L"],["E"],["S"],["A"],["R"],["E"] at the end, using a comparator function to check values (a,b) => a == b. Essentially, it needs to check each successive letter, and if they are the same, group into their own array within the master. The two A's should not be grouped together, since they aren't successive.
var arr = ["A","P","P","L","E"];
var master = [];
arr.sort(function(a,b){
for(var i = 0; i <= arr.length; i++){
compare each element to its successor. if successor is the same, create array of like elements ["A"],["C","C"],["B"] within final array
if(i + 1 == i){
master.push(i);
}
}
});
Just loop through the array and compare the last value to the current one.
DO NOT SORT -- that will change the order of your input array!
const coolFn = (arr) => {
return arr.reduce((rez, value, index) => {
if (index !== 0 && rez[rez.length - 1][0] === value) {
rez[rez.length - 1].push(value);
} else {
rez.push([value]);
}
return rez;
}, []);
}
const rez = coolFn('APPLES ARE NOT A BANANA PUDDING CUP'.split(''));
console.log(rez);
Can be accomplished pretty easily with Set to get unique items, reduce to turn transform and filter to find matching elements:
const arr = ["A","P","P","L","E"]
// get unique keys by expanding to a Set
const letters = [...new Set(arr)].reduce((p, c) => {
// add all matching elements from original array to aggregate
p.push(arr.filter(i => i === c))
return p;
}, []);
console.log(letters);
edit: sorry, I missed the requirement (hidden in a comment in your code) that you only add by comparing each element to its successor. My solution creates an array of each letter with its number of occurrences
You might do as follows;
var arr = ["A","P","P","L","E"],
result = arr.reduce((p,c) => {var fi = p.findIndex(a => a[0] === c);
fi === -1 ? p.push([c]) : p[fi].push(c);
return p;
},[]);
console.log(result);
As per the grouping only the sequential duplicates the following should do;
var arr = ["A","P","P","L","E","S","A","R","E"],
stickTheSame = a => a.reduce((p,c) => (p[p.length-1][0] === void 0 ||
p[p.length-1][0] === c ? p[p.length-1].push(c)
: p.push([c]),
p),[[]]);
console.log(JSON.stringify(stickTheSame(arr)));

Get all elements of array with same (highest) occurrence

I have an array like [1,4,3,1,6,5,1,4,4]
Here Highest element frequency is 3 ,I need to select all elements from array that have 3 frequency like [1,4] in above example.
I have tried with this
var count = {},array=[1,4,3,1,6,5,1,4,4],
value;
for (var i = 0; i < array.length; i++) {
value = array[i];
if (value in count) {
count[value]++;
} else {
count[value] = 1;
}
}
console.log(count);
this will output array element with their frequency , now i need all elements that have highest frequency.
I'd approach this problem as follows.
First, write down how you think the problem can be solved IN ENGLISH, or something close to English (or your native language of course!). Write down each step. Start off with a high-level version, such as:
Count the frequency of each element in the input.
Find the highest frequency.
and so on. At this point, it's important that you don't get bogged down in implementation details. Your solution should be applicable to almost any programming language.
Next flesh out each step by adding substeps. For instance, you might write:
Find the highest frequency.
a. Assume the highest frequency is zero.
b. Examine each frequency. If it is higher than the current highest frqeuency, make it the current highest frequency.
Test your algorithm by executing it manually in your head.
Next, convert what you have written about into what is sometimes called pseudo-code. It is at this point that our algorithm starts to look a little bit like a computer program, but is still easily human-readable. We may now use variables to represent things. For instance, we could write "max_freq ← cur_freq". We can refer to arrays, and write loops.
Finally, convert your pseudo-code into JS. If all goes well, it should work the first time around!
In recent years, a lot of people are jumping right into JavaScript, without any exposure to how to think about algorithms, even simple ones. They imagine that somehow they need to be able to, or will magically get to the point where they can, conjure up JS out of thin air, like someone speaking in tongues. In fact, the best programmers do not instantly start writing array.reduce when confronted with a problem; they always go through the process--even if only in their heads--of thinking about the approach to the problem, and this is an approach well worth learning from.
If you do not acquire this skill, you will spend the rest of your career posting to SO each time you can't bend your mind around a problem.
A proposal with Array.prototype.reduce() for a temporary object count, Object.keys() for getting the keys of the temporary object, a Array.prototype.sort() method for ordering the count results and Array.prototype.filter() for getting only the top values with the most count.
Edit: Kudos #Xotic750, now the original values are returned.
var array = [1, 4, 3, 1, 6, 5, 1, 4, 4],
result = function () {
var temp = array.reduce(function (r, a, i) {
r[a] = r[a] || { count: 0, value: a };
r[a].count++;
return r;
}, {});
return Object.keys(temp).sort(function (a, b) {
return temp[b].count - temp[a].count;
}).filter(function (a, _, aa) {
return temp[aa[0]].count === temp[a].count;
}).map(function (a) {
return temp[a].value;
});
}();
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
Bonus with a different attempt
var array = [1, 4, 3, 1, 6, 5, 1, 4, 4],
result = array.reduce(function (r, a) {
r.some(function (b, i) {
var p = b.indexOf(a);
if (~p) {
b.splice(p, 1);
r[i + 1] = r[i + 1] || [];
r[i + 1].push(a);
return true;
}
}) || (
r[1] = r[1] || [],
r[1].push(a)
);
return r;
}, []).pop();
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
you can try this
var input = [1,4,3,1,6,5,1,4,4];
var output = {};
for ( var counter = 0; counter < input.length; counter++ )
{
if ( !output[ input[ counter ] ] )
{
output[ input[ counter ] ] = 0;
}
output[ input[ counter ] ]++;
}
var outputArr = [];
for (var key in output)
{
outputArr.push([key, output[key]])
}
outputArr = outputArr.sort(function(a, b) {return b[1] - a[1]})
now initial values of outputArr are the ones with highest frequency
Here is the fiddle
Check this updated fiddle (this will give the output you want)
var input = [1,4,3,1,6,5,1,4,4];
var output = {}; // this object holds the frequency of each value
for ( var counter = 0; counter < input.length; counter++ )
{
if ( !output[ input[ counter ] ] )
{
output[ input[ counter ] ] = 0; //initialized to 0 if value doesn't exists
}
output[ input[ counter ] ]++; //increment the value with each occurence
}
var outputArr = [];
var maxValue = 0;
for (var key in output)
{
if ( output[key] > maxValue )
{
maxValue = output[key]; //find out the max value
}
outputArr.push([key, output[key]])
}
var finalArr = []; //this array holds only those keys whose value is same as the highest value
for ( var counter = 0; counter < outputArr.length; counter++ )
{
if ( outputArr[ counter ][ 1 ] == maxValue )
{
finalArr.push( outputArr[ counter ][ 0 ] )
}
}
console.log( finalArr );
I would do something like this. It's not tested, but it's commented for helping you to understand my approach.
// Declare your array
var initial_array = [1,4,3,1,6,5,1,4,4];
// Declare an auxiliar counter
var counter = {};
// Loop over the array
initial_array.forEach(function(item){
// If the elements is already in counter, we increment the repetition counter.
if counter.hasOwnProperty(item){
counter[item] += 1;
// If the element is not in counter, we set the repetitions to one
}else{
counter[item] = 1;
}
});
// counter = {1 : 3, 4 : 3, 3 : 1, 6 : 1, 5 : 1}
// We move the object keys to an array (sorting it's more easy this way)
var sortable = [];
for (var element in counter)
sortable.push([element, counter[element]]);
// sortable = [ [1,3], [4,3], [3,1], [6,1], [5,1] ]
// Sort the list
sortable.sort(function(a, b) {return a[1] - b[1]})
// sortable = [ [1,3], [4,3], [3,1], [6,1], [5,1] ] sorted, in this case both are equals
// The elements in the firsts positions are the elements that you are looking for
// This auxiliar variable will help you to decide the biggest frequency (not the elements with it)
higgest = 0;
// Here you will append the results
results = [];
// You loop over the sorted list starting for the elements with more frequency
sortable.forEach(function(item){
// this condition works because we have sorted the list previously.
if(item[1] >= higgest){
higgest = item[1];
results.push(item[0]);
}
});
I'm very much with what #torazaburo had to say.
I'm also becoming a fan of ES6 as it creeps more and more into my daily browser. So, here is a solution using ES6 that is working in my browser now.
The shims are loaded to fix browser browser bugs and deficiencies, which is recommended in all environments.
'use strict';
// Your array of values.
const array = [1, 4, 3, 1, 6, 5, 1, 4, 4];
// An ES6 Map, for counting the frequencies of your values.
// Capable of distinguishing all unique values except `+0` and `-0`
// i.e. SameValueZero (see ES6 specification for explanation)
const frequencies = new Map();
// Loop through all the `values` of `array`
for (let item of array) {
// If item exists in frequencies increment the count or set the count to `1`
frequencies.set(item, frequencies.has(item) ? frequencies.get(item) + 1 : 1);
}
// Array to group the frequencies into list of `values`
const groups = [];
// Loop through the frequencies
for (let item of frequencies) {
// The `key` of the `entries` iterator is the value
const value = item[0];
// The `value` of the `entries` iterator is the frequency
const frequency = item[1];
// If the group exists then append the `value`,
// otherwise add a new group containing `value`
if (groups[frequency]) {
groups[frequency].push(value);
} else {
groups[frequency] = [value];
}
}
// The most frequent values are the last item of `groups`
const mostFrequent = groups.pop();
document.getElementById('out').textContent = JSON.stringify(mostFrequent);
console.log(mostFrequent);
<script src="https://cdnjs.cloudflare.com/ajax/libs/es5-shim/4.4.1/es5-shim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/json3/3.3.2/json3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.34.0/es6-shim.js"></script>
<pre id="out"></pre>
you can do like this to find count occurrence each number
var array = [1, 4, 3, 1, 6, 5, 1, 4, 4];
var frequency = array.reduce(function(sum, num) {
if (sum[num]) {
sum[num] = sum[num] + 1;
} else {
sum[num] = 1;
}
return sum;
}, {});
console.log(frequency)
<script src="https://getfirebug.com/firebug-lite-debug.js"></script>

Finding all subsets of a set, Powerset, recursively by cloning n-1 and adding n to the clones

Having the set {a,b,c} I want find all the subsets in recursive manner. I have already solved this problem using bitmasking but I want to understand the way that a person stated in this youtube video here
There are other stackoverflow threads about this problem but I have not found any that are solving the way she states in the video, she says,
"take the subsets of a and b, clone them and then add c to all the clones"
I am having trouble picturing the "simple" recursive method that would accomplish this. Is the recursive method, once exhausted, have all the subsets of A,B and the clones of A,B (duplicates at this point) and then propagates back up adding C to only the clones?
In other words, I start with a for loop on the set, I call my recursive function, I then do a for loop of n-1 and call my recursive method in that for loop, I cannot see how I can get C to be added to already existing subset clones in an array that is being built with recursion.
function SubsetBuilder(set) {
this.set = set;
}
SubsetBuilder.prototype.getSubsetsRecursive = function () {
//Set = {a,b,c}
//take the subsets of a and b, clone them and then add c to all the clones
//subsets of {a,b}=
//{}
//{a}
//{b}
//{a,b}
var n = this.set.length;
var result = [];
var recurseForSubsets = function (prefix, index) {
for (var i = index; i < n -1; i ++) {
result.push(prefix + this.set[i]);
recurseForSubsets(prefix + this.set[i], i + 1);
}
}
for (var j = 0; j < n; j++) {
recurseForSubsets("", j);
}
return result;
}
SubsetBuilder.prototype.printSubsets = function () {
var self = this;
if (!self.set)
return;
var n = this.set.length;
for (var i = 0; i < (1 << n) ; i++) {
var subset = [];
for (var j = 0; j < n; j++) {
if (((i >> j) & 1) === 1) { // bit j is on
subset.push(this.set[j]);
}
}
console.log(subset);
}
}
var set = ['a', 'b', 'c'];
var obj = new SubsetBuilder(set);
//obj.printSubsets();
console.log(obj.getSubsetsRecursive());
I gave this a try and came up with
function getSubsets(inp) {
if (inp.length == 1) {
// return the single item set plus the empty set
return [inp, []];
} else {
var e = inp.pop();
var s = getSubsets(inp);
// duplicate the elements of s into s1 without creating references.
// this might not be the best technique
var s1 = s.concat(JSON.parse(JSON.stringify(s)));
// add e to the second group of duplicates
for (var i=s.length; i < s1.length; i++) {
s1[i].push(e);
}
return s1;
}
}
var set = ['a', 'b', 'c'];
var list = getSubsets(set);
console.log(list);
// result
// [["a"], [], ["a", "b"], ["b"], ["a", "c"], ["c"], ["a", "b", "c"], ["b", "c"]]
The lady in the video said that all subsets of {a,b,c} can be formed from taking all the subsets of {a,b} and appending c to each one. Not entirely accurate (a valid subset of {a,b,c} does not have to include c), but a starting place for the algorithm. I changed the rule to all subsets of {a,b,c} can be formed from taking two copies of the subsets of {a,b} and appending c to each element of the second copy.
I think I could get rid of or simplify the if, because essentially the second block of code does the same as the first, so it's not ideal.
To me it makes sense that the algorithm runs in O(2^n) because the results vary in the same way (3 elements in the input array = 2^3 elements in the output array) - you might have to forgive my use of JSON to assume that complexity though. I'd find a better way to deep clone the array, even so, that might add more complexity.

javascript permutation generator with permutation length parameter

I've seen a few generators out there but they all make a squared matrix. For example, you give it a list of three items and it'll assume the output of the length is also three. However, I'd like to specify the items and the length.
Sound like an easy problem can't believe there isn't a library available for it. Would like to avoid writing this myself if there's a tested library out there. Any suggestions would be great.
Example of what i've found
var list = 'abc';
perms = permutations(list);
//you cannot define the length
Example
var list = 'abc';
var length = 3;
perms = permutations(list,length);
console.log(perms);
/* output
a,a,a
a,b,c
a,b,a
a,c,a
c,a,a
...
*/
I would like to be able to change length and should create permutations accordingly
length = 2
a,a
a,b
b,b
b,a
length = 4
a,a,a,a
a,a,a,b
....
You can imagine the length as representing the number of slots. Each slot has N possibilities, given that N is the number of elements in your initial list. So given three values [1,2,3], you will have a total of 3 x 3 x 3 = 27 permutations.
Here's my attempt. Comments included!
var list = [1,2,3];
var getPermutations = function(list, maxLen) {
// Copy initial values as arrays
var perm = list.map(function(val) {
return [val];
});
// Our permutation generator
var generate = function(perm, maxLen, currLen) {
// Reached desired length
if (currLen === maxLen) {
return perm;
}
// For each existing permutation
for (var i = 0, len = perm.length; i < len; i++) {
var currPerm = perm.shift();
// Create new permutation
for (var k = 0; k < list.length; k++) {
perm.push(currPerm.concat(list[k]));
}
}
// Recurse
return generate(perm, maxLen, currLen + 1);
};
// Start with size 1 because of initial values
return generate(perm, maxLen, 1);
};
var res = getPermutations(list, 3);
console.log(res);
console.log(res.length); // 27
fiddle
If you're looking for an answer based on performance, you can use the length of the array as a numerical base, and access the elements in the array based on this base, essentially replacing actual values from the base with the values in your array, and accessing each of the values in order, using a counter:
const getCombos = (arr, len) => {
const base = arr.length
const counter = Array(len).fill(base === 1 ? arr[0] : 0)
if (base === 1) return [counter]
const combos = []
const increment = i => {
if (counter[i] === base - 1) {
counter[i] = 0
increment(i - 1)
} else {
counter[i]++
}
}
for (let i = base ** len; i--;) {
const combo = []
for (let j = 0; j < counter.length; j++) {
combo.push(arr[counter[j]])
}
combos.push(combo)
increment(counter.length - 1)
}
return combos
}
const combos = getCombos([1, 2, 3], 3)
console.log(combos)
For smaller use cases, like the example above, performance shouldn't be an issue, but if you were to increase the size of the given array from 3 to 10, and the length from 3 to 5, you have already moved from 27 (33) combinations to 100,000 (105), you can see the performance difference here:
I wrote a little library that uses generators to give you permutations with custom items and number of elements. https://github.com/acarl005/generatorics
const G = require('generatorics')
for (let perm of G.permutation(['a', 'b', 'c'], 2)) {
console.log(perm);
}
// [ 'a', 'b' ]
// [ 'a', 'c' ]
// [ 'b', 'a' ]
// [ 'b', 'c' ]
// [ 'c', 'a' ]
// [ 'c', 'b' ]

Categories

Resources