197

I want to create different methods for a class called Multiset.

I have all the required methods, but I'm unsure of how to write intersection, union, and subset methods.

For intersection and union, my code starts like this:

def intersect(var)
  x = Multiset.new
end

Here is an example:

X = [1, 1, 2, 4]
Y = [1, 2, 2, 2]

then the intersection of X and Y is [1, 2].

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
user487743
  • 2,145
  • 4
  • 16
  • 11

3 Answers3

361

I assume X and Y are arrays? If so, there's a very simple way to do this:

x = [1, 1, 2, 4]
y = [1, 2, 2, 2]

# intersection
x & y            # => [1, 2]

# union
x | y            # => [1, 2, 4]

# difference
x - y            # => [4]

Source

Jon Gauthier
  • 25,202
  • 6
  • 63
  • 69
  • 17
    In other words, just do `Multiset < Array`. – sawa Apr 15 '11 at 15:32
  • What if you have x = [1,1,2,4] y = [1,2,2,2] z = [4] How can you get it to give you any intersections between with sets instead of the intersection of all sets? So instead of it giving you [], it gives you [1,2,4]? – mharris7190 Jul 03 '14 at 16:07
  • 1
    @mharris7190 you can take the union of all those intersections: `(x & y) | (y & z) | (x & z)` – xavdid Nov 10 '15 at 19:37
  • 2
    Don't forget there is also `&=`, `|=`, and `-=` if you also want to immediately store the value like I did! :) – Pysis Apr 17 '17 at 15:47
  • 2
    Exactly what I thought @sawa. Why is the OP creating this class in the first place? It doesn't do anything that Array doesn't already do from Ruby's Standard Lib. – danielricecodes Aug 30 '17 at 17:18
  • That's funny sawa – Wylliam Judd Feb 15 '18 at 18:19
163

Utilizing the fact that you can do set operations on arrays by doing &(intersection), -(difference), and |(union).

Obviously I didn't implement the MultiSet to spec, but this should get you started:

class MultiSet
  attr_accessor :set
  def initialize(set)
    @set = set
  end
  # intersection
  def &(other)
    @set & other.set
  end
  # difference
  def -(other)
    @set - other.set
  end
  # union
  def |(other)
    @set | other.set
  end
end

x = MultiSet.new([1,1,2,2,3,4,5,6])
y = MultiSet.new([1,3,5,6])

p x - y # [2,2,4]
p x & y # [1,3,5,6]
p x | y # [1,2,3,4,5,6]
dinjas
  • 2,115
  • 18
  • 23
Mike Lewis
  • 63,433
  • 20
  • 141
  • 111
  • 9
    2 big crimes in this answer: (1) The word `set` as variable name of a plain array; (2) Replicating everything that `Array` already does. If the OP wants to add functionality to the `Array` class with some additional methods, you should simply do: `class MultiSet < Array def inclusion?(other) Set.new(self).subset?(Set.new(other)) end end` – Rahul Murmuria Sep 11 '18 at 01:08
  • 1
    Agreed... this is probably the most useless class I've seen in my life... but I realize that's not really your fault. – mpowered Nov 20 '18 at 17:58
17

If Multiset extends from the Array class

x = [1, 1, 2, 4, 7]
y = [1, 2, 2, 2]
z = [1, 1, 3, 7]

UNION

x.union(y)           # => [1, 2, 4, 7]      (ONLY IN RUBY 2.6)
x.union(y, z)        # => [1, 2, 4, 7, 3]   (ONLY IN RUBY 2.6)
x | y                # => [1, 2, 4, 7]

DIFFERENCE

x.difference(y)      # => [4, 7] (ONLY IN RUBY 2.6)
x.difference(y, z)   # => [4] (ONLY IN RUBY 2.6)
x - y                # => [4, 7]

INTERSECTION

x.intersection(y)    # => [1, 2] (ONLY IN RUBY 2.7)
x & y                # => [1, 2]

For more info about the new methods in Ruby 2.6, you can check this blog post about its new features