-1

For example two arrays:

var names = ['Tom','Jerry','Sam'];
var hobbies = ['Eat','Sleep','Laugh'];

Is there a function can construct the two arrays as maps like:

{'Tome':'Eat','Jerry':'Sleep','Sam':'Laugh'}
{'Tome':'Sleep','Jerry':'Eat','Sam':'Laugh'}
{'Tome':'Laugh','Jerry':'Eat','Sam':'Laugh'}

and other 3 more... Totally as given the two arrays the returned maps number should be A33 = 6. By javascript or python any can do it. Any ideas?


After searching from web, this is an assignment problem and the way to resolve it is called Hungarian Method. Now I am looking for a Hungarian Algorithm implementation by javascript or python.
ShuSon
  • 407
  • 5
  • 10
  • 1
    You could use one more tag and specify another language! – devnull May 29 '14 at 09:08
  • It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (console output, stack traces, compiler errors - whatever is applicable). The more detail you provide, the more answers you are likely to receive. – Martijn Pieters May 29 '14 at 09:11
  • Thanks both above, I am new into programming with bad mathematics and logic ability, also bad English expression. I am learning the code provided by Bakuriu. It helps me a lot. – ShuSon May 29 '14 at 09:35

3 Answers3

1

well, this is not permutation, but product of 2 vectors. In Python Itertools, there's a function product can deal with this.

import itertools as itls
names = ['Tom','Jerry','Sam']
hobbies = ['Eat','Sleep','Laugh']
print list(itls.product(names, hobbies))

the result is: [('Tom', 'Eat'), ('Tom', 'Sleep'), ('Tom', 'Laugh'), ('Jerry', 'Eat'), ('Jerry', 'Sleep'), ('Jerry', 'Laugh'), ('Sam', 'Eat'), ('Sam', 'Sleep'), ('Sam', 'Laugh')]

Actually, product of 2 vectors is to do some 'op' for each one of vector1 with each one of vector2, in this case, the 'op' is to make a tuple. The product operation is equiverlent to:

for i = 0; i<length(vector1); ++i
    for j = 0; j<length(vector2); ++j
        vector1[i] 'op' vector2[j];
dguan
  • 1,023
  • 1
  • 9
  • 21
0

You want to map the names to every possible permutation of the second array:

from itertools import permutations

hashmaps = [dict(zip(names, perm)) for perm in permutations(hobbies)]

Note that permutations returns a list that is N! in length, which is going to be huge even with very small lengths.

Bakuriu
  • 98,325
  • 22
  • 197
  • 231
0

I got an answer from this link Permutations in JavaScript?.

After that the method can be done like this:

function permute(input) {
var permArr = [],
usedChars = [];
function main(){
    var i, ch;
    for (i = 0; i < input.length; i++) {
        ch = input.splice(i, 1)[0];
        usedChars.push(ch);
        if (input.length == 0) {
            permArr.push(usedChars.slice());
        }
        main();
        input.splice(i, 0, ch);
        usedChars.pop();
    }
    return permArr;
}
return main();

}

function mapFromArrays(source,target){
var targets = permute(target);
var returns = [];

for(var t in targets){
    var map = {};
    for(var i=0;i<source.length;i++){
        map[source[i]] = targets[t][i];
    }
    returns.push(map);
}

return returns;

}

Community
  • 1
  • 1
ShuSon
  • 407
  • 5
  • 10