Trouble transposing the array which has different size [duplicate] - ruby

I have an array:
arr=[[1,2,3],[4,5],[6]],
I have the following code:
arr.transpose
but it doesn't work,how to solve it?
I am getting
[[1,2,3],[4,5],[6]].transpose
IndexError: element size differs (2 should be 3)
from (irb):13:in `transpose'
from (irb):13
from /home/durrant
my solution:
arr.reduce(&:zip).map(&:flatten)
output:
[[1, 4, 6], [2, 5, nil], [3, nil, nil]]

Using zip as in Stefan's answer is the most straightforward, but if you insist on using transpose, then:
l = arr.map(&:length).max
arr.map{|e| e.values_at(0...l)}.transpose
# => [[1, 4, 6], [2, 5, nil], [3, nil, nil]]
Or without using either:
Array.new(arr.map(&:length).max){|i| arr.map{|e| e[i]}}
# => [[1, 4, 6], [2, 5, nil], [3, nil, nil]]

A similar answer was posted (but deleted) an hour earlier:
arr = [[1, 2, 3], [4, 5], [6]]
arr[0].zip(*arr[1..-1])
#=> [[1, 4, 6], [2, 5, nil], [3, nil, nil]]
The above is equivalent to:
[1, 2, 3].zip([4, 5], [6])
This approach assumes that your first sub-array is always the longest. Otherwise the result will be truncated:
arr = [[1, 2], [3, 4, 5], [6]]
arr[0].zip(*arr[1..-1])
#=> [[1, 3, 6], [2, 4, nil]] missing: [nil, 5, nil]

If the length of the subarrays don’t match, an IndexError is raised.
irb(main):002:0> arr=[[1,2,3],[4,5],[6]]
=> [[1, 2, 3], [4, 5], [6]]
irb(main):003:0> arr.transpose
IndexError: element size differs (2 should be 3)
from (irb):3:in `transpose'
from (irb):3
from /Users/liuxingqi/.rvm/rubies/ruby-2.1.2/bin/irb:11:in `<main>'
should be:
irb(main):004:0> arr=[[1,2,3],[4,5,6]]
=> [[1, 2, 3], [4, 5, 6]]
irb(main):005:0> arr.transpose
=> [[1, 4], [2, 5], [3, 6]]
or
irb(main):006:0> arr=[[1,2],[3,4],[5,6]]
=> [[1, 2], [3, 4], [5, 6]]
irb(main):007:0> arr.transpose
=> [[1, 3, 5], [2, 4, 6]]

Related

How to get 'fair combination' from an array of n elements?

Using combination method on Ruby,
[1, 2, 3, 4, 5, 6].combination(2).to_a
#=> [[1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [2, 3],
# [2, 4], [2, 5], [2, 6], [3, 4], [3, 5], [3, 6],
# [4, 5], [4, 6], [5, 6]]
we can get a 2-dimensional array having 15 (6C2) elements.
I would like to create a fair_combination method that returns an array like this:
arr = [[1, 2], [3, 5], [4, 6],
[3, 4], [5, 1], [6, 2],
[5, 6], [1, 3], [2, 4],
[2, 3], [4, 5], [6, 1],
[1, 4], [2, 5], [3, 6]]
So that every three sub-arrays (half of 6) contain all the given elements:
arr.each_slice(3).map { |a| a.flatten.sort }
#=> [[1, 2, 3, 4, 5, 6],
# [1, 2, 3, 4, 5, 6],
# [1, 2, 3, 4, 5, 6],
# [1, 2, 3, 4, 5, 6],
# [1, 2, 3, 4, 5, 6]]
This makes it kind of "fair", by using as different elements as possible as arrays go on.
To make it more general, what it needs to satisfy is as follows:
(1) As you follow the arrays from start and count how many times each number appears, at any point it should be as flat as possible;
(1..7).to_a.fair_combination(3)
#=> [[1, 2, 3], [4, 5, 6], [7, 1, 4], [2, 5, 3], [6, 7, 2], ...]
The first 7 numbers make [1,2,...,7] and so do the following 7 numbers.
(2) Once number A comes in the same array with B, A does not want to be in the same array with B if possible.
(1..10).to_a.fair_combination(4)
#=> [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 1, 5], [2, 6, 9, 3], [4, 7, 10, 8], ...]
Is there any good algorithm that creates a "fair combination" like this ?
It's not guaranteed to give the best solution, but it gives a good enough one.
At each step, it chooses a minimal subpool which is the set of items of minimal height, for which there is still a combination to choose from (height is the number of times the items have been used before).
For instance, let the enumerator be
my_enum = FairPermuter.new('abcdef'.chars, 4).each
The first iteration may return
my_enum.next # => ['a', 'b', 'c', 'd']
At this point those letters have height 1, but there is not enough letters of height 0 to make a combination, so take just all of them for the next:
my_enum.next # => ['a', 'b', 'c', 'e'] for instance
Now the heights are 2 for a, b and c, 1 for d and e, and 0 for f, and still the optimal pool is the full initial set.
So this is not really optimized for combinations of large size. On the other side, if the size of the combination is at most half of the size of the initial set, then the algorithm is pretty decent.
class FairPermuter
def initialize(pool, size)
#pool = pool
#size = size
#all = Array(pool).combination(size)
#used = []
#counts = Hash.new(0)
#max_count = 0
end
def find_valid_combination
[*0..#max_count].each do |height|
candidates = #pool.select { |item| #counts[item] <= height }
next if candidates.size < #size
cand_comb = [*candidates.combination(#size)] - #used
comb = cand_comb.sample
return comb if comb
end
nil
end
def each
return enum_for(:each) unless block_given?
while combination = find_valid_combination
#used << combination
combination.each { |k| #counts[k] += 1 }
#max_count = #counts.values.max
yield combination
return if #used.size >= [*1..#pool.size].inject(1, :*)
end
end
end
Results for fair combinations of 4 over 6
[[1, 2, 4, 6], [3, 4, 5, 6], [1, 2, 3, 5],
[2, 4, 5, 6], [2, 3, 5, 6], [1, 3, 5, 6],
[1, 2, 3, 4], [1, 3, 4, 6], [1, 2, 4, 5],
[1, 2, 3, 6], [2, 3, 4, 6], [1, 2, 5, 6],
[1, 3, 4, 5], [1, 4, 5, 6], [2, 3, 4, 5]]
Results of fair combination of 2 over 6
[[4, 6], [1, 3], [2, 5],
[3, 5], [1, 4], [2, 6],
[4, 5], [3, 6], [1, 2],
[2, 3], [5, 6], [1, 6],
[3, 4], [1, 5], [2, 4]]
Results of fair combinations of 2 over 5
[[4, 5], [2, 3], [3, 5],
[1, 2], [1, 4], [1, 5],
[2, 4], [3, 4], [1, 3],
[2, 5]]
Time to get combinations of 5 over 12:
1.19 real 1.15 user 0.03 sys
Naïve implementation would be:
class Integer
# naïve factorial implementation; no checks
def !
(1..self).inject(:*)
end
end
class Range
# constant Proc instance for tests; not needed
C_N_R = -> (n, r) { n.! / ( r.! * (n - r).! ) }
def fair_combination(n)
to_a.permutation
.map { |a| a.each_slice(n).to_a }
.each_with_object([]) do |e, memo|
e.map!(&:sort)
memo << e if memo.all? { |me| (me & e).empty? }
end
end
end
▶ (1..6).fair_combination(2)
#⇒ [
# [[1, 2], [3, 4], [5, 6]],
# [[1, 3], [2, 5], [4, 6]],
# [[1, 4], [2, 6], [3, 5]],
# [[1, 5], [2, 4], [3, 6]],
# [[1, 6], [2, 3], [4, 5]]]
▶ (1..6).fair_combination(3)
#⇒ [
# [[1, 2, 3], [4, 5, 6]],
# [[1, 2, 4], [3, 5, 6]],
# [[1, 2, 5], [3, 4, 6]],
# [[1, 2, 6], [3, 4, 5]],
# [[1, 3, 4], [2, 5, 6]],
# [[1, 3, 5], [2, 4, 6]],
# [[1, 3, 6], [2, 4, 5]],
# [[1, 4, 5], [2, 3, 6]],
# [[1, 4, 6], [2, 3, 5]],
# [[1, 5, 6], [2, 3, 4]]]
▶ Range::C_N_R[6, 3]
#⇒ 20
Frankly, I do not understand how this function should behave for 10 and 4, but anyway this implementation is too memory consuming to work properly on big ranges (on my machine it gets stuck on ranges of size > 8.)
To adjust this to more robust solution one needs to get rid of permutation there in favor of “smart concatenate permuted arrays.”
Hope this is good for starters.

How do I convert a string to an array of arrays?

I have a string with an array of arrays inside:
"[[1, 2], [3, 4], [5, 6]]"
Can I convert this to the array of arrays, without using eval or a regular expression, gsub, etc.?
Can I make turn it into:
[[1, 2], [3, 4], [5, 6]]
How about the following?
require 'json'
arr = JSON.parse("[[1, 2], [3, 4], [5, 6]]") # => [[1, 2], [3, 4], [5, 6]]
arr[0] # => [1, 2]
The same can be done using Ruby standard libaray documentation - YAML:
require 'yaml'
YAML.load("[[1, 2], [3, 4], [5, 6]]")
# => [[1, 2], [3, 4], [5, 6]]

Ruby array access 2 consecutive(chained) elements at a time

Now, This is the array,
[1,2,3,4,5,6,7,8,9]
I want,
[1,2],[2,3],[3,4] upto [8,9]
When I do, each_slice(2) I get,
[[1,2],[3,4]..[8,9]]
Im currently doing this,
arr.each_with_index do |i,j|
p [i,arr[j+1]].compact #During your arr.size is a odd number, remove nil.
end
Is there a better way??
Ruby reads your mind. You want cons ecutive elements?
[1, 2, 3, 4, 5, 6, 7, 8, 9].each_cons(2).to_a
# => [[1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 8], [8, 9]]
.each_cons does exactly what you want.
[1] pry(main)> a = [1,2,3,4,5,6,7,8,9]
=> [1, 2, 3, 4, 5, 6, 7, 8, 9]
[2] pry(main)> a.each_cons(2).to_a
=> [[1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 8], [8, 9]]
You almost got it right :)
arr = [1,2,3,4,5,6,7,8,9]
arr.each_cons(2) do |chunk|
p chunk
end
# >> [1, 2]
# >> [2, 3]
# >> [3, 4]
# >> [4, 5]
# >> [5, 6]
# >> [6, 7]
# >> [7, 8]
# >> [8, 9]
And if you wanted to implement your own each_cons:
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
cons = 2
0.upto(arr.size - cons) do |i|
p arr[i, cons]
end
Output:
[1, 2]
[2, 3]
[3, 4]
[4, 5]
[5, 6]
[6, 7]
[7, 8]
[8, 9]

How to reshape a Ruby array

Having an array
a = [1, 2, 3, 4, 5, 6]
I want to reshape it to
a = [[1, 2], [3, 4], [5, 6]]
I've had an impression that there was a specific method for this. I've just been through Array class reference, but failed to find it. Does anyone remember?
You can do something like this:
a = [1, 2, 3, 4, 5, 6]
a.each_slice(2).to_a # => [[1, 2], [3, 4], [5, 6]]
Like this, for example:
a = [1, 2, 3, 4, 5, 6]
a.each_slice(2).to_a # => [[1, 2], [3, 4], [5, 6]]

Creating permutations from a multi-dimensional array in Ruby

I have the following multi-dimensional array in Ruby:
[[1,2], [3], [4,5,6]]
I need to have the following output:
[[1,3,4], [1,3,5], [1,3,6], [2,3,4], [2,3,5], [2,3,6]]
I have tried creating a recursive function, but I'm not having much luck.
Are there any Ruby functions that would help with this? Or is the only option to do it recursively?
Thanks
Yup, Array#product does just that (Cartesian product):
a = [[1,2], [3], [4,5,6]]
head, *rest = a # head = [1,2], rest = [[3], [4,5,6]]
head.product(*rest)
#=> [[1, 3, 4], [1, 3, 5], [1, 3, 6], [2, 3, 4], [2, 3, 5], [2, 3, 6]]
Another variant:
a.inject(&:product).map(&:flatten)
#=> [[1, 3, 4], [1, 3, 5], [1, 3, 6], [2, 3, 4], [2, 3, 5], [2, 3, 6]]

Resources