freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-an.../intermediate-algorithm-scri.../dna-pairing.md

1.9 KiB
Raw Blame History

id challengeType forumTopicId localeTitle
afd15382cdfb22c9efe8b7de 5 16009 DNA 配对

Description

DNA 链缺少配对元素。对于每个字符,获取与其配对的元素,并将结果作为二维数组返回。 碱基对 是一对 AT 和 CG。将缺少的元素与提供的字符匹配。 将提供的字符作为每个数组中的第一个元素返回。 例如,对于输入 GCG返回“G”, “C”][“C”, “G”][“G”, “C”。 字符及与其配对的元素在一个数组中。再将所有数组放到一个封装数组中。

Instructions

Tests

tests:
  - text: "<code>pairElement('ATCGA')</code>应该返回<code>[['A','T'],['T','A'],['C','G'],['G','C'],['A','T']]</code>。"
    testString: assert.deepEqual(pairElement("ATCGA"),[["A","T"],["T","A"],["C","G"],["G","C"],["A","T"]]);
  - text: "<code>pairElement('TTGAG')</code>应该返回<code>[['T','A'],['T','A'],['G','C'],['A','T'],['G','C']]</code>。"
    testString: assert.deepEqual(pairElement("TTGAG"),[["T","A"],["T","A"],["G","C"],["A","T"],["G","C"]]);
  - text: "<code>pairElement('CTCTA')</code>应该返回<code>[['C','G'],['T','A'],['C','G'],['T','A'],['A','T']]</code>。"
    testString: assert.deepEqual(pairElement("CTCTA"),[["C","G"],["T","A"],["C","G"],["T","A"],["A","T"]]);

Challenge Seed

function pairElement(str) {
  return str;
}

pairElement("GCG");

Solution

var lookup = Object.create(null);
lookup.A = 'T';
lookup.T = 'A';
lookup.C = 'G';
lookup.G = 'C';

function pairElement(str) {
 return str.split('').map(function(p) {return [p, lookup[p]];});
}