is ruby assoc array a hashtable internally? what is lookup time complexity? - arrays

aa = [ %w[Someone 1],
%w[Bla 2]]
p aa.assoc("Someone")
p aa.assoc("Bla")
# Result:
# ["Someone", "1"]
# ["Bla", "2"]
p aa.rassoc("1")
p aa.rassoc("2")
# Result:
# ["Someone", "1"]
# ["Bla", "2"]
is ruby assoc array a hashtable internally? what is lookup time complexity?
for example is this call linear or O(1) aa.assoc("Someone")
thanks

This discussion implies it is a linear search, not a hash table: http://error.news/question/4883140/does-ruby-arrayassoc-use-linear-search/
This makes sense because the data type you initialize is just an array. assoc() is just a function operating over it.
There is a Hash data type though: http://ruby-doc.org/core-1.9.3/Hash.html

Related

Why is the method #each_index non existing for hashes in Ruby?

Why can I use #each and #each_with_index on both arrays and hashes, but the #each_index method can be used only on an array and not on a hash?
each_index
Iterates over array indexes.
Hashes don't have indexes. Hashes have keys. Use each_key.
Why do Hashes have each_with_index? Because that's part of the Enumerable module which both Hashes and Arrays include. Enumerable methods use shared terminology. Hashes and Arrays predate the Enumerable module, Array#each_index (sometime before v1.0r2) came before Enumerable#each_with_index (Feb 5th, 1998), so there is some overlap between Array, Hash, and Enumerable for backwards compatibility.
Hashes don't have indexes. The order of elements in a Hash is unstable and can change. You can iterate over keys and values with each.
hash = { a: 1, b: 2 }
hash.each do |key, val|
puts "#{key.inspect} => #{val.inspect}"
end
#=> :a => 1
#=> :b => 2
If you really need to iterate over a hash with a counter of some sort you can just chain each with with_index like so:
hash = { a: 1, b: 2 }
hash.each.with_index do |key_val, i|
key, val = key_val
puts "#{i}| #{key.inspect} => #{val.inspect}"
end
#=> 0| :a => 1
#=> 1| :b => 2

How to change order of elements in array of arrays?

I want to transform the array
[[:name1, :value1], [:name2, :value2]]
to array
[[:value1, :name1], [:value2, :name2]]
What is the best way to do this?
Various options (each version followed by short-hand sugar):
# Create a new array of swapped elements
my_array = my_array.map{ |a| a.reverse }
my_array = my_array.map(&:reverse)
# Mutate the existing array with swapped elements
my_array.map!{ |a| a.reverse }
my_array.map!(&:reverse)
# Mutate the elements in the existing array
my_array.each{ |a| a.reverse! }
my_array.each(&:reverse!)
The first option is the 'safest', insofar as no existing arrays are modified (in case you have other references to them that should not be changed).
The second option is roughly the same as the first, unless you had two references to my_array and wanted one of them unswapped.
The third option is the most 'destructive'; every reference to the pairwise elements will be changed. However, it's also the most memory-efficient, because no new arrays are created.
Just-for-fun answer (rotate the matrix like rubik's cube):
array = [[:name1, :value1], [:name2, :value2]]
array.transpose # [[:name1, :name2], [:value1, :value2]]
.reverse # [[:value1, :value2], [:name1, :name2]]
.transpose
#=> [[:value1, :name1], [:value2, :name2]]
One more (not as funny and obvious, but still possible) way:
array.inject(&:zip).reverse.inject(&:zip)
#=> [[:value1, :name1], [:value2, :name2]]
One more way is to use to_h to_a:
array = [[:name1, :value2], [:name2, :value2]]
array.to_h # {name1: :value1, name2: :value2}
.invert # {value1: :name1, value2: :name2}
.to_a # [[:value1, :name1], [:value2, :name2]]

Ruby : Choosing between each, map, inject, each_with_index and each_with_object

When I started writing Ruby many years ago, it took me a while to understand the difference between each and map. It only got worse when I discovered all the other Enumerable and Array methods.
With the help of the official documentation and many StackOverflow questions, I slowly began to understand what those methods did.
Here is what took me even longer to understand though :
Why should I use one method or another?
Are there any guidelines?
I hope this question isn't a duplicate : I'm more interested in the "Why?" than the "What?" or "How?", and I think it could help Ruby newcomers.
A more tl;dr answer:
How to choose between each, map, inject, each_with_index and each_with_object?
Use #each when you want "generic" iteration and don't care about the result. Example - you have numbers, you want to print the absolute value of each individual number:
numbers.each { |number| puts number.abs }
Use #map when you want a new list, where each element is somehow formed by transforming the original elements. Example - you have numbers, you want to get their squares:
numbers.map { |number| number ** 2 }
Use #inject when you want to somehow reduce the entire list to one single value. Example - you have numbers, you want to get their sum:
numbers.inject(&:+)
Use #each_with_index in the same situation as #each, except you also want the index with each element:
numbers.each_with_index { |number, index| puts "Number #{number} is on #{index} position" }
Uses for #each_with_object are more limited. The most common case is if you need something similar to #inject, but want a new collection (as opposed to singular value), which is not a direct mapping of the original. Example - number histogram (frequencies):
numbers.each_with_object({}) { |number, histogram| histogram[number] = histogram[number].to_i.next }
Which object can I use?
First, the object you're working with should be an Array, a Hash, a Set, a Range or any other object that respond to each. If it doesn't, it might be converted to something that will. You cannot call each directly on a String for example, because you need to specify if you'd like to iterate over each byte, character or line.
"Hello World".respond_to?(:each)
#=> false
"Hello World".each_char.respond_to?(:each)
#=> true
I want to calculate something with each element, just like with a for loop in C or Java.
If you want to iterate over each element, do something with it and not modify the original object, you can use each. Please keep reading though, in order to know if you really should.
array = [1,2,3]
#NOTE: i is a bound variable, it could be replaced by anything else (x, n, element). It's a good idea to use a descriptive name if you can
array.each do |i|
puts "La"*i
end
#=> La
# LaLa
# LaLaLa
It is the most generic iteration method, and you could write any of the other mentioned methods with it. We will actually, for pedagogical purposes only. If you spot a similar pattern in your code, you could probably replace it with the corresponding method.
It is basically never wrong to use each, it is almost never the best choice though. It is verbose and not Ruby-ish.
Note that each returns the original object, but this is rarely (never?) used. The logic happens inside the block, and should not modify the original object.
The only time I use each is:
when no other method would do. The more I learn about Ruby, the less often it happens.
when I write a script for someone who doesn't know Ruby, has some programming experience (e.g. C, Fortran, VBA) and would like to understand my code.
I want to get an Array out of my String/Hash/Set/File/Range/ActiveRecord::Relation
Just call object.to_a.
(1..10).to_a
#=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
"Hello world".each_char.to_a
#=> ["H", "e", "l", "l", "o", " ", "w", "o", "r", "l", "d"]
{:a => 1, :b => 2}.to_a
#=> [[:a, 1], [:b, 2]]
Movie.all.to_a #NOTE: Probably very inefficient. Try to keep an ActiveRecord::Relation as Relation for as long as possible.
#=> [Citizen Kane, Trois couleurs: Rouge, The Grapes of Wrath, ....
Some methods described below (e.g. compact, uniq) are only defined for Arrays.
I want to get a modified Array based on the original object.
If you want to get an Array based on the original object, you can use map. The returned object will have the same size as the original one.
array = [1,2,3]
new_array = array.map do |i|
i**2
end
new_array
#=> [1, 4, 9]
#NOTE: map is often used in conjunction with other methods. Here is the corresponding one-liner, without creating a new variable :
array.map{|i| i**2}
#=> [1, 4, 9]
# EACH-equivalent (For pedagogical purposes only):
new_array = []
array.each do |i|
new_array << i**2
end
new_array
#=> [1, 4, 9]
The returned Array will not replace the original object.
This method is very widely used. It should be the first one you learn after each.
collect is a synonym of map. Make sure to use only one of both in your projects.
I want to get a modified Hash based on the original Hash.
If your original object is a Hash, map will return an Array anyway. If you want a Hash back :
hash = {a: 1, b: 2}
hash.map{|key, value| [key, value*2]}.to_h
#=> {:a=>2, :b=>4}
# EACH-equivalent
hash = {a: 1, b: 2}
new_hash = {}
hash.each do |key,value|
new_hash[key]=value*2
end
new_hash
#=> {:a=>2, :b=>4}
I want to filter some elements.
I want to remove nil elements
You can call compact. It will return a new Array without the nil elements.
array = [1,2,nil,4,5]
#NOTE: array.map{|i| i*2} Would raise a NoMethodError
array.compact
# => [1, 2, 4, 5]
# EACH-equivalent
new_array = []
array.each do |integer_or_nil|
new_array << integer_or_nil unless integer_or_nil.nil?
end
new_array
I want to write some logic to determine if an element should be kept in the new Array
You can use select or reject.
integers = (1..10)
integers.select{|i| i.even?}
# => [2, 4, 6, 8, 10]
integers.reject{|i| i.odd?}
# => [2, 4, 6, 8, 10]
# EACH-equivalent
new_array = []
integers.each do |i|
new_array << i if i.even?
end
new_array
I want to remove duplicate elements from your Array
You can use uniq :
letters = %w(a b a b c)
letters.uniq
#=> ["a", "b", "c"]
# EACH-equivalent
uniq_letters = []
letters.each do |letter|
uniq_letters << letter unless uniq_letters.include?(letter)
end
uniq_letters
#TODO: Add find/detect/any?/all?/count
#TODO: Add group_by/sort/sort_by
I want to iterate over all the elements while counting from 0 to n-1
You can use each_with_index :
letters = %w(a b c)
letters.each_with_index do |letter, i|
puts "Letter ##{i} : #{letter}"
end
#=> Letter #0 : a
# Letter #1 : b
# Letter #2 : c
#NOTE: There's a nice Ruby syntax if you want to use each_with_index with a Hash
hash = {:a=>1, :b=>2}
hash.each_with_index{|(key,value),i| puts "#{i} : #{key}->#{value}"}
# => 0 : a->1
# 1 : b->2
# EACH-equivalent
i = 0
letters.each do |letter|
puts "Letter ##{i} : #{letter}"
i+=1
end
each_with_index returns the original object.
I want to iterate over all the elements while setting a variable during each iteration and using it in the next iteration.
You can use inject :
gauss = (1..100)
gauss.inject{|sum, i| sum+i}
#=> 5050
#NOTE: You can specify a starting value with gauss.inject(0){|sum, i| sum+i}
# EACH-equivalent
sum = 0
gauss.each do |i|
sum = sum + i
end
puts sum
It returns the variable as defined by the last iteration.
reduce is a synonym. As with map/collect, choose one keyword and keep it.
I want to iterate over all the elements while keeping a variable available to each iteration.
You can use each_with_object :
letter_ids = (1..26)
letter_ids.each_with_object({}){|i,alphabet| alphabet[("a".ord+i-1).chr]=i}
#=> {"a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5, "f"=>6, "g"=>7, "h"=>8, "i"=>9, "j"=>10, "k"=>11, "l"=>12, "m"=>13, "n"=>14, "o"=>15, "p"=>16, "q"=>17, "r"=>18, "s"=>19, "t"=>20, "u"=>21, "v"=>22, "w"=>23, "x"=>24, "y"=>25, "z"=>26}
# EACH-equivalent
alphabet = {}
letter_ids.each do |i|
letter = ("a".ord+i-1).chr
alphabet[letter]=i
end
alphabet
It returns the variable as modified by the last iteration. Note that the order of the two block variables is reversed compared to inject.
If your variable is a Hash, you should probably prefer this method to inject, because h["a"]=1 returns 1, and it would require one more line in your inject block to return a Hash.
I want something that hasn't been mentioned yet.
Then it's probably okay to use each ;)
Notes :
It's a work in progress, and I would gladly appreciate any feedback. If it's interesting enough and fit in one page, I might extract a flowchart out of it.

Splitting a hash variable with reduce

When invoking reduce on an array of hashes, I thought I could split the hash by key and value within the parameters using the () technique. But in this case, it does not appear to work:
columns = [
{"lead"=>["source", 2]},
{"parent"=>["name", 4]}
]
columns.reduce({}) do |acc, (k,v)|
puts "k #{k} v #{v}"
end
# k {"lead"=>["source", 2]} v
# k {"parent"=>["name", 4]} v
# => nil
I expected k to be "lead" and v to be ["source", 2]. Because columns is an array and not a hash, I cannot do this (k,v) to get the key/value pair of hash. Is there another technique I can use in argument list in order to pass the k/v pair rather than having to dissect it in the block?
It happens because the logic behind this parenthesis is basically a parallel assignment, which doesn't work in your case because each item in the collection is a hash. So practically what you can do it convert each item to array instead:
columns = columns.flat_map(&:to_a)
Demonstration

Find the index by current sort order of an array in ruby

If there is an array
array A = ["a","b","c","d"] #Index is [0,1,2,3]
And it's sorted to.
array A = ["d","c","b","a"]
I need an array that returns me the updated index based on the sorted order
[3,2,1,0]
I'm trying to find a solution to this ruby
UPDATE to the question
If a is sorted to
array A = ["d","b","c","a"] #not a pure reverse
Then the returned index array should be
[3,1,2,0]
You need to create a mapping table that preserves the original order, then use that order to un-map the re-ordered version:
orig = %w[ a b c d ]
orig_order = orig.each_with_index.to_h
revised = %w[ d c b a ]
revised.map { |e| orig_order[e] }
# => [3, 2, 1, 0]
So long as your elements are unique this will be able to track any shift in order.
Here is one way to do this:
original_array = ["a","b","c","d"]
jumbled_array = original_array.shuffle
jumbled_array.map {|i| original_array.index(i)}
#=> [1, 3, 0, 2]
Note:
In this sample, output will change for every run as we are using shuffle to demonstrate the solution.
The solution will work only as long as array has no duplicate values.
If you do wish to solution to work with arrays with duplicate values, then, one possibility is to look at object_id of array members while figuring out the index.
jumbled_array.map {|i| original_array.map(&:object_id).index(i.object_id)}
This solution will work as long as jumbled_array contains element from original_array and no elements were recreated using dup or something that results in change in object_id values
You can use the map and index methods.
arr = ["a","b","c","d"]
sort_arr = ["d","c","b","a"]
sort_arr.map{|s| arr.index(s)}
# => [3, 2, 1, 0]

Resources