ruby - Combination of elements from multiple arrays -


i'm working on chemistry research project , need create files various angles. want every combination of following:

angle1 can [0, -36, -72, -108, -144, -180] angle2 can [-180, -108, -36] angle3 can [0, -36, -72, -108, -144, -180] angle4 can [-180, -108, -36] 

i wrote ruby code this, appears giving me half of number of expected combinations. programming skills aren't great, wondering if tell me i'm doing wrong.

thank can offer:

phi1 = [0, -36, -72, -108, -144, -180] psi1 = [-180, -108, -36]  phi2 = [0, -36, -72, -108, -144, -180] psi2 = [-180, -108, -36]  psi1.each |a|   psi2.each |b|     phi1.each |c|       psi2.each |d|          line1 = 'select' + "#{b}" + '}}'         line2 = 'select' + "#{a}" + '}}'         line3 = 'select' + "#{d}" + '}}'         line4 = 'select' + "#{c}" + '}}'          filename = "angles#{b}_#{a}_#{d}_#{c}"         puts filename         puts line1         puts line2         puts line3         puts line4       end     end   end end 

expected output 'puts filename' filename every combination of phi1, psi1, phi2, psi2. expect puts 324 times, it's doing 162 times.

you should use array#product here.

phi1 = [0, -36, -72, -108, -144, -180] psi1 = [-180, -108, -36] phi2 = [0, -36, -72, -108, -144, -180] psi2 = [-180, -108, -36]  phi1.product(psi1, phi2, psi2).each |arr|   puts "angles#{ arr.join("_") }"   arr.each { |angle| puts "select #{angle}" } end  angles0_-180_0_-180 select 0 select -180 select 0 select -180 angles0_-180_0_-108 select 0 select -180 select 0 select -108 ... angles-180_-36_-180_-108 select -180 select -36 select -180 select -108 angles-180_-36_-180_-36 select -180 select -36 select -180 select -36  phi1.product(psi1, phi2, psi2).count   #=> 324 

Comments