Sequence Statistics with Comprehensions

Intermediate Python Foundations Python List Comprehensions Aggregation Formatting
Significance:

Comprehensions let you express a filter-and-transform in one readable line instead of
a four-line loop, and idiomatic Python for data work leans on them heavily. Computing summary statistics
over a batch of sequences is exactly the shape of problem they were designed for, and learning to write
them well is the difference between code a collaborator can read and code they cannot.

Statement

You are given a set of DNA sequences.

Compute and print, each on its own line:

  1. Count: <number of sequences>
  2. Total: <total bases across all sequences>
  3. Mean: <mean sequence length to two decimal places>
  4. Longest: <length of the longest sequence>
  5. GC-rich: <number of sequences with GC content strictly above 50 percent>

GC content is the fraction of G and C bases in a sequence.

Input — read from standard input
Variable Type Description
n
line 1
int The number of sequences
1 <= n <= 200
seqs
line 2..n+1
str One DNA sequence per line
1 <= len(seq) <= 1000, uppercase A, C, G, T only

These variables are already read for you in the starter code on the right.

Output

str five labelled statistic lines in the stated order

Sample Cases
Sample 1
Input
3
ACGTACGT
GGGCCC
AAATTT
Expected Output
Count: 3
Total: 20
Mean: 6.67
Longest: 8
GC-rich: 1
Three sequences of differing length; only the all-GC one exceeds fifty percent GC.
Sample 2
Input
1
GCGC
Expected Output
Count: 1
Total: 4
Mean: 4.00
Longest: 4
GC-rich: 1
A single sequence that is one hundred percent GC.

Submit also runs your code against 3 hidden test cases. Hidden inputs are never shown — if one fails you'll get its number and a description of the mismatch, not the data.

Constraints
  • 1 <= n <= 200
  • Labels must match exactly, with a single space after each colon
  • Mean is printed to exactly two decimal places
  • The GC-rich test is strictly greater than 0.5, so a sequence at exactly 50 percent does not count
Further Reading
  • [len(s) for s in seqs] gives you the lengths list that three of the five statistics need.
  • sum(1 for s in seqs if gc(s) > 0.5) counts without building an intermediate list.
  • Compute GC as (s.count('G') + s.count('C')) / len(s) — no rounding before the comparison.

My Notes
Log in to save personal notes.
Console output will appear here when you click Run Code or Submit...
Expected: n (int), seqs (remaining lines)
Next Problem
Validating Sequence Records