Complementary Strand in a DNA
Difficulty: 660
Solution :
T = int(input())
for _ in range(T):
n = int(input())
s = input()
x = {'A': 'T', 'T': 'A', 'C': 'G', 'G': 'C'}
new = ''
for string in s:
new += x[string]
print(new)
Explanation of Code
Objective:
You are given a DNA sequence represented by a string of letters A
, T
, C
, and G
. You need to find the complementary DNA strand based on these rules:
A
(Adenine) pairs withT
(Thymine)T
(Thymine) pairs withA
(Adenine)C
(Cytosine) pairs withG
(Guanine)G
(Guanine) pairs withC
(Cytosine)
Code Breakdown
python
Copy
T = int(input()) # Read the number of test cases
T = int(input())
:
This line asks the user to input the number of test cases. A “test case” is one individual example or scenario where we need to find the complementary strand.
python
Copy
for _ in range(T): # Loop through each test case
for _ in range(T):
:
This is a loop that runsT
times, one time for each test case. We don’t need to use the variable_
here (because we’re not interested in the exact number), so it’s just a placeholder. The loop will execute the code inside it for each test case.
python
Copy
n = int(input()) # Read the length of the string (not used in this code)
n = int(input())
:
Here, the program asks the user to input the length of the DNA string. Although we don’t actually use the valuen
in our logic, it’s part of the problem’s format (it tells us how long the strings
will be).
python
Copy
s = input() # Read the DNA sequence string
s = input()
:
The program then asks for the DNA sequence string itself, which consists of the charactersA
,T
,C
, andG
. This string represents one strand of the DNA.
python
Copy
x = {'A':'T', 'T':'A', 'C':'G', 'G':'C'} # Create a dictionary for complementary bases
x = {'A':'T', 'T':'A', 'C':'G', 'G':'C'}
:
This line creates a dictionary calledx
where:
A
is mapped toT
(A pairs with T)T
is mapped toA
(T pairs with A)C
is mapped toG
(C pairs with G)G
is mapped toC
(G pairs with C)This dictionary helps us easily find the complementary base for each character in the DNA sequence.
python
Copy
new = '' # Initialize an empty string to store the complementary strand
new = ''
:
Here, we initialize an empty string callednew
. This string will hold the complementary DNA strand after we process each character ins
.
python
Copy
for string in s: # Loop through each nucleotide in the input string s
new += x[string] # Find the complementary base and add it to new
for string in s:
:
This loop goes through each character (nucleotide) in the DNA sequences
. The variablestring
will take on each character one by one from the strings
.new += x[string]
:
For each nucleotidestring
ins
, we look it up in the dictionaryx
to get the complementary base. We then append (add) that complementary base to the stringnew
.For example, ifstring
is'A'
, thenx['A']
gives us'T'
. This'T'
is added to the stringnew
.
python
Copy
print(new) # Output the complementary strand for this test case
print(new)
:
After the loop has processed all the nucleotides ins
, we print the resulting complementary strandnew
.
Example:
Input:
Copy
2
4
ATCG
5
AAAAA
- For the first test case:
- The input DNA sequence is
ATCG
. - The complementary strand is found by looking up each character in the dictionary
x
:A
→T
T
→A
C
→G
G
→C
- So, the complementary strand is
TAGC
.
- The input DNA sequence is
- For the second test case:
- The input DNA sequence is
AAAAA
. - The complementary strand is:
A
→T
(repeated 5 times)
- So, the complementary strand is
TTTTT
.
- The input DNA sequence is
Output:
nginx
Copy
TAGC
TTTTT
Summary:
- Input: We first read the number of test cases. Then for each test case, we read the DNA sequence.
- Process: For each nucleotide in the sequence, we find its complementary base using a dictionary.
- Output: After processing each test case, we output the complementary strand.
Key Concepts:
- Dictionary: We used a dictionary to map each nucleotide to its complementary base. This makes it easy to look up the complementary base during the processing of the string.
- Looping through a string: The
for
loop is used to go through each character in the DNA sequence and find its complement. - String manipulation: We build the complementary strand by appending each complement to the string
new
.