|
| 1 | +"""Kata - DNA Sequence Tester |
| 2 | +
|
| 3 | +completed at: 2024-05-03 14:01:58 |
| 4 | +by: Jakub Červinka |
| 5 | +
|
| 6 | +DNA is a biomolecule that carries genetic information. It is composed of four different building blocks, called nucleotides: adenine (A), thymine (T), cytosine (C) and guanine (G). Two DNA strands join to form a double helix, whereby the nucleotides of one strand bond to the nucleotides of the other strand at the corresponding positions. The bonding is only possible if the nucleotides are complementary: A always pairs with T, and C always pairs with G. |
| 7 | +
|
| 8 | +Due to the asymmetry of the DNA, every DNA strand has a direction associated with it. The two strands of the double helix run in opposite directions to each other, which we refer to as the 'up-down' and the 'down-up' directions. |
| 9 | +
|
| 10 | +Write a function `checkDNA` that takes in two DNA sequences as strings, and checks if they are fit to form a fully complementary DNA double helix. The function should return a Boolean `true` if they are complementary, and `false` if there is a sequence mismatch (Example 1 below). |
| 11 | +
|
| 12 | +Note: |
| 13 | +
|
| 14 | +- All sequences will be of non-zero length, and consisting only of `A`, `T`, `C` and `G` characters. |
| 15 | +- All sequences **will be given in the up-down direction**. |
| 16 | +- The two sequences to be compared can be of different length. If this is the case and one strand is entirely bonded by the other, and there is no sequence mismatch between the two (Example 2 below), your function should still return `true`. |
| 17 | +- If both strands are only partially bonded (Example 3 below), the function should return `false`. |
| 18 | +
|
| 19 | +Example 1: |
| 20 | +```javascript |
| 21 | +seq1 = 'GTCTTAGTGTAGCTATGCATGC'; // NB up-down |
| 22 | +seq2 = 'GCATGCATAGCTACACTACGAC'; // NB up-down |
| 23 | +
|
| 24 | +checkDNA (seq1, seq2); |
| 25 | +// --> false |
| 26 | +
|
| 27 | +// Because there is a sequence mismatch at position 4: |
| 28 | +// (seq1) up-GTCTTAGTGTAGCTATGCATGC-down |
| 29 | +// ||| |||||||||||||||||| |
| 30 | +// (seq2) down-CAGCATCACATCGATACGTACG-up |
| 31 | +``` |
| 32 | +
|
| 33 | +Example 2: |
| 34 | +
|
| 35 | +```javascript |
| 36 | +seq1 = 'GCGCTGCTAGCTGATCGA'; // NB up-down |
| 37 | +seq2 = 'ACGTACGATCGATCAGCTAGCAGCGCTAC'; // NB up-down |
| 38 | +
|
| 39 | +checkDNA (seq1, seq2); |
| 40 | +// --> true |
| 41 | +
|
| 42 | +// Because one strand is entirely bonded by the other: |
| 43 | +// (seq1) up-GCGCTGCTAGCTGATCGA-down |
| 44 | +// |||||||||||||||||| |
| 45 | +// (seq2) down-CATCGCGACGATCGACTAGCTAGCATGCA-up |
| 46 | +``` |
| 47 | +
|
| 48 | +Example 3: |
| 49 | +
|
| 50 | +```javascript |
| 51 | +seq1 = 'CGATACGAACCCATAATCG'; // NB up-down |
| 52 | +seq2 = 'CTACACCGGCCGATTATGG'; // NB up-down |
| 53 | +
|
| 54 | +checkDNA (seq1, seq2); |
| 55 | +// --> false |
| 56 | +
|
| 57 | +// Because both strands are only partially bonded: |
| 58 | +// (seq1) up-CGATACGAACCCATAATCG-down |
| 59 | +// ||||||||| |
| 60 | +// (seq2) down-GGTATTAGCCGGCCACATC-up |
| 61 | +``` |
| 62 | +
|
| 63 | +--- |
| 64 | +#### If you enjoyed this kata, check out also my other DNA kata: [**Longest Repeated DNA Motif**](http://www.codewars.com/kata/longest-repeated-dna-motif) |
| 65 | +
|
| 66 | +""" |
| 67 | + |
| 68 | +trans = str.maketrans("ACGT", "TGCA") |
| 69 | + |
| 70 | +def check_DNA(s1, s2): |
| 71 | + s1, s2 = sorted((s1, s2), key=len) |
| 72 | + return s1 in s2[::-1].translate(trans) |
0 commit comments