I need to swap letters in a string (DNA strand) using Ruby and the following rules:
'A' is replaced by 'T' 'T' is replaced by 'A''C' is replaced by 'G''G' is replaced by 'C'For example, 'ACGTA' should become 'TGCAT'.
I have only got this far:
def DNA_strand(dna)
dna.tr!('A', 'T')
end
You were quite close:
dna.tr('ATCG', 'TAGC') # => "TGCAT"
See ruby-doc.org on tr:
Returns a copy of
strwith the characters infrom_strreplaced by the corresponding characters into_str.
Use tr! the same way if you want to modify your string in-place.
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