Ruby: Invert a hash to also preserve non unique values - ruby

I have a hash that looks like this:
{"a" => [1, 2, 3], "b" => [4, 5, 6], "c" => [3, 4, 5], "d" => [7, 2, 3]}
What I want to do is to make a hash of all existing values with an array of all keys that included it, e.g. turn the above into this:
{1 => ["a"], 2 => ["a", "d"], 3 => ["a", "c", "d"], 4 => ["b", "c"]}

Try this:
module HashReverser
def invert_map
each_with_object({}) do |(key, value), result|
value.each { |v| (result[v] ||= []) << key }
end
end
end
original = {"a" => [1, 2, 3], "b" => [4, 5, 6], "c" => [3, 4, 5]}
original.extend(HashReverser).invert_map # => {1=>["a"], 2=>["a"], 3=>["a", "c"], 4=>["b", "c"], 5=>["b", "c"], 6=>["b"]}

I do prefer #Jikku's solution, but there's always another way. Here's one.
(I see this is very close to #Chris's solution. I will leave it for the last line, which is a little different.)
Code
def inside_out(h)
g = h.flat_map { |s,a| a.product([s]) }
.group_by(&:first)
g.merge(g) { |_,a| a.map(&:last) }
end
Example
h = {"a" => [1, 2, 3], "b" => [4, 5, 6], "c" => [3, 4, 5], "d" => [7, 2, 3]}
inside_out(h)
#=> {1=>["a"], 2=>["a", "d"], 3=>["a", "c", "d"], 4=>["b", "c"],
# 5=>["b", "c"], 6=>["b"], 7=>["d"]}
Explanation
For h above:
a = h.flat_map { |s,a| a.product([s]) }
#=> [[1, "a"], [2, "a"], [3, "a"], [4, "b"], [5, "b"], [6, "b"],
# [3, "c"], [4, "c"], [5, "c"], [7, "d"], [2, "d"], [3, "d"]]
g = a.group_by(&:first)
#=> {1=>[[1, "a"]], 2=>[[2, "a"], [2, "d"]],
# 3=>[[3, "a"], [3, "c"], [3, "d"]],
# 4=>[[4, "b"], [4, "c"]],
# 5=>[[5, "b"], [5, "c"]],
# 6=>[[6, "b"]],
# 7=>[[7, "d"]]}
g.merge(g) { |_,a| a.map(&:last) }
#=> {1=>["a"], 2=>["a", "d"], 3=>["a", "c", "d"], 4=>["b", "c"],
# 5=>["b", "c"], 6=>["b"], 7=>["d"]}

An alternate solution:
# Given
h = {"a" => [1, 2, 3], "b" => [4, 5, 6], "c" => [3, 4, 5], "d" => [7, 2, 3]}
h.flat_map {|k, v| v.product [k]}.group_by(&:first).each_value {|v| v.map! &:last }
Or:
h.flat_map {|k, v| v.product [k]}.reduce({}) {|o, (k, v)| (o[k] ||= []) << v; o}
The idea here is that we use Array#product to create a list of inverted single key-value pairs:
product = h.flat_map {|k, v| v.product([k]) }
# => [[1, "a"], [2, "a"], [3, "a"], [4, "b"], [5, "b"], [6, "b"], [3, "c"], [4, "c"], [5, "c"], [7, "d"], [2, "d"], [3, "d"]]
Group them by the value of the first item in each pair:
groups = product.group_by(&:first)
# => {1=>[[1, "a"]], 2=>[[2, "a"], [2, "d"]], 3=>[[3, "a"], [3, "c"], [3, "d"]], 4=>[[4, "b"], [4, "c"]], 5=>[[5, "b"], [5, "c"]], 6=>[[6, "b"]], 7=>[[7, "d"]]}
And then convert the values to a list of the last values in each pair:
result = groups.each_value {|v| v.map! &:last }
# => {1=>["a"], 2=>["a", "d"], 3=>["a", "c", "d"], 4=>["b", "c"], 5=>["b", "c"], 6=>["b"], 7=>["d"]}

Related

How to combine elements from one array with each element from another array

I'm trying to combine elements from one array with every element from another array, I tried looking for some solutions but I couldn't figure it out.
Take these two arrays for example:
num = [1,2,3]
let = ["a","b","c"]
I want to combine them in order to obtain:
combined = [[1, "a"], [1, "b"], [1, "c"], [2, "a"], [2, "b"], [2, "c"],
[3, "a"], [3, "b"], [3, "c"]]
You can use #product:
num = [1,2,3]
let = ["a","b","c"]
num.product let
#=>[[1, "a"], [1, "b"], [1, "c"], [2, "a"], [2, "b"], [2, "c"], [3, "a"], [3, "b"], [3, "c"]]

How to replace this nil value in Ruby style

I have an array like
A=[[a,x,y,z],[nil,e,f,d],[nil,k,j,u],[nil,w,p,k],[b,x,y,z],[nil,e,f,d],[nil,k,j,u],[nil,w,p,k]]
Result
A=[[a,x,y,z],[a,e,f,d],[a,k,j,u],[a,w,p,k],[b,x,y,z],[b,e,f,d],[b,k,j,u],[b,w,p,k]]
You might be noticing that first character of the first array is replacing the upcoming nil value until first character of the first array is b and then nil values are replaced with b.
This one I have achieved through iterating the array and writing the if condition and storing the first character somewhere and then by comparing the first character while I am iterating. But I don't still don't find a ruby way of doing this. Can someone help me here
A = [['a',1,2],[nil,3,4],[nil,5,6],[nil,7,8],['b',9,10],[nil,11,12],[nil,13,14]]
A.each_cons(2){|first, last| last[0] ||= first[0] }
last[0] ||= first[0] means something like "Give last[0] the value of first[0], unless it already has a value (not nil or false)".
arr = [['a',1,2],[nil,3,4],[nil,5,6],[nil,7,8],['b',9,10],[nil,11,12],[nil,13,14]]
last = nil
arr.map do |f,*rest|
if f.nil?
[last, *rest]
else
last = f
[f, *rest]
end
end
#=> [["a", 1, 2], ["a", 3, 4], ["a", 5, 6], ["a", 7, 8],
# ["b", 9, 10], ["b", 11, 12], ["b", 13, 14]]
The steps are as follows:
last = nil
enum = arr.map
#=> #<Enumerator: [["a", 1, 2], [nil, 3, 4], [nil, 5, 6], [nil, 7, 8],
# ["b", 9, 10], [nil, 11, 12], [nil, 13, 14]]:map>
f,*rest = enum.next
#=> ["a", 1, 2]
f #=> "a"
rest
#=> [1, 2]
f.nil?
#=> false
last = f
#=> "a"
[f, *rest]
#=> ["a", 1, 2]
f,*rest = enum.next
#=> [nil, 3, 4]
f.nil?
#=> true
[last, *rest]
#=> ["a", 3, 4]
f,*rest = enum.next
#=> [nil, 5, 6]
f.nil?
#=> true
[last, *rest]
#=> ["a", 5, 6]
f,*rest = enum.next
#=> [nil, 7, 8]
f.nil?
#=> true
[last, *rest]
#=> ["a", 7, 8]
and so on.
Alternatively,
arr.drop(1).each_with_object([arr.first]) { |a,ar|
ar << [a.first || ar.last.first, *a.drop(1)] }
#=> [["a", 1, 2], ["a", 3, 4], ["a", 5, 6], ["a", 7, 8],
# ["b", 9, 10], ["b", 11, 12], ["b", 13, 14]]

How to find the coordinates of a specific matrix-entry in ruby

Say I have a matrix, 5×6, filled with single letters and a few special signs, like this one:
upper = [['A', 'B', 'C', 'D', 'E'],
['F', 'G', 'H', 'I', 'J'],
['K', 'L', 'M', 'N', 'O'],
['P', 'Q', 'R', 'S', 'T'],
['U', 'V', 'W', 'X', 'Y'],
['Z', ',', '.', ' ', '?']]
How can I determine the coordinates of single letters of a string I loop through?
I found no clear description on the #index method, and all ways I've tried to call it (i.e. upper.index("A") ), have failed.
In the end I'm trying to code a simple version of the two-square encryption method and this is the one step I am currently stumped on.
Thanks for your help!
You could build an array of coordinates:
coordinates = upper.first.each_index.to_a.product(upper.each_index.to_a)
#=> [[0, 0], [0, 1], [0, 2], [0, 3], [0, 4], [0, 5],
# [1, 0], [1, 1], [1, 2], [1, 3], [1, 4], [1, 5],
# [2, 0], [2, 1], [2, 2], [2, 3], [2, 4], [2, 5],
# [3, 0], [3, 1], [3, 2], [3, 3], [3, 4], [3, 5],
# [4, 0], [4, 1], [4, 2], [4, 3], [4, 4], [4, 5]]
And find the item's coordinates by traversing this array:
coordinates.find { |x, y| upper[y][x] == 'A' }
#=> [0, 0]
coordinates.find { |x, y| upper[y][x] == '?' }
#=> [4, 5]
coordinates.find { |x, y| upper[y][x] == '-' }
#=> nil
Alternative approach
Instead of a (two-dimenstional) array of rows, you could use a (one-dimensional) hash of coordinate => value pairs. Example:
str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ,. ?'
upper = {}
str.each_char.with_index { |c, i| upper[i.divmod(5)] = c }
upper
#=> {[0, 0]=>"A", [0, 1]=>"B", [0, 2]=>"C", [0, 3]=>"D", [0, 4]=>"E",
# [1, 0]=>"F", [1, 1]=>"G", [1, 2]=>"H", [1, 3]=>"I", [1, 4]=>"J",
# [2, 0]=>"K", [2, 1]=>"L", [2, 2]=>"M", [2, 3]=>"N", [2, 4]=>"O",
# [3, 0]=>"P", [3, 1]=>"Q", [3, 2]=>"R", [3, 3]=>"S", [3, 4]=>"T",
# [4, 0]=>"U", [4, 1]=>"V", [4, 2]=>"W", [4, 3]=>"X", [4, 4]=>"Y",
# [5, 0]=>"Z", [5, 1]=>",", [5, 2]=>".", [5, 3]=>" ", [5, 4]=>"?"}
The values can be accessed by [x, y] coordinates:
upper[[0, 2]] #=> "C"
and looking up a key is trivial:
upper.key('C') #=> [0, 2]
You can also build an inverse hash that maps values to coordinates:
upper.invert
#=> {"A"=>[0, 0], "B"=>[0, 1], "C"=>[0, 2], "D"=>[0, 3], "E"=>[0, 4],
# "F"=>[1, 0], "G"=>[1, 1], "H"=>[1, 2], "I"=>[1, 3], "J"=>[1, 4],
# "K"=>[2, 0], "L"=>[2, 1], "M"=>[2, 2], "N"=>[2, 3], "O"=>[2, 4],
# "P"=>[3, 0], "Q"=>[3, 1], "R"=>[3, 2], "S"=>[3, 3], "T"=>[3, 4],
# "U"=>[4, 0], "V"=>[4, 1], "W"=>[4, 2], "X"=>[4, 3], "Y"=>[4, 4],
# "Z"=>[5, 0], ","=>[5, 1], "."=>[5, 2], " "=>[5, 3], "?"=>[5, 4]}
I would start with building hash letter ⇒ index:
hash = upper.each_with_index.inject({}) do |memo, (inner, x)|
inner.each_with_index.inject(memo) do |memo, (letter, y)|
memo[letter] = [x,y]
memo
end
end
Now we have a desired hash, so to determine an index:
▶ hash['C']
#⇒ [
# [0] 0,
# [1] 2
# ]
Since according to your question, you are going to iterate through string and find an index for each letter, it is way more efficient not to lookup for an index on every loop iteration. Once this hash is built, the lookup for indices will be as fast as hash lookup.
Will this work for you?
Assuming c represents a character whose index you are interested in, we loop through the 2-D array - and if a inner array contains c, then, we take its index in outer array, and index of c in that inner array, and assign them as array to value pos
upper=[['A','B','C','D','E'],['F','G','H','I','J'],
['K','L','M','N','O'],['P','Q','R','S','T'],
['U','V','W','X','Y'],['Z',',','.',' ','?']]
def find_index(array, c)
# initialize the return value to be empty
pos = []
# each_index is a method that will execute the given block (code inside {..}
# for each element of the array, by passing index as parameter to block.
# In this case 'i' receives index value.
# if array[i].include?(c) checks whether array[i] has an element with
# value contained in variable c. If yes, then, we lookup index of c in
# it by using array[i].index(c).
# We then use array literal notation, such as pos = [x,y], to assign the
# the result
array.each_index { |i| pos = [i, array[i].index(c)] if array[i].include?(c) }
# Return pos - you need not use 'return' keyword, last statement's value is treated as return value in Ruby
return pos
end
# p prints the variable value - in below cases prints the return value of function calls
p find_index(upper, 'C')
p find_index(upper, 't'.upcase)
p find_index(upper, 'S')
x, y = *find_index(upper, '?') # Here we splat the array into two variables
p x
p y
Output
[0, 2]
[3, 4]
[3, 3]
5
4
Here is the another variant of above function which purposefully stays away with advanced Ruby Idioms and tries to keep code easier to understand
def find_index(array, c)
x = nil, y = nil
# Find the index of array that has element c
for i in 0..array.length-1 do
if array[i].include?(c)
x = i
end
end
# Find the index of c within that array
y = array[x].index(c)
return [x, y]
end
The following is one way to do it:
upper = [['A','B','C','D','E','C'],
['F','C','C','C','G','H','I','J'],
['K','L','M','N','O'],
['P','Q','R','S','T'],
['U','V','W','X','Y','C'],
['Z',',','.',' ','?']
]
target = 'C'
upper.each_with_index.with_object([]) { |(a,i),arr|
a.each_with_index { |c,j| arr << [i,j] if c == target } }
#=> [[0, 2], [0, 5], [1, 1], [1, 2], [1, 3], [4, 5]]
Note that Array#index doesn't work when the target appears more than once in an inner array.
If the target can appear at most once in each inner array, you could write:
upper = [['A','B','C','D','E'],
['F','C','G','H','I','J'],
['K','L','M','N','O'],
['P','Q','R','S','T'],
['U','V','W','X','Y','C'],
['Z',',','.',' ','?']
]
target = 'C'
upper.each_with_index.with_object([]) do |(a,i),arr|
j = a.index(target)
arr << [i,j] if j
end
#=> [[0, 2], [1, 1], [4, 5]]
As you are a self-confessed Ruby newbie, this probably looks pretty formidable. It's not so bad, however, if we break it down step-by-step.
We first send the method Enumerable#each_with_index to the "receiver" upper, without a block. If you examine the docs for that method, you'll see that an enumerator is returned:
enum0 = upper.each_with_index
#=> #<Enumerator: [["A", "B", "C", "D", "E"], ["F", "C", "G", "H", "I", "J"],
# ["K", "L", "M", "N", "O"], ["P", "Q", "R", "S", "T"],
# ["U", "V", "W", "X", "Y", "C"],
# ["Z", ",", ".", " ", "?"]]:each_with_index>
Next, the method Enumerator#with_object is sent to enum0, with an argument equal to an empty array (the "object"):
enum1 = enum0.with_object([])
#=> #<Enumerator: #<Enumerator: [["A", "B", "C", "D", "E"],
# ["F", "C", "G", "H", "I", "J"], ["K", "L", "M", "N", "O"],
# ["P", "Q", "R", "S", "T"], ["U", "V", "W", "X", "Y", "C"],
# ["Z", ",", ".", " ", "?"]]:each_with_index>:with_object([])>
(Since enum0 is an instance of the class Enumerator, each_object must be a method of that class.)
As you see, enum1 is another enumerator, which you might think of as a "compound" enumerator. (Inspect the return value above carefully.) We can view the elements of this enumerator by converting it to an array:
enum1.to_a
#=> [[[["A", "B", "C", "D", "E"], 0], []],
# [[["F", "C", "G", "H", "I", "J"], 1], []],
# [[["K", "L", "M", "N", "O"], 2], []],
# [[["P", "Q", "R", "S", "T"], 3], []],
# [[["U", "V", "W", "X", "Y", "C"], 4], []],
# [[["Z", ",", ".", " ", "?"], 5], []]]
enum1 contains six elements, the first being:
[[["A", "B", "C", "D", "E"], 0], []]
The elements of enum1 are passed into the block by Enumerator#each (which calls Array#each). We can use Enumerator#next to sequentially obtain those elements and set the block variables equal to them:
(a,i),arr = enum1.next
#=> [[["A", "B", "C", "D", "E"], 0], []]
a #=> ["A", "B", "C", "D", "E"]
i #=> 0
arr #=> []
The array passed into the block is broken down by Ruby's use of "parallel assignment" and "disambiguation".
We can now perform the block calculation:
j = a.index(target)
#=> j = ["A", "B", "C", "D", "E"].index("C")
#=> 2
arr << [i,j] if j
#=> [] << [0,2] if 2
#=> [[0, 2]]
So now arr (which will be returned when the calculations are complete) equals [[0, 2]].
The next element of enum1 is now passed into the block and assigned to the block variables:
(a,i),arr = enum1.next
#=> [[["F", "C", "G", "H", "I", "J"], 1], [[0, 2]]]
a #=> ["F", "C", "G", "H", "I", "J"]
i #=> 1
arr #=> [[0, 2]]
Notice that arr has been updated. We now perform the block calculation:
j = a.index(target)
#=> ["F", "C", "G", "H", "I", "J"].index("C")
#=> 1
arr << [i,j] if j
#=> [[0, 2]] << [1,1] if 1
#=> [[0, 2], [1, 1]]
The third element of enum1 is passed to the block:
(a,i),arr = enum1.next
#=> [[["K", "L", "M", "N", "O"], 2], [[0, 2], [1, 1]]]
a #=> ["K", "L", "M", "N", "O"]
i #=> 2
arr #=> [[0, 2], [1, 1]]
j = a.index(target)
#=> ["K", "L", "M", "N", "O"].index("C")
#=> nil
arr << [i,j] if j
#=> [[0, 2], [1, 1]] << [2,nil] if nil
so arr is not altered. The remainIng calculations are similar.

Ruby - putting array elements into another array in order

array1 = [ [a], [b], [c], [d], [e] ]
array2 = [1, 2, 3, 4, 5, ...]
How can I put each of the elements of array2 into each the elements of array1 to get something like:
array3 = [ [a, 1], [b, 2], [c, 3], [d, 4], ... ]
I'm trying something like array1.map { |a| [a, array2.each { |b| b}] }, but not really sure how to get it yet.
Thanks!
Just try this using Array#flatten and Array#zip
array1 = [ ['a'], ['b'], ['c'], ['d'], ['e'] ]
array2 = [1, 2, 3, 4, 5]
array1.flatten.zip(array2)
# [["a", 1], ["b", 2], ["c", 3], ["d", 4], ["e", 5]]
More information about Array#zip can be found here.
array1 = [ ['a'], ['b'], ['c'], ['d','e'] ]
array2 = [1, 2, 3, 4]
If you do not wish to alter array1 or array2:
array1.zip(array2).map { |a1,e2| a1 + [e2] }
#=> [["a", 1], ["b", 2], ["c", 3], ["d", "e", 4]]
array1
#=> [ ['a'], ['b'], ['c'], ['d','e'] ]
If you do wish to alter array1 but not array2:
array1.zip(array2).map { |a1,e2| a1 << e2 }
#=> [["a", 1], ["b", 2], ["c", 3], ["d", "e", 4]]
array1
#=> [["a", 1], ["b", 2], ["c", 3], ["d", "e", 4]]
If you do wish to alter array1 and can also alter array2:
array1.map { |a| a << array2.shift }
#=> [["a", 1], ["b", 2], ["c", 3], ["d", "e", 4]]
array1
#=> [["a", 1], ["b", 2], ["c", 3], ["d", "e", 4]]
array2
#=> []
In the first two cases you could use Array#transpose instead of Array#zip by replacing array1.zip(array2) with [array1, array2].transpose.

Inject each element of an array to a different array

I have two arrays:
array1: [[1, 2], [2, 3]]
array2: ["a", "b", "c"]
I would like to combine those two and get the below result:
[[1, 2, "a"], [1, 2, "b"], [1, 2, "c"], [2, 3, "a"], [2, 3, "b"], [2, 3, "c"]]
You can use Array#product:
array1.product(array2).map &:flatten
#=> [[1, 2, "a"], [1, 2, "b"], [1, 2, "c"], [2, 3, "a"], [2, 3, "b"], [2, 3, "c"]]
Array#product is purpose-built for this, but this is one alternative:
array2.flat_map { |e| array1.map { |arr| arr+[e] } }

Resources