Related
I have (m = rows-1, n = cols-1) dimensional matrix.
And I passing i to method which will return array in following manner (provided with i <= m,n)
Suppose n=0, so for 4x4 matrix, it will return boundary elements position.
Do not consider below as ruby syntax, get only flow.
square = [[i,i] -> [i, m-i] -> [n-i, m-1] -> [n-i, i] -> [i,i]]
(no data is repeated in above)
I achieved above in recursion manner by setting parameters but I need easier/optimised trick.
Update - for user sawa
arr = [*1..16].each_slice(4).to_a
m,n = arr.length-1, arr[0].length-1
loop_count = 0
output = [[0, 0], [1, 0], [2, 0], [3, 0], [4, 0], [4, 1], [4, 2], [3, 2], [2, 2], [1, 2], [0, 2], [0, 1]]
loop_count = 1
output = [[1, 1], [2, 1], [2, 2], [1, 2]]
I ended up with this solution, but I think there is a better way out there.
First define a method to print the matrix mapped by indexes, just to check if the result id correct:
def print_matrix(n,m)
range_n, range_m = (0..n-1), (0..m-1)
mapp = range_m.map { |y| range_n.map { |x| [x, y] } }
mapp.each { |e| p e }
puts "-" * 8 * n
end
Then define a method that returns the frame starting from the loop s (where 0 is the external frame):
def frame (n, m, s = 0)
res = []
return res if (s >= n/2 and s >= m/2) and (n.even? or m.even?)
(s..n-s-1).each { |x| res << [x,s] }
(s..m-s-1).each { |y| res << [res.last[0], y] }
(res.last[0].downto s).each { |x| res << [x, res.last[1]] }
(res.last[1].downto s).each { |y| res << [res.last[0], y] }
res.uniq
end
Now, call the methods and check the output:
n, m, loops = 4, 4, 1
print_matrix(n,m)
frame(n, m, loops)
# [[0, 0], [1, 0], [2, 0], [3, 0]]
# [[0, 1], [1, 1], [2, 1], [3, 1]]
# [[0, 2], [1, 2], [2, 2], [3, 2]]
# [[0, 3], [1, 3], [2, 3], [3, 3]]
# --------------------------------
# [[1, 1], [2, 1], [2, 2], [1, 2]]
Here we can use Matrix methods to advantage, specifically Matrix::build, Matrix#minor and Matrix#[].
Code
require 'matrix'
def border_indices(nrows, ncols, i)
m = Matrix.build(nrows, ncols) { |r,c| [r,c] }.minor(i..nrows-1-i, i..ncols-1-i)
[[1,0,m.row_count-1], [0,1,m.column_count-1],
[-1,0,m.row_count-1], [0,-1,m.column_count-2]].
each_with_object([[0,0]]) do |(x,y,n),a|
n.times { a << [a.last.first+x, a.last.last+y] }
end.map { |i,j| m[i,j] }
end
Examples
nrows = 5
ncols = 6
border_indices(nrows, ncols, 0)
#=> [[0, 0], [1, 0], [2, 0], [3, 0],
# [4, 0], [4, 1], [4, 2], [4, 3], [4, 4],
# [4, 5], [3, 5], [2, 5], [1, 5],
# [0, 5], [0, 4], [0, 3], [0, 2], [0, 1]]
border_indices(nrows, ncols, 1)
#=> [[1, 1], [2, 1],
# [3, 1], [3, 2], [3, 3],
# [3, 4], [2, 4],
# [1, 4], [1, 3], [1, 2]]
border_indices(nrows, ncols, 2)
#=> [[2, 2], [2, 3]]
Explanation
Consider the calculation of border_indices(5, 6, 1).
nrows = 5
ncols = 6
i = 1
mat = Matrix.build(nrows, ncols) { |r,c| [r,c] }
#=> Matrix[[[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]]]
m = mat.minor(i..nrows-1-i, i..ncols-1-i)
#=> mat.minor(1..3, 1..4)
#=> Matrix[[[1, 1], [1, 2], [1, 3], [1, 4]],
# [[2, 1], [2, 2], [2, 3], [2, 4]],
# [[3, 1], [3, 2], [3, 3], [3, 4]]]
b = [[1,0,m.row_count-1], [0,1,m.column_count-1],
[-1,0,m.row_count-1], [0,-1,m.column_count-2]]
#=> [[1, 0, 2], [0, 1, 3], [-1, 0, 2], [0, -1, 2]]
c = b.each_with_object([[0,0]]) do |(x,y,n),a|
n.times { a << [a.last.first+x, a.last.last+y] }
end
#=> [[0, 0], [1, 0],
# [2, 0], [2, 1], [2, 2],
# [2, 3], [1, 3],
# [0, 3], [0, 2], [0, 1]]
c.map { |i,j| m[i,j] }
#=> [[1, 1], [2, 1],
# [3, 1], [3, 2], [3, 3],
# [3, 4], [2, 4],
# [1, 4], [1, 3], [1, 2]]
Note that in the calculation of c, a.last is the last pair of indices added to the array being constructed (a.last = [a.last.first, a.last.last]).
Following will work for both m == n & m != n case.
I hope, all will consider what matrix variable below stands for (2 D array)
def matrixRotation(matrix)
m,n = matrix.length-1, matrix[0].length-1
loop_count = [m,n].min/2
0.upto(loop_count) do |i|
indices = []
i.upto(m-i) { |j| indices << [j, i] }
i.upto(n-i) { |j| indices << [m-i, j] }
i.upto(m-i) { |j| indices << [m-j, n-i] }
i.upto(n-i) { |j| indices << [i, n-j] }
puts "-------------- For Layer #{i+1} ---------------", nil
indices = indices.uniq
values = indices.map { |x| matrix[x[0]][x[1]] }
puts 'indices:', indices.inspect, nil, 'values:', values.inspect
end
end
Say I have this array:
[0, 1, 4], [2, 3]
How can I merge them to get:
[0,2], [0,3], [1,2], [1,3], [4,2], [4,3]
I tried:
[0, 1, 4].zip [2, 3]
But I got:
[[0, 2], [1, 3], [4, nil]]
Any ideas?
[0, 1, 4].product([2, 3])
That should generate:
[[0, 2], [0, 3], [1, 2], [1, 3], [4, 2], [4, 3]]
Having the following nested array
[[[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]], [[5, 0], [5, 1], [5, 2], [5, 3], [5, 4], [5, 5]]]
I'd like to remove subarray containers until it becomes a 2 dimensional array like:
[[0,0], [5,1], [5,4]...]
.flatten removes everything and I need to keep the groups of 2 within subarrays.
also, next time you can try to read documentation :)
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]], [[5, 0], [5, 1], [5, 2], [5, 3], [5, 4], [5, 5]]]
a.flatten(1)
>[[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], [5, 0], [5, 1], [5, 2], [5, 3], [5, 4], [5, 5]]
How do I join two ranges into a 2d array as such in ruby? Using zip doesn't provide the result I need.
(0..2) and (0..2)
# should become => [[0,0],[0,1],[0,2], [1,0],[1,1],[1,2], [2,0],[2,1],[2,2]]
Ruby has a built in method for this: repeated_permutation.
(0..2).to_a.repeated_permutation(2).to_a
I'm puzzled. Here it is a day after the question was posted and nobody has suggested the obvious: Array#product:
[*0..2].product [*1..3]
#=> [[0, 1], [0, 2], [0, 3], [1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3]]
range_a = (0..2)
range_b = (5..8)
def custom_join(a, b)
a.inject([]){|carry, a_val| carry += b.collect{|b_val| [a_val, b_val]}}
end
p custom_join(range_a, range_b)
Output:
[[0, 5], [0, 6], [0, 7], [0, 8], [1, 5], [1, 6], [1, 7], [1, 8], [2, 5], [2, 6], [2, 7], [2, 8]]
straight forward solution:
range_a = (0..2)
range_b = (5..8)
def custom_join(a, b)
[].tap{|result| a.map{|i| b.map{|j| result << [i, j]; } } }
end
p custom_join(range_a, range_b)
Output:
[[0, 5], [0, 6], [0, 7], [0, 8], [1, 5], [1, 6], [1, 7], [1, 8], [2, 5], [2, 6], [2, 7], [2, 8]]
Simply, this will do it:
a = (0...2).to_a
b = (0..2).to_a
result = []
a.each { |ae| b.each { |be| result << [ae, be] } }
p result
# => [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2]]
i need to quickly determine the possible uniq combinations of elements in an array based on a condition.
They have the following structure:
[[id,parent_id]]
I have no problems with smaller arrays. If all the parent_ids are uniq. Example:
a = (1..6).to_a.map{ |a| [a,a] }
=> [[1, 1], [2, 2], [3, 3], [4, 4], [5, 5], [6, 6]]
a.combination(3).size # => 20
answers immediately.
If I have ids with reoccurring parent_ids I can still use combination and iterate through all combinations.
a = (1..7).to_a.map{ |a| [a,a] };a[6] = [7,6]
=> [[1, 1], [2, 2], [3, 3], [4, 4], [5, 5], [6, 6], [7, 6]]
a.combination(3).size # => 35
valid_combos = a.combination(3).to_a.select { |c| c.map(&:last).uniq.size == c.size }.size # => 30
This is still quick on small arrays. But if the arrays has 33 entries with 1 reoccurring parent_id I'll have to check 1166803110 combinations. This is slow. Of course.
Any ideas or hints on how to get this solved quickly and efficient are welcome.
I like the combination method for the Array class. But i would use a Hash or set too.
Also there could be arrays like:
a = [[1, 1], [2, 1], [3, 1], [4, 2], [5, 2], [6, 2], [7, 3], [8, 3]]
a.combination(3).size #=> 56
But only 18 are "valid".
Any help is appreciated.
EDIT:
Valid input no reoccurring parent_ids:
[[1, 1], [2, 2], [3, 3], [4, 4], [5, 5]]
Valid output with combination of 4 each (5 uniq combos):
[[[1, 1], [2, 2], [3, 3], [4, 4]], [[1, 1], [2, 2], [3, 3], [5, 5]], [[1, 1], [2, 2], [4, 4], [5, 5]], [[1, 1], [3, 3], [4, 4], [5, 5]], [[2, 2], [3, 3], [4, 4], [5, 5]]]
Valid input 1 reoccurring parent_ids:
[[1, 1], [2, 2], [3, 3], [4, 4], [5, 5], [6,5]]
Valid output with combination of 4 each (9 uniq combos):
[[[1, 1], [2, 2], [3, 3], [4, 4]], [[1, 1], [2, 2], [3, 3], [5, 5]], [[1, 1], [2, 2], [3, 3], [6, 5]], [[1, 1], [2, 2], [4, 4], [5, 5]], [[1, 1], [2, 2], [4, 4], [6, 5]], [[1, 1], [3, 3], [4, 4], [5, 5]], [[1, 1], [3, 3], [4, 4], [6, 5]], [[2, 2], [3, 3], [4, 4], [5, 5]], [[2, 2], [3, 3], [4, 4], [6, 5]]]
These are the invalid combos [5,5] and [6,5] aren't allowed:
[[[1, 1], [2, 2], [5, 5], [6, 5]], [[1, 1], [3, 3], [5, 5], [6, 5]], [[1, 1], [4, 4], [5, 5], [6, 5]], [[2, 2], [3, 3], [5, 5], [6, 5]], [[2, 2], [4, 4], [5, 5], [6, 5]], [[3, 3], [4, 4], [5, 5], [6, 5]]]
If I understand correctly, you want all possible combinations of ids where the ids don't share a parent id. I had a go at something different, just for fun, with no real idea if performance will improve.
x = [[1, 1], [2, 2], [3, 3], [4, 4], [5, 5], [6,5]]
First, let's flip reduce it.
hash = x.reduce({}) {|hash, pair| (hash[pair.last] ||= []).push pair.first}
#=> {1=>[1], 2=>[2], 3=>[3], 4=>[4], 5=>[5, 6]}
Now we get all possible combinations of the parent IDs.
parents = hash.keys.combination(4).to_a
#=> [[1, 2, 3, 4], [1, 2, 3, 5], [1, 2, 4, 5], [1, 3, 4, 5], [2, 3, 4, 5]]
Now we map each parent ID to it's child ids.
children = parents.map do |array|
array.map {|parent| hash[parent]}
end
#=> [[[1], [2], [3], [4]], [[1], [2], [3], [5, 6]], [[1], [2], [4], [5, 6]], [[1], [3], [4], [5, 6]], [[2], [3], [4], [5, 6]]]
We're knee deep in arrays at this point. Now, we take the product of each sub-array to get all possible combinations, and we don't even need to uniq them.
children.map {|array| array.first.product *array.drop(1)}.flatten(1)
#=> [[1, 2, 3, 4], [1, 2, 3, 5], [1, 2, 3, 6], [1, 2, 4, 5], [1, 2, 4, 6], [1, 3, 4, 5], [1, 3, 4, 6], [2, 3, 4, 5], [2, 3, 4, 6]]
Now you have all combinations of ids, and could use those to look up parent ids if you still need them using the opposite of the hash table.
What about performance? I benchmarked by running this file.
With 50 entries, 25 repeated, and combination of 4:
3957124
Original: 8.719000 0.110000 8.829000 ( 8.860909)
3957124
Simons: 4.875000 0.094000 4.969000 ( 6.458309)
So it looks quicker in theory. But, with 125 entries, 25 repeated, and combination of 4:
9811174
Original: 22.875000 0.281000 23.156000 ( 23.213483)
9811174
Simons: 20.703000 0.391000 21.094000 ( 21.232167)
Which is not much faster. This is because for so many combinations Ruby spends the majority of it's time doing memory allocation (try watching in Task Manager or top), which in Ruby is dog-slow. There's not really any helpful way to allocate the memory up-front, so beyond a certain point you're at a hard limit.
But this is only happening because you're forcing Ruby to collect all of the array items together at once. If you're specific use case allows you to deal with each combination individually, you can avoid most of the memory allocation. By calling yield with every child array (this file):
9811174
Simons: 8.485000 0.000000 8.485000 ( 8.476653)
Much quicker. You will also observe the memory usage remains constant. It's still gonna take a while though. However, if you have multiple cores you could in principle parallelise because once you have the hash each combination can be worked on independently of the others. I'll leave that for you to try :)
You can do that as follows.
Code
def combos(pairs, group_size)
pairs.group_by(&:last).
values.
combination(group_size).
flat_map { |a| a.shift.product(*a) }
end
Examples
pairs = [[1, 1], [2, 2], [3, 3], [4, 4], [5, 5], [6,5]]
combos(pairs, 4)
#=> [[[1, 1], [2, 2], [3, 3], [4, 4]],
# [[1, 1], [2, 2], [3, 3], [5, 5]],
# [[1, 1], [2, 2], [3, 3], [6, 5]],
# [[1, 1], [2, 2], [4, 4], [5, 5]],
# [[1, 1], [2, 2], [4, 4], [6, 5]],
# [[1, 1], [3, 3], [4, 4], [5, 5]],
# [[1, 1], [3, 3], [4, 4], [6, 5]],
# [[2, 2], [3, 3], [4, 4], [5, 5]],
# [[2, 2], [3, 3], [4, 4], [6, 5]]]
combos(pairs, 5)
#=> [[[1, 1], [2, 2], [3, 3], [4, 4], [5, 5]],
# [[1, 1], [2, 2], [3, 3], [4, 4], [6, 5]]]
combos(pairs, 1).size #=> 6
combos(pairs, 2).size #=> 14
combos(pairs, 3).size #=> 16
combos(pairs, 4).size #=> 9
combos(pairs, 5).size #=> 2
Explanation
For the array pairs used in the examples, and
group_size = 4
we perform the following calculations. First we group the elements of pairs by the last element of each pair (i.e., parent_id):
h = pairs.group_by(&:last)
#=> {1=>[[1, 1]], 2=>[[2, 2]], 3=>[[3, 3]], 4=>[[4, 4]], 5=>[[5, 5], [6, 5]]}
We only need the values from this hash:
b = h.values
#=> [[[1, 1]], [[2, 2]], [[3, 3]], [[4, 4]], [[5, 5], [6, 5]]]
We now obtain combinations of the elements of b:
enum = b.combination(group_size)
#=> b.combination(4)
#=> #<Enumerator: [[[1, 1]], [[2, 2]], [[3, 3]], [[4, 4]],
# [[5, 5], [6, 5]]]:combination(4)>
We can view the (5) elements of this enumerator by converting it to an array:
enum.to_a
#=> [[[[1, 1]], [[2, 2]], [[3, 3]], [[4, 4]]],
# [[[1, 1]], [[2, 2]], [[3, 3]], [[5, 5], [6, 5]]],
# [[[1, 1]], [[2, 2]], [[4, 4]], [[5, 5], [6, 5]]],
# [[[1, 1]], [[3, 3]], [[4, 4]], [[5, 5], [6, 5]]],
# [[[2, 2]], [[3, 3]], [[4, 4]], [[5, 5], [6, 5]]]]
The last step is to map each element of enum to the product of its elements (each element of enum being an array of pairs). We use Enumerable#flat_map so we don't have to subsequently do any flattening:
enum.flat_map { |a| a.shift.product(*a) }
returns the array given in the examples for group_size = 4.
Let's look more carefully as what is happening in the last statement:
enum1 = enum.flat_map
#=> #<Enumerator: #<Enumerator: [[[1, 1]], [[2, 2]], [[3, 3]], [[4, 4]],
# [[5, 5], [6, 5]]]:combination(4)>:flat_map>
You might want to think of enum1 as a "compound enumerator". The elements of enum1 are passed into it's block by Enumerator#each (which will call Array#each) and assigned to the block variable a. Let's look at the second value passed to the block.
Skip the first:
a = enum1.next
#=> [[[1, 1]], [[2, 2]], [[3, 3]], [[4, 4]]]
Pass in the second:
a = enum1.next
#=> [[[1, 1]], [[2, 2]], [[3, 3]], [[5, 5], [6, 5]]]
We take the product of these four arrays as follows:
a[0].product(a[1], a[2], a[3])
#=> [[[1, 1], [2, 2], [3, 3], [5, 5]],
# [[1, 1], [2, 2], [3, 3], [6, 5]]]
which we could also write:
a[0].product(*a[1..-1])
or, as I have done:
a.shift.product(*a)
Note that, in the last expression, a of *a is what's left of a after a.shift is executed.