Saturday 15 March 2014

javascript - Two arrays, where items in array x can be in array y but not vice versa, test all permutations -


A small application I wrote allows the user to add different objects in two arrays. Some arguments calculate a figure from the contents of each array.

Any objects in array X can be stored in array y, and can return again. Items in the array Y can never go related (unless they are transferred from the array X.)

Users can move these items in almost two lists using the simple JavaScript UI. To simplify things, I originally created a naive script:

  1. An item was moved from one to y.
  2. Used some reasoning using this 'probability'
  3. If the result was less than the first, leave y in y.
  4. If not, then X remains in X.
  5. Go to the next item in X and repeat.

    I knew it was ineffective. I have read around and I have been told that it is using bitware math to remember the possibilities or 'permutations', but at this stage I am struggling to get my head about this particular problem I am

    If anyone explains (pseudo-code is okay) what would be the best way to get the following: I would be very grateful.

    array x = [100,200,300,400,500] array y = [50,150,350,900]

    with these two arrays, for each value from X, push every combination of that value and X Take all the other values ​​in the array y. For each one I will perform some logic (i.e. the test result and store this sequence in an array (an object of two arrays representing X and Y), which I hope is very costly with these large arrays. I think that I am almost there, but lost in this last phase.

    Sorry for the long explanation, and thank you in advance!

    x :

      function power (x, y ), Use it {var r = [Y] []], // as a blank set / array for fallback l = 1; (var i = 0; i & lt; x.length; l = 1 & lt; ++ i) // ok, l just r [ii] length, but it looks good :) for (var j = 0; j & lt; l; j ++ ) {R.push (r [j]. Slice (0)); // copy r [j]. Push (x [ii]); } Return R; }   

    Usage:

      & gt; Power ([0,2], [5,6]) [[5,6,0,2], [5,6,2], [5,6,0], [5,6]]   

    I have been told that using bitworth mathematics should remember the possibilities or 'permutations', but at this level to get my head about this particular problem I am struggling for.

    This will be repeated to 2 n (for an array of length n), to determine whether any object should be included in the subset or not , Using a single bit. Example for an array [a, b]:

      binary included in i set ------------------------ ------ 00 00 {} 1 01 {b} 2 10 {a} 3 11 {a, b}   

    We can use for 31 arrays with arrays in JS (Which should be enough)

      function power (x, y) {var l = Math.pow (2, x.length), r = new array (l); (Var i = 0; i & lt; l; i ++) for {var all = y? Y.slice (0): []; For (var j = 0; j & lt; x.length; j ++) // If the right bit of J bit is set to i (i & amp; Math.pow (2, j)) / / Math.pow (2, j) === 1 & lt; & Lt; J sub push (x [j]); R [ii] = sub; } Return R; }    

No comments:

Post a Comment