Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Ruby中交换字符串中的字母_Ruby - Fatal编程技术网

在Ruby中交换字符串中的字母

在Ruby中交换字符串中的字母,ruby,Ruby,我需要使用Ruby和以下规则交换字符串(DNA链)中的字母: 'A'替换为'T' 'T'替换为'A' 'C'替换为'G' 'G'替换为'C' 例如,'ACGTA'应该变成'TGCAT' 我只走了这么远: def DNA_strand(dna) dna.tr!('A', 'T') end 你很接近: dna.tr('ATCG', 'TAGC') # => "TGCAT" 见: 返回str的副本,其中中的字符由 中的对应字符到\u str 使用tr

我需要使用Ruby和以下规则交换字符串(DNA链)中的字母:

  • 'A'
    替换为
    'T'
  • 'T'
    替换为
    'A'
  • 'C'
    替换为
    'G'
  • 'G'
    替换为
    'C'
例如,
'ACGTA'
应该变成
'TGCAT'

我只走了这么远:

def DNA_strand(dna)    
  dna.tr!('A', 'T')    
end
你很接近:

dna.tr('ATCG', 'TAGC')   # => "TGCAT"
见:

返回
str
的副本,其中
中的字符由
中的对应字符到\u str


使用
tr非常感谢!我不知道你能做到。你的问题是什么?