Determining whether one array contains the contents of another array in ruby

前端 未结 11 1017
清歌不尽
清歌不尽 2021-02-09 03:06

In ruby, how do I test that one array not only has the elements of another array, but contain them in that particular order?

correct_combination = [1, 2, 3, 4, 5         


        
相关标签:
11条回答
  • 2021-02-09 03:42

    I think it can be done simply.

    class Array
      def contain? other; (self & other) == other end
    end
    
    correct_combination = [1, 2, 3, 4, 5]
    [1, 5, 8, 2, 3, 4, 5].contain?(correct_combination) # => false
    [8, 10, 1, 2, 3, 4, 5, 9].contain?(correct_combination) # => true
    
    0 讨论(0)
  • 2021-02-09 03:43

    A really quick way to do this is to simply subtract one array from the other and test for an empty array.

    correct_combination = [1, 2, 3, 4, 5]
    yep = [8, 10, 1, 2, 3, 4, 5, 9]
    nope = [1, 8, 2, 3, 4]
    if correct_combination - yep == []
      puts "yep has all the values"
    end
    if correct_combination - nope == []
      puts "nope has all the values"
    end
    

    This approach does not care about position so delete away!

    Sorry... I missed the point to the question as well. Didn't realize you were looking for order of precedence. I came across this when looking for a solution to evaluating if one large array contained all the entries of another large array. The .all?/include? approach takes a really long time to complete. Good luck!

    0 讨论(0)
  • 2021-02-09 03:43

    You could simply compare the arrays as strings:

    correct_combination = [1, 2, 3, 4, 5]
    yep = [8, 10, 1, 2, 3, 4, 5, 9]
    nope = [1, 5, 8, 2, 3, 4, 5]
    if yep.to_s.include?(correct_combination.to_s)
      puts "yep"
    end
    if nope.to_s.include?(correct_combination.to_s)
      puts "nope"
    end
    
    0 讨论(0)
  • 2021-02-09 03:45

    If you want to ignore the order, (as I did when I came across this post), you could use Array.sort and <=> http://ruby-doc.org/core-1.8.7/classes/Array.html#M000316

    a = [1, 2, 3, 4, 5]
    b = [2, 1, 5, 4, 3]
    a.sort <=> b.sort
    

    You then need to check the output value equals 0.

    0 讨论(0)
  • 2021-02-09 03:48

    Maybe <=> is what you are looking for.

    Comparison—Returns an integer (-1, 0, or +1) if this array is less than, equal to, or greater than other_array

    a = [1, 2, 3, 4, 5]
    b = [1, 5, 8, 2, 3, 4, 5]
    c = [8, 10, 1, 2, 3, 4, 5, 9]
    
    puts a <=> b # => -1
    puts a <=> c # => -1
    puts a <=> a # => 0
    

    Update: nevermind, just noted it doesn't care about position.

    puts a <=> a.reverse # => -1
    
    0 讨论(0)
  • 2021-02-09 03:49

    Not exactly the best solution possible, but at least it's brief

    (',' + [1, 5, 8, 2, 3, 4, 5].join(',') + ',').include?(',' + correct_combination.join(',') + ',')
    

    The best solution possible would be to employ one of string searching algorithms on array but you would have to code it yourself, I don't think there's standard solution.

    0 讨论(0)
提交回复
热议问题