All changes saved to local database
ML - discrete structures
Instructions

DNA sequences are the fundamental blueprints of biological life, represented as long strings of four nucleotide bases: Adenine (A)Thymine (T)Guanine (G), and Cytosine (C). In genomic research, analyzing the frequency of these bases is the critical first step toward understanding genetic traits, identifying mutations, and engineering targeted medical treatments. Your task is to develop a high-performance frequency counter that can process these genetic strands and return a structured summary of their composition.

Methodology

To accurately analyze a genetic sequence (SS) and map its components to a frequency distribution, we follow a three-step data transformation pipeline:

Step 1: Case Normalization Because genomic data can be represented in various formats, the input must be standardized to a single case (Uppercase) to ensure the logic remains case-insensitive.

Snormalized=uppercase(S)S_{normalized} = \text{uppercase}(S)

Step 2: Component Mapping We initialize a frequency map (dictionary) where the keys represent the four unique bases B={A,T,G,C}B = \{A, T, G, C\}. Every base must be present in the map initially, set to zero.

Step 3: Linear Accumulation As we iterate through the sequence, for every character (cc) encountered, we increment the corresponding value in our mapping.

Frequencyc=i=1n1(Si=c)\text{Frequency}_c = \sum_{i=1}^{n} \mathbb{1}(S_i = c)

Implementation

In Python, this is most efficiently handled using a Dictionary. By using a dictionary, we achieve O(n)O(n)time complexity, as we only need to pass through the string once. Your implementation should leverage the .upper() method for standardization and dictionary key-indexing for the count updates.

🛠️ System Note: Standard Python dictionaries maintain the order of insertion, but for this challenge, the specific order of keys does not matter as long as all four bases ('A', 'T', 'G', 'C') are present.

Consider the sequence "AATGCC":

  1. Normalization: "aatgcc" becomes "AATGCC".
  2. Counting:
    • 'A' occurs 2 times.
    • 'T' occurs 1 time.
    • 'G' occurs 1 time.
    • 'C' occurs 2 times.
  3. Result: {"A": 2, "T": 1, "G": 1, "C": 2}

Validation

To ensure your solution passes our automated verification system (==), you must satisfy the following:

  1. Fixed Keys: The dictionary must contain keys for all four bases: "A""T""G", and "C".
  2. Missing Bases: If a base does not appear in the sequence, its value in the dictionary must be 0.
  3. Return Type: The function must return a standard Python dict.

The Challenge

Implement the function analyze_dna(sequence). It must accept a string of genetic material and return a dictionary with the total count of each base.

def analyze_dna(sequence): # Step 1: Normalize string to uppercase # Step 2: Initialize dictionary with A, T, G, C set to 0 # Step 3: Loop through sequence and increment counts # Final Step: Return the dictionary pass
solution.py
Terminal