Skip to content

Instantly share code, notes, and snippets.

@xcobar

xcobar/ruby.rb Secret

Created May 17, 2019 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xcobar/fcdbcd49a26adcf574c2cf7a2a076852 to your computer and use it in GitHub Desktop.
Save xcobar/fcdbcd49a26adcf574c2cf7a2a076852 to your computer and use it in GitHub Desktop.
def find(*array)
set1 = ["cat", "dog"]
set2 = ["apple", "orange"]
if (array & set1).size.positive? || (array & set1).size.positive?
return 1
elsif (array & set1 & set2).size.positive?
return 2
end
end
find(["cat", "apple"]) #=> 2
find(["cat", "dog"]) #=> 1
find(["apple", "dog"]) #=> 2
find(["orange"]) #=> 1
@havenwood
Copy link

ANIMALS = %w[cat dog].freeze
FRUIT = %w[apple orange].freeze
CATAGORIES = [ANIMALS, FRUIT].freeze

def count_catagories(items, categories = CATAGORIES)
  categories.reduce 0 do |count, category|
    next count.next if items.any? { |item| category.include? item }
    count
  end
end

@havenwood
Copy link

  categories.sum do |category|
    next 1 if items.any? { |item| category.include? item }
    0
  end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment