简体   繁体   中英

Counting in binary ruby

I have two arrays

[a0 b0 c0]

[a1 b1 c1]

I want to calculate all the possible sums between the two. A possible sum consists of only 1 element for each column slot. For example a possible sum is

a0 + b1 + c1

or

a1 + b1 + c1

but not a1 + a0 + b0 + c0

In other words the sum in the example will have 3 slots, each one having only 1 element of the two arrays. From my point of view this looks like counting in binary, where each slot can take only 1 out of two numbers (0 or 1). So in this example

000 means all the elements in the sum come from the first array

sum(000) = a0 + b0 + c0.
sum(111) = a1 + b1 + c1
sum(010) = a0 + b1 + c0

you get the memo.

I would like to know how can I do this in ruby. I'm thinking of a complicated solution where I count in a binary string and for each count I "select" the correct elements from the arrays. Since I want all the possible combinations (2^n), can I code this in a single line or close to that?

▶ a1 = [11,12,13]
#⇒ [11, 12, 13]
▶ b1 = [21,22,23]
#⇒ [21, 22, 23]
▶ a1.zip(b1).reduce(&:product).map(&:flatten)
#⇒ [[11, 12, 13], [11, 12, 23], [11, 22, 13], [11, 22, 23], 
#⇒  [21, 12, 13], [21, 12, 23], [21, 22, 13], [21, 22, 23]]
▶ a1.zip(b1).reduce(&:product).map(&:flatten).map { |e| e.reduce &:+ }
#⇒ [36, 46, 46, 56, 46, 56, 56, 66]

UPD Just out of curiosity, this is @pangpang's solution written in ruby:

[0,1].repeated_permutation([a1.length, a2.length].min).map do |bits|
  bits.each_with_index.reduce(0) do |memo, (e, i)| 
    memo + (e.zero? ? a1[i] : a2[i])
  end
end
arr1 = [0,0,0]
arr2 = [1,1,1]

(0..(2**arr1.length-1)).each do |i|
   sum = 0
   bina = "%0#{arr1.length}b" % i # convert int to binary
   bina.split("").each_with_index do |e,i|
       e.to_i == 0 ? sum += arr1[i] : sum += arr2[i]
   end
   puts "#{bina} and #{sum}"
end

output:

000 sum 0
001 sum 1
010 sum 1
011 sum 2
100 sum 1
101 sum 2
110 sum 2
111 sum 3

Here is a brute force approach. I'm sure that there is a way more elegant way to do this with a lambda but my brain doesn't work that way at this time of day:

2.1.2 :003 > a=[1,2,3]
 => [1, 2, 3] 
2.1.2 :005 > b=[4,5,6]
 => [4, 5, 6] 
2.1.2 :006 > 1.downto(0) do |outer|
2.1.2 :007 >     1.downto(0) do |middle|
2.1.2 :008 >       1.downto(0) do |inner|
2.1.2 :009 >         puts (outer==1 ? b[0] : a[0]) + (middle==1 ? b[1] : a[1]) + (inner==1 ? b[2] : a[2])
2.1.2 :010?>       end
2.1.2 :011?>     end
2.1.2 :012?>   end
15
12
12
9
12
9
9
6

Here's another way to implement @pangpang's answer. I've also attempted to explain the basic idea of this approach.

Code

def perm_sums(arr0, arr1)
  sz = arr0.size
  at = [arr0, arr1].transpose
  (0...2**sz).map { |n| sz.times.reduce(0) { |t,i| t + at[i][n[i]] } }
end

Example

arr0 = [1,2,3]
arr1 = [6,7,8]

perm_sums(arr0, arr1) #=> [6, 11, 11, 16, 11, 16, 16, 21]

Explanation

For the example above:

sz = arr0.size #=> 3

at = [arr0, arr1].transpose #=> [[1, 6], [2, 7], [3, 8]]

This is of course the same as arr0.zip(arr1) .

e0 = (0...2**sz).map #=> #<Enumerator: 0...8:map>

We can view the elements of this enumerator by converting it to an array:

e0.to_a #=> [0, 1, 2, 3, 4, 5, 6, 7]

The first element of e0 is passed to the block and assigned to the block variable:

n = e0.next #=> 0 

n=0 is not so interesting, as its binary representation is all zero bits. Let's instead look at n=3 :

n = e0.next #=> 1 
n = e0.next #=> 2 
n = e0.next #=> 3

e1 = sz.times #=> #<Enumerator: 3:times> 
e1.to_a           #=> [0, 1, 2] 

The block calculations make use of Fixnum#[] . The binary representation of n=3 is shown by the string:

3.to_s(2).rjust(sz,'0') #=> "011"

3[i] gives the ith most significant digit of the binary value:

3[0] #=> 1
3[1] #=> 1
3[2] #=> 0

Block calculations proceed as follows. reduce sets the block variable t to the initial value of 0 and then passes each of the three elements of e1 to the block:

t = 0
i = e1.next     #=> 0
t + at[i][n[i]] #=> 0 + at[0][n[0]] => [1, 6][3[0]] => [1, 6][1] => 6

t = 6
i = e1.next     #=> 1
t + at[i][n[i]] #=> 1 + at[1][3[1]] => 1 + [2,7][1] => 8

t = 8
i = e1.next     #=> 2
t + at[i][n[i]] #=> 8 + at[2][n[2]] => 8 + [3,8][3[2]] => 8 + [3,8][0] => 11

i = e1.next
  #=> StopIteration: iteration reached an end

So the number 3 is mapped to 11 . Other calculations are performed similarly.

Note that we would get the same answer if we replaced at[i][n[i]] with at[i][n[sz-1-i]] (ie, extracting bits from high to low).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM