array dynamic intersection and union

Discussion in 'Ruby' started by Jamal Soueidan, Jan 14, 2011.

  1. Hello,

    I was wondering If could intersect and union array dynamic in my code.

    Let us say I have these conditions.

    array = A & B | (C & D ) & E


    Is their any ways of doing these stuff dynamic if all the objects was
    arrays.

    A = [1,2,3,4,5]


    Thanks for any help.

    --
    Posted via http://www.ruby-forum.com/.
    Jamal Soueidan, Jan 14, 2011
    #1
    1. Advertising

  2. On Jan 14, 2011, at 4:07 PM, Jamal Soueidan wrote:

    > Hello,
    >
    > I was wondering If could intersect and union array dynamic in my code.
    >
    > Let us say I have these conditions.
    >
    > array = A & B | (C & D ) & E
    >
    >
    > Is their any ways of doing these stuff dynamic if all the objects was
    > arrays.
    >
    > A = [1,2,3,4,5]
    >
    >
    > Thanks for any help.
    >


    Why don't you try a few examples and find out? If you still have
    questions, then at least you can point to actual code to clarify where
    you need help.

    -Rob


    Rob Biedenharn
    http://AgileConsultingLLC.com/
    http://GaslightSoftware.com/
    Rob Biedenharn, Jan 14, 2011
    #2
    1. Advertising

  3. Hello Rob,

    Sorry I did not explain myself first time.

    But I let us say I have these hashes.

    rule = {:array => [1,2,3,4], :condition => "AND"}
    rule2 = {:array => [1,2,3,4], :condition => "OR"}
    rule3 = {:array => [3,4]}

    How can I intersection and union based on the conditions in the hash
    through them all?

    So it end up like this

    array = rule[:array] & rule2[:array] | rule3[:array]

    --
    Posted via http://www.ruby-forum.com/.
    Jamal Soueidan, Jan 14, 2011
    #3
  4. On Jan 14, 2011, at 5:39 PM, Jamal Soueidan wrote:

    > Hello Rob,
    >
    > Sorry I did not explain myself first time.
    >
    > But I let us say I have these hashes.
    >
    > rule = {:array => [1,2,3,4], :condition => "AND"}
    > rule2 = {:array => [1,2,3,4], :condition => "OR"}
    > rule3 = {:array => [3,4]}
    >
    > How can I intersection and union based on the conditions in the hash
    > through them all?
    >
    > So it end up like this
    >
    > array = rule[:array] & rule2[:array] | rule3[:array]


    Let me rephrase that because you still aren't giving enough
    information to really know what you're trying to do.

    Given an array of hashes:
    input = [ {:array => [1,2,3,4], :condition => "AND"},
    {:array => [2,4,6,8], :condition => "OR"},
    {:array => [1,3]},
    ]
    array = input[0][:array] & input[1][:array] | input[2][:array]
    => [2, 4, 1, 3]


    Well, if you apply the operations serially, you will actually get
    something like:

    array = ((input[0][:array] & input[1][:array]) | input[2][:array])

    Since & is higher precendence than |, the answer is the same, but if
    you change the order:

    array = input[0][:array] | input[1][:array] & input[2][:array]
    => [1, 2, 3, 4]

    is not the same as:

    array = ((input[0][:array] | input[1][:array]) & input[2][:array])
    => [1, 3]


    def combine(arrays)
    result, op = [], "OR"
    arrays.each do |hsh|
    case op
    when "OR"
    result |= hsh[:array]
    when "AND"
    result &= hsh[:array]
    end
    op = hsh[:condition]
    end
    result
    end

    combine(input)
    #=> [2, 4, 1, 3]

    -Rob

    Rob Biedenharn
    http://AgileConsultingLLC.com/
    http://GaslightSoftware.com/
    Rob Biedenharn, Jan 15, 2011
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?ISO-8859-1?Q?Mickel_Gr=F6nroos?=

    Standard ways to get union, intersection, difference of lists?

    =?ISO-8859-1?Q?Mickel_Gr=F6nroos?=, Jun 26, 2003, in forum: Python
    Replies:
    2
    Views:
    1,832
    Erik Max Francis
    Jun 26, 2003
  2. Gerrit Holl
    Replies:
    0
    Views:
    857
    Gerrit Holl
    Jun 26, 2003
  3. =?ISO-8859-1?Q?Mickel_Gr=F6nroos?=

    Re: Standard ways to get union, intersection, difference of lists?

    =?ISO-8859-1?Q?Mickel_Gr=F6nroos?=, Jun 26, 2003, in forum: Python
    Replies:
    3
    Views:
    1,430
    Mike C. Fletcher
    Jun 26, 2003
  4. inetquestion
    Replies:
    3
    Views:
    226
    John W. Krahn
    Feb 3, 2008
  5. Peng Yu
    Replies:
    1
    Views:
    126
    J├╝rgen Exner
    Dec 31, 2009
Loading...

Share This Page