access the indexes of a ruby hash

1

I am doing a program to eliminate surpluses from an array, if we have more elements than maxOcurrences we omit them.

For example

  

[20,37,20,21] = > [20,37,21]

My problem in particular is to access the indexes of a hash, this is my code:

def delete_nth(elements,maxOcurrences)
  if maxOcurrences < 1
        return []
  end
    newArray = []
    aparisons = {}
    elements.each do |element|
        occurrences = aparisons[String(element)]
        puts "a" << String(occurrences)
        if occurrences.nil?
            newArray.push(element)
            aparisons["element"]=1
            puts aparisons
        else
            puts "e" << String(occurrences)
            if occurrences < maxOcurrences
                puts "i" << String(occurrences)
                newArray.push(element)
                aparisons["element"]=occurrences + 1
                puts "j"<<aparisons
            end
        end
    end
    return newArray
end

Entry

  

delete_nth ([20,37,20,21], 1)

Exit

  

a   {"element" = > 1}   to   {"element" = > 1}   to   {"element" = > 1}   to   {"element" = > 1}

How can I correct it? At the moment I try to access the indexes by aparisons[String(element)]

    
asked by Ruslan López 06.04.2018 в 03:50
source

1 answer

0

To find the key of a hash (the hash do not have indices) you should simply use the [] method with the name of the key (either a < em> string , a symbol or a number); for example:

nuevo_hash = { "uno" => "Valor 1", "dos" => "Valor 2" }

nuevo_hash["uno"]
#=> "Valor 1"

If you search for the name of a key that does not exist in the hash it will return nil (or, if set when creating the hash , a value by default).

The code above is just happening: ocurrences will always be nil because you ask for a non existent key of the hash aparisons in the following line:

occurrences = aparisons[String(element)]

Which would be equivalent to the following lines (one for each iteration of each ):

occurrences = aparisons["20"]
occurrences = aparisons["37"]
occurrences = aparisons["20"]
occurrences = aparisons["21"]

Nowhere are you assigning value to those keys in the hash aparisons , which you create with aparisons = {} , or an empty hash . p>

You seem to be trying to add the element key to the hash , which you can achieve with:

aparisons[String(element)] = 0

You should also change the following line:

aparisons["element"]=1

Well, you're assigning the value 1 to the key "element" ; and it seems that what you are trying to do is assign it to the key with the name equal to the element corresponding to the iteration, or:

aparisons[String(element)] = 1

As for your algorithm, I think it's more complicated than it should be, so it would be worth checking the logic (I recommend doing the exercise by hand / paper and then take it to code).

As a final comment, you could avoid using String(element) using the to_s method: element.to_s ; or, better yet, use the numbers as the names of the keys, it is not necessary to convert them to string ; for example:

aparisons[element]

If you want to see the modified code (following your same logic), discover the following code fragment (although I recommend you try it before you see it):

def delete_nth(elements, max_ocurrences)
  aparisons = Hash.new(0)

  elements.each_with_object([]) do |element, new_array|
    aparisons[element] += 1
    
    if aparisons[element] <= max_ocurrences
      new_array << element
    end
  end
end
    
answered by 06.04.2018 / 05:12
source