Sort array by other array - ruby

I have two arrays:
a = [ 1, 0, 2, 1, 0]
b = ['a', 'b', 'c', 'd', 'e']
I want to order the b array according to a's elements values.
I can make this by merging the two arrays into a Hash and the order by key:
h = Hash[b.zip a]
=> {"a"=>1, "b"=>0, "c"=>2, "d"=>1, "e"=>0}
h2 = Hash[h.sort_by{|k, v| v}]
=> {"b"=>0, "e"=>0, "a"=>1, "d"=>1, "c"=>2}
array = h2.keys
=> ["b", "e", "a", "d", "c"]
Where there is a tie the order may be chosen arbitrary.
Is there a way (maybe more compact), I can achieve this without using the hash.

a.zip(b).sort.map(&:last)
In parts:
p a.zip(b) # => [[1, "a"], [0, "b"], [2, "c"], [1, "d"], [0, "e"]]
p a.zip(b).sort # => [[0, "b"], [0, "e"], [1, "a"], [1, "d"], [2, "c"]]
p a.zip(b).sort.map(&:last) # => ["b", "e", "a", "d", "c"]

a = [ 1, 0, 2, 1, 0]
b = ['a', 'b', 'c', 'd', 'e']
p b.sort_by.each_with_index{|el,i| a[i]}
# => ["b", "e", "a", "d", "c"]

Related

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.

Count the identical string elements in Ruby array

I can't find a simple solution for this problem
For example we have an array:
["a", "a", "a", "a", "a", "b", "b", "c", "a", "a", "a"]
I need to count the identical elements in this way:
[["a", 5], ["b", 2], ["c", 1], ["a", 3]]
Uses the chunk method to group identical elements, then uses map to convert [letter, array] pairs to [letter, count].
arr = ["a", "a", "a", "a", "a", "b", "b", "c", "a", "a", "a"]
counted = arr.chunk { |x| x }.map { |a, b| [a, b.count] }
# => [["a", 5], ["b", 2], ["c", 1], ["a", 3]]
In Ruby 2.2 you could use Enumable#slice_when:
arr = ["a", "a", "a", "a", "a", "b", "b", "c", "a", "a", "a"]
arr.slice_when { |e,f| e!=f }.map { |a| [a.first, a.size] }
#=> [["a", 5], ["b", 2], ["c", 1], ["a", 3]]

Convert array into a hash

I try to learn map and group_by but it's difficult...
My array of arrays :
a = [ [1, 0, "a", "b"], [1, 1, "c", "d"], [2, 0, "e", "f"], [3, 1, "g", "h"] ]
Expected result :
b= {
1=> {0=>["a", "b"], 1=>["c", "d"]} ,
2=> {0=>["e", "f"]} ,
3=> {1=>["g", "h"]}
}
Group by the first value, the second value can just be 0 or 1.
A starting :
a.group_by{ |e| e.shift}.map { |k, v| {k=>v.group_by{ |e| e.shift}} }
=> [{1=>{0=>[["a", "b"]], 1=>[["c", "d"]]}},
{2=>{0=>[["e", "f"]]}}, {3=>{1=>[["g", "h"]]}}]
I want to get "a" and "b" with the 2 first values, it's the only solution that I've found... (using a hash of hash)
Not sure if group_by is the simplest solution here:
a = [ [1, 0, "a", "b"], [1, 1, "c", "d"], [2, 0, "e", "f"], [3, 1, "g", "h"] ]
result = a.inject({}) do |acc,(a,b,c,d)|
acc[a] ||= {}
acc[a][b] = [c,d]
acc
end
puts result.inspect
Will print:
{1=>{0=>["a", "b"], 1=>["c", "d"]}, 2=>{0=>["e", "f"]}, 3=>{1=>["g", "h"]}}
Also, avoid changing the items you're operating on directly (the shift calls), the collections you could be receiving in your code might not be yours to change.
If you want a somewhat custom group_by I tend do just do it manually. group_by creates an Array of grouped values, so it creates [["a", "b"]] instead of ["a", "b"]. In addition your code is destructive, i.e. it manipulates the value of a. That is only a bad thing if you plan on re using a later on in its original form, but important to note.
As I mentioned though, you might as well just loop through a once and build the desired structure instead of doing multiple group_bys.
b = {}
a.each do |aa|
(b[aa[0]] ||= {})[aa[1]] = aa[2..3]
end
b # => {1=>{0=>["a", "b"], 1=>["c", "d"]}, 2=>{0=>["e", "f"]}, 3=>{1=>["g", "h"]}}
With (b[aa[0]] ||= {}) we check for the existence of the key aa[0] in the Hash b. If it does not exist, we assign an empty Hash ({}) to that key. Following that, we insert the last two elements of aa (= aa[2..3]) into that Hash, with aa[1] as key.
Note that this does not account for duplicate primary + secondary keys. That is, if you have another entry [1, 1, "x", "y"] it will overwrite the entry of [1, 1, "c", "d"] because they both have keys 1 and 1. You can fix that by storing the values in an Array, but then you might as well just do a double group_by. For example, with destructive behavior on a, handling "duplicates":
# Added [1, 1, "x", "y"], removed some others
a = [ [1, 0, "a", "b"], [1, 1, "c", "d"], [1, 1, "x", "y"] ]
b = Hash[a.group_by(&:shift).map { |k, v| [k, v.group_by(&:shift) ] }]
#=> {1=>{0=>[["a", "b"]], 1=>[["c", "d"], ["x", "y"]]}}
[[1, 0, "a", "b"], [1, 1, "c", "d"], [2, 0, "e", "f"], [3, 1, "g", "h"]].
group_by{ |e| e.shift }.
map{ |k, v| [k, v.inject({}) { |h, v| h[v.shift] = v; h }] }.
to_h
#=> {1=>{0=>["a", "b"], 1=>["c", "d"]}, 2=>{0=>["e", "f"]}, 3=>{1=>["g", "h"]}}
Here's how you can do it (nondestructively) with two Enumerable#group_by's and an Object#tap. The elements of a (arrays) could could vary in size and the size of each could be two or greater.
Code
def convert(arr)
h = arr.group_by(&:first)
h.keys.each { |k| h[k] = h[k].group_by { |a| a[1] }
.tap { |g| g.keys.each { |j|
g[j] = g[j].first[2..-1] } } }
h
end
Example
a = [ [1, 0, "a", "b"], [1, 1, "c", "d"], [2, 0, "e", "f"], [3, 1, "g", "h"] ]
convert(a)
#=> {1=>{0=>["a", "b"], 1=>["c", "d"]}, 2=>{0=>["e", "f"]}, 3=>{1=>["g", "h"]}}
Explanation
h = a.group_by(&:first)
#=> {1=>[[1, 0, "a", "b"], [1, 1, "c", "d"]],
# 2=>[[2, 0, "e", "f"]],
# 3=>[[3, 1, "g", "h"]]}
keys = h.keys
#=> [1, 2, 3]
The first value of keys passed into the block assigns the value 1 to the block variable k. We will set h[1] to a hash f, computed as follows.
f = h[k].group_by { |a| a[1] }
#=> [[1, 0, "a", "b"], [1, 1, "c", "d"]].group_by { |a| a[1] }
#=> {0=>[[1, 0, "a", "b"]], 1=>[[1, 1, "c", "d"]]}
We need to do further processing of this hash, so we capture it with tap and assign it to tap's block variable g (i.e., g will initially equal f above). g will be returned by the block after modification.
We have
g.keys #=> [0, 1]
so 0 is the first value passed into each's block and assigned to the block variable j. We then compute:
g[j] = g[j].first[2..-1]
#=> g[0] = [[1, 0, "a", "b"]].first[2..-1]
#=> ["a", "b"]
Similarly, when g's second key (1) is passed into the block,
g[j] = g[j].first[2..-1]
#=> g[1] = [[1, 1, "c", "d"]].first[2..-1]
#=> ["c", "d"]
Ergo,
h[1] = g
#=> {0=>["a", "b"], 1=>["c", "d"]}
h[2] and h[3] are computed similarly, giving us the desired result.

How can I push keys to an unsorted array?

I'm trying to create an array of the keys of an ordered hash. I want them to be listed in the same order in both the array and the hash. I have this hash.
h = { "a" => 3, "b" => 1, "c" = 4, "d" = 2 }
What I want is this array.
arr = ["b", "d", "a", "c"]
I have
h.sort_by { |k, v| v}
h.keys
but that returns the keys in alphabetical order. What can I do to keep them in the order of the sorted hash?
h.sort_by{|k,v| v} will give you [["b", 1], ["d", 2], ["a", 3], ["c", 4]], then use .map to get the key.
h.sort_by{|k,v| v}.map &:first
h = { "a" => 3, "b" => 1, "c" => 4, "d" => 2 }
p h.sort_by(&:last).map(&:first) #=> ["b", "d", "a", "c"]
You may try this also,
h = { "a" => 3, "b" => 1, "c" => 4, "d" => 2 }
Hash[h.sort_by{|k,v| v}].keys
#=> ["b", "d", "a", "c"]
This code
h.sort_by { |k,v| v}
h.keys
doesn't work because the sort_by method doesn't sort the original array, it returns a new sorted array, where each value is a (key, value) pair from the original hash:
[["b", 1], ["d", 2], ["a", 3], ["c", 4]]
If you're using Ruby 2.1.1, you can then just call to_h on the array, which will re-map the key/value pairs back into a hash:
h.sort_by { |k, v| v}.to_h.keys

how to get the indexes of duplicating elements in a ruby array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
arr = ["A", "X", "X", "D", "C", "B", "A"}
arr.detect{|e| arr.count(e) > 1}
duplicating_value_index_int_array = arr.index(<all duplicating values>)
Hi I want to get all the duplicating element's indexes from a ruby array. How may I achieve this?
duplicates = arr.each_with_index.group_by(&:first).inject({}) do |result, (val, group)|
next result if group.length == 1
result.merge val => group.map {|pair| pair[1]}
end
This will return a hash where the keys will be the duplicate elements and the values will be an array containing the index of each occurrence.
For your test input, the result is:
{"A"=>[0, 6], "X"=>[1, 2]}
If all your care about is the indices you can do duplicates.values.flatten to get an array with just the indices.
In this case: [0, 6, 1, 2]
This is quite straightforward implementation. It may be improved greatly, I think
arr = ["A", "X", "X", "D", "C", "B", "A"]
groups = arr.each.with_index.group_by{|s, idx| s}.to_a # => [["A", [["A", 0], ["A", 6]]], ["X", [["X", 1], ["X", 2]]], ["D", [["D", 3]]], ["C", [["C", 4]]], ["B", [["B", 5]]]]
repeating_groups = groups.select{|key, group| group.length > 1} # => [["A", [["A", 0], ["A", 6]]], ["X", [["X", 1], ["X", 2]]]]
locations = repeating_groups.each_with_object({}) {|(key, group), memo| memo[key] = group.map{|g| g[1]}} # => {"A"=>[0, 6], "X"=>[1, 2]}
It's not clear exactly what you want, but this code will find the indices of all elements of an array that aren't unique. It's far from efficient, but probably it doesn't need to be.
arr = %W/ A X X D C B A /
dup_indices = arr.each_index.find_all { |i| arr.count(arr[i]) > 1 }
p dup_indices
output
[0, 1, 2, 6]
I will assume a valid Ruby array arr as follows:
arr = ["A", "X", "X", "D", "C", "B", "A"]
Under this arr, and further assumption that it does not include nil:
arr.map.with_index{|e, i| i if arr.count(e) > 1}.compact
# => [0, 1, 2, 6]

Resources