I have a ruby array with 4 strings
nucleotides = ['A', 'G', 'C', 'T']
I need 4^3 combinations of strings.
new_array = ['AGC', 'AGT' .. 'TTT']
This is the wobble hypothesis in genetics. Could someone explain how I could go about achieving this using ruby.
use Array#repeated_permutation
:
nucleotides = %w(A G C T)
nucleotides.repeated_permutation(3).map(&:join)
#=> ["AAA", "AAG", "AAC", "AAT", "AGA", "AGG", "AGC", "AGT", "ACA", "ACG", "ACC", "ACT", "ATA", "ATG", "ATC", "ATT", "GAA", "GAG", "GAC", "GAT", "GGA", "GGG", "GGC", "GGT", "GCA", "GCG", "GCC", "GCT", "GTA", "GTG", "GTC", "GTT", "CAA", "CAG", "CAC", "CAT", "CGA", "CGG", "CGC", "CGT", "CCA", "CCG", "CCC", "CCT", "CTA", "CTG", "CTC", "CTT", "TAA", "TAG", "TAC", "TAT", "TGA", "TGG", "TGC", "TGT", "TCA", "TCG", "TCC", "TCT", "TTA", "TTG", "TTC", "TTT"]
nucleotides = ['A', 'G', 'C', 'T']
(nucleotides*3).permutation(3).to_a.map(&:join).uniq
#=> ["AGC", "AGT", "AGA", "AGG", "ACG", "ACT", "ACA", "ACC", "ATG", "ATC", "ATA", "ATT", "AAG", "AAC", "AAT", "AAA", "GAC", "GAT", "GAA", "GAG", "GCA", "GCT", "GCG", "GCC", "GTA", "GTC", "GTG", "GTT", "GGA", "GGC", "GGT", "GGG", "CAG", "CAT", "CAA", "CAC", "CGA", "CGT", "CGG", "CGC", "CTA", "CTG", "CTC", "CTT", "CCA", "CCG", "CCT", "CCC", "TAG", "TAC", "TAA", "TAT", "TGA", "TGC", "TGG", "TGT", "TCA", "TCG", "TCC", "TCT", "TTA", "TTG", "TTC", "TTT"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With