faby v
faby v

Reputation: 19

wrong number of arguments and hash issues

I am trying to make a method that counts the number of times it uses a word from a dictionary and is returned as a hash. Here's my code now:

def substrings(words, dictionary)
  hash = {}
  substrings.downcase!
  dictionary.each do |substring|
    words.each do |word|
      if word.include? substring +=1
      end
    end
  end
 hash.to_s
end

dictionary = ["below", "down", "go", "going", "horn", "how", "howdy", "it", "i", "low", "own", "part", "partner", "sit"]
  words = "below"

substrings(words, dictionary)

And I get this error:

wrong number of arguments (given 0, expected 2)

I'm looking for something like this:

=> {"below"=>1, "low"=>1}

I have tried multiple things but it never gives me that hash. I either get an undefined method error or this:

=> ["below", ["below", "down", "go", "going", "horn", "how", "howdy", "it", "i", "low", "own", "part", "partner", "sit"]]   

Upvotes: 0

Views: 593

Answers (3)

marksiemers
marksiemers

Reputation: 641

You can use #reduce:

def substrings(sentence, dictionary)
  sentence = sentence.downcase
  dictionary.reduce(Hash.new(0)) do |counts,word| 
    counts[word] +=1 if sentence.include?(word.downcase)
    counts
  end
end

dictionary = ["below", "down", "go", "going", "horn", "how", "howdy", "it", "i", "low", "own", "part", "partner", "sit"]
sentence = "below"

substrings(sentence, dictionary) #=> {"below"=>1, "low"=>1}

Or #each:

def substrings(sentence, dictionary)
  sentence = sentence.downcase
  counts = Hash.new(0) # Makes the default value `0` instead of `nil`
  dictionary.each do |word| 
    if sentence.include?(word.downcase)
      counts[word] += 1
    end
  end
  counts
end

dictionary = ["below", "down", "go", "going", "horn", "how", "howdy", "it", "i", "low", "own", "part", "partner", "sit"]
sentence = "below"

substrings(sentence, dictionary) #=> {"below"=>1, "low"=>1}

Upvotes: 0

moveson
moveson

Reputation: 5213

This will produce the desired result, but I'm exchanging words in favor of word:

def substrings(word, dictionary)
  word = word.downcase
  dictionary.select { |entry| word.include?(entry.downcase) }
            .group_by(&:itself)
            .map { |k, v| [k, v.size] }.to_h
end

This results in:

>> dictionary = ["below", "down", "go", "going", "horn", "how", "howdy", "it", "i", "low", "own", "part", "partner", "sit"]
>> word = 'below'
>> substrings(word, dictionary)
=> {"below"=>1, "low"=>1}

And counts multiple copies of words, which although not explicitly stated, is presumably what you are after:

>> dictionary = ["below", "be", "below", "below", "low", "be", "pizza"]
>> word = 'below'
>> substrings(word, dictionary)
=> {"below"=>3, "be"=>2, "low"=>1}

Upvotes: 0

Peter Camilleri
Peter Camilleri

Reputation: 1912

Your error is caused by the line "substrings.downcase!" This is a recursive call to your substrings method which takes two arguments, and you are providing none. If this were not the case, you would still get an error, a stack overflow caused by the infinite recursion of this code.

Upvotes: 1

Related Questions