Filter Sequences by Length

Foundational Python Foundations Python Lists Loops Conditionals
Significance:

Length filtering is the first thing every read-processing pipeline does. Reads shorter
than some cutoff carry too little information to map uniquely, so they are discarded before alignment.
The pattern here — loop over a collection, test a condition, keep what passes — is the single most
reused control structure in all of data analysis.

Statement

You are given a minimum length and a list of DNA sequences.

Print every sequence whose length is greater than or equal to the minimum, one per line, preserving
the order they appeared in the input.

On the final line, print how many sequences were discarded.

Input — read from standard input
Variable Type Description
min_len
line 1
int The minimum sequence length to keep
1 <= min_len <= 1000
n
line 2
int The number of sequences that follow
1 <= n <= 200
seqs
line 3..n+2
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 the passing sequences one per line in input order, then a final line with the discarded count

Sample Cases
Sample 1
Input
5
4
ACGTACGT
ACG
AAAAA
TT
Expected Output
ACGTACGT
AAAAA
2
Two sequences meet the five-base minimum; the other two are discarded.
Sample 2
Input
3
2
ACG
AC
Expected Output
ACG
1
The boundary case: a three-base sequence is kept because the test is >=.

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
  • The comparison is >=, so a sequence exactly at the minimum length is kept
  • 1 <= n <= 200
  • Output order must match input order
  • If no sequences pass, print only the discarded count line
Further Reading
  • Read the two integers first, then loop n times to collect the sequences.
  • A single counter for discards is simpler than computing n - len(kept) afterwards, though either works.
  • Remember that >= and > give different answers on the boundary case.

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