Need to sort array based on another array of array - ruby

I have two arrays which are to be compared and sorted based on another array. Here is as follows:
a = [["A", 1075000], ["C", 1750000], ["D", 0], ["E", 0], ["B", 0]]
b = ['A','B','C','D','E']
The array a should be sorted in order as follows(in which a is compared with b):
[["A", 1075000], ["B", 0], ["C", 1750000], ["D", 0], ["E", 0]]
I have tried this:
sort_by a.sort! {|a1,b1| a1[0] <=> b1[0]}

Another way of doing this:
lookup = {}
b.each_with_index { |el, i| lookup[el] = i }
a.sort_by { |el| lookup.fetch(el.first) }
# => [["A", 1075000], ["B", 0], ["C", 1750000], ["D", 0], ["E", 0]]

I assume you want to sort the elements in a according to their position in b and that the elements in b are the strings 'A', 'B', etc and not constants.
Then I would do something like this:
a = [["A", 1075000], ["C", 1750000], ["D", 0], ["E", 0], ["B", 0]]
b = ['A','B','C','D','E']
a.sort { |x, y| b.index(x.first) <=> b.index(y.first) }
#=> [["A", 1075000], ["B", 0], ["C", 1750000], ["D", 0], ["E", 0]]
Depending on the size of b it might make sense to use sort_by instead of sort. sort_by catches the return value of the block and does not evaluate the block multiple times:
a.sort_by { |x| b.index(x) }

Related

Create a hash from an array using group_by

I have the following array
ages = [["a", 15],["b", 16], ["c", 15], ["d", 16], ["e", 17], ["f", 20]]
I have to create a hash with ages as values such that it looks like this
{15 => ["a","c"], 16=> ["b","d]....}
when I run the group_by method:
puts ages.group_by {|list| list[1]}
this is what i get:
{15=>[["a", 15], ["c", 15]], 16=>[["b", 16], ["d", 16]], 17=>[["e", 17]], 20=>[["f", 20]]}
I'd really appreciate it if you can clarify how to make this cleaner and get the values as an array of names with same ages.
ages = [["a", 15],["b", 16], ["c", 15], ["d", 16], ["e", 17], ["f", 20]]
You can simplify your first step:
ages.group_by(&:last)
#=> {15=>[["a", 15], ["c", 15]],
# 16=>[["b", 16], ["d", 16]],
# 17=>[["e", 17]],
# 20=>[["f", 20]]}
You then need only transform the values to the desired arrays:
ages.group_by(&:last).transform_values { |arr| arr.map(&:first) }
#=> {15=>["a", "c"],
# 16=>["b", "d"],
# 17=>["e"],
# 20=>["f"]}
When
arr = [["a", 15], ["c", 15]]
for example,
arr.map(&:first)
#=> ["a", "c"]
See Hash#transform_values.
The first thing that comes to mind is
irb(main):012:0> ages.group_by {|e| e[1]}.map {|k, v| [k, v.map(&:first)]}.to_h
=> {15=>["a", "c"], 16=>["b", "d"], 17=>["e"], 20=>["f"]}
Here we map the hash to key-value pairs, map the values of each pair to first elements then convert the pairs back to a hash.

Do something in middle of recursive function, then return as needed

How do I do something in the middle of a recursion, and return as needed? In other words, maybe no more recursion is needed because I have found a "solution" in which case to save resources, the recursion can stop.
For example, let's say I have a working permute method that does this
permute([["a","b"],[1,2]])
>>> [["a", 1], ["a", 2], ["b", 1], ["b", 2]]
Rather than have the method generate all 4 possibilities, if one meets my requirements, I'd like it to stop. For example, let's say I'm searching for ["a",2], then the method can stop after it creates the second possibility.
This is my current permute method that is working
def permute(arr)
if arr.length == 1
return arr.first
else
first = arr.shift
return first.product(permute(arr)).uniq
end
end
I feel like I need to inject a do block somewhere with something like the below, but not sure how/where...
if result_of_permutation_currently == ["a",2]
return ...
else
# continuing the permutations
end
You could write your method as follows.
def partial_product(arr, last_element)
#a = []
#last_element = last_element
recurse(arr)
#a
end
def recurse(arr, element = [])
first, *rest = arr
if rest.empty?
first.each do |e|
el = element + [e]
#a << el
return true if el == #last_element
end
else
first.each do |e|
rv = recurse(rest, element + [e])
return true if rv
end
end
false
end
arr = [["a","b"], [1,2,3], ["cat","dog"]]
partial_product(arr, ["b",2,"dog"])
#=> [["a", 1, "cat"], ["a", 1, "dog"], ["a", 2, "cat"],
# ["a", 2, "dog"], ["a", 3, "cat"], ["a", 3, "dog"],
# ["b", 1, "cat"], ["b", 1, "dog"], ["b", 2, "cat"],
# ["b", 2, "dog"]]
partial_product(arr, ["a",1,"dog"])
#=> [["a", 1, "cat"], ["a", 1, "dog"]]
partial_product(arr, ["b",2,"pig"])
#=> [["a", 1, "cat"], ["a", 1, "dog"], ["a", 2, "cat"],
# ["a", 2, "dog"], ["a", 3, "cat"], ["a", 3, "dog"],
# ["b", 1, "cat"], ["b", 1, "dog"], ["b", 2, "cat"],
# ["b", 2, "dog"], ["b", 3, "cat"], ["b", 3, "dog"]]
If you prefer to avoid using instance variables, you could carry a and last_element as arguments in recurse, but there would be inefficiencies by doing so, particularly in terms of memory use.
Here are two ways that could be done without using recursion.
Use each to generate elements of the desired array until the target pair is reached
def permute(arr1, arr2, last_pair = [])
arr1.each_with_object([]) do |e1,a|
arr2.each do |e2|
a << [e1, e2]
break a if [e1, e2] == last_pair
end
end
end
permute(["a","b"],[1,2],["b", 1])
#=> [["a", 1], ["a", 2], ["b", 1]]
permute(["a","b"],[1,2],["b", 99])
#=> [["a", 1], ["a", 2], ["b", 1], ["b", 2]]
permute(["a","b"],[1,2])
#=> [["a", 1], ["a", 2], ["b", 1], ["b", 2]]
permute(["a","b"],[],["b", 1])
#=> []
permute([],[1,2],["b", 1])
#=> []
permute([],[],["b", 1])
#=> []
Map a sequence of the indices of the desired array
def permute(arr1, arr2, last_pair = [])
n1 = arr1.size
n2 = arr2.size
idx1 = arr1.index(last_pair.first)
idx2 = idx1.nil? ? nil : arr2.index(last_pair.last)
return arr1.product(arr2) if idx2.nil?
0.step(to: idx1*n2+idx2).
map {|i| [arr1[(i % (n1*n2))/n2], arr2[i % n2]]}
end
permute(["a","b"],[1,2],["b", 1])
See Numeric#step
idx1*n2 + idx2, the number of elements in the array to be returned, is computed as follows.
last_pair = ["b", 1]
n2 = arr2.size
#=> 2
idx1 = arr1.index(last_pair.first)
#=> 1
idx2 = idx1.nil? ? nil : arr2.index(last_pair.last)
#=> 0
idx1*n2 + idx2
#=> 2
The element at index i of the array returned is:
n1 = arr1.size
#=> 2
[arr1[(i % (n1*n2))/n2], arr2[i % n2]]
#=> [["a","b"][(i % 2*2)/2], [1,2][i % 2]]
For i = 1 this is
[["a","b"][(1 % 4)/2], [1,2][1 % 2]]
#=> [["a","b"][0], [1,2][1]]
#=> [“a”, 2]
For i = 2 this is
[["a","b"][(2 % 4)/2], [1,2][2 % 2]]
#=> [["a","b"][1], [1,2][0]]
#=> [“b”,1]
Note that we cannot write
arr1.lazy.product(arr2).first(idx1*n2+idx2+1)
because arr1.lazy returns an enumerator (arr1.lazy
#=> #<Enumerator::Lazy: ["a", "b"]>) but Array#product requires it's receiver to be an array. It's for that reason that some Rubyists would like to see product made an Enumerable method (with a lazy version), but don't hold your breathe.

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.

Count consecutives

I need to write a method that does the following
consecutive_count("aaabbcbbaaa") == [["a", 3], ["b", 2], ["c", 1], ["b", 2], ["a", 3]]
I got the code, but it looks ugly and I'm trying to see a better solution, please advice.
Here is my code:
def consecutive_count(str)
el = str[0]; count = 0; result = []
str.split("").each do |l|
if (el != l)
result << [el, count]
count = 1
el = l
else
count +=1
end
end
result << [el, count] if !el.nil?
return result
end
Here is one way :
s = "aaabbcbbaaa"
s.chars.chunk{|e| e }.map{|item,ary| [item,ary.size]}
# => [["a", 3], ["b", 2], ["c", 1], ["b", 2], ["a", 3]]
"aaabbcbbaaa".scan(/(?<s>(?<c>.)\k<c>*)/).map{|s, c| [c, s.length]}
# => [["a", 3], ["b", 2], ["c", 1], ["b", 2], ["a", 3]]
or
"aaabbcbbaaa".scan(/((.)\2*)/).map{|s, c| [c, s.length]}
# => [["a", 3], ["b", 2], ["c", 1], ["b", 2], ["a", 3]]
A solution which does not involve regex magic (although those are a bit shorter and probably faster) is this:
str.each_char.each_with_object([]) do |char, result|
if (result.last || [])[0] == char
result.last[1] += 1
else
result << [char, 1]
end
end
Depending on your level of understanding, it might better transport your intended meaning which might help to debug the thing in 6 month :)
Regexp solution:
my_s = "aaabbcbbaaa"
p my_s.scan(/(.)(\1*)/).map{|x,y| [x, y.size + 1]}
#=> [["a", 3], ["b", 2], ["c", 1], ["b", 2], ["a", 3]]
or
a, result = "aaabbcbbaaa", []
result << a.slice!(/(\w)\1*/) until a.empty?
and then map the result with counts.
You can try:
def consecutive_count(str)
result = {}
array = str.split(//).uniq
array.each.map {|char| result[char] = 0}
array.each do |char|
while str.starts_with?(char) do
result[char] += 1
str[0] = ""
end
result
end

Resources