Complementary Strand in a DNA---Difficulty: 660---Python Solution

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 with T (Thymine)
  • T (Thymine) pairs with A (Adenine)
  • C (Cytosine) pairs with G (Guanine)
  • G (Guanine) pairs with C (Cytosine)

Code Breakdown

python

Copy

T = int(input())  # Read the number of test cases
  1. 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
  1. for _ in range(T)::
    This is a loop that runs T 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)
  1. n = int(input()):
    Here, the program asks the user to input the length of the DNA string. Although we don’t actually use the value n in our logic, it’s part of the problem’s format (it tells us how long the string s will be).

python

Copy

    s = input()  # Read the DNA sequence string
  1. s = input():
    The program then asks for the DNA sequence string itself, which consists of the characters A, T, C, and G. 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
  1. x = {'A':'T', 'T':'A', 'C':'G', 'G':'C'}:
    This line creates a dictionary called x where:
  • A is mapped to T (A pairs with T)
  • T is mapped to A (T pairs with A)
  • C is mapped to G (C pairs with G)
  • G is mapped to C (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
  1. new = '':
    Here, we initialize an empty string called new. This string will hold the complementary DNA strand after we process each character in s.

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
  1. for string in s::
    This loop goes through each character (nucleotide) in the DNA sequence s. The variable string will take on each character one by one from the string s.
  2. new += x[string]:
    For each nucleotide string in s, we look it up in the dictionary x to get the complementary base. We then append (add) that complementary base to the string new.For example, if string is 'A', then x['A'] gives us 'T'. This 'T' is added to the string new.

python

Copy

    print(new)  # Output the complementary strand for this test case
  1. print(new):
    After the loop has processed all the nucleotides in s, we print the resulting complementary strand new.

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:
      • AT
      • TA
      • CG
      • GC
    • So, the complementary strand is TAGC.
  • For the second test case:
    • The input DNA sequence is AAAAA.
    • The complementary strand is:
      • AT (repeated 5 times)
    • So, the complementary strand is TTTTT.

Output:

nginx

Copy

TAGC
TTTTT

Summary:

  1. Input: We first read the number of test cases. Then for each test case, we read the DNA sequence.
  2. Process: For each nucleotide in the sequence, we find its complementary base using a dictionary.
  3. 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.