FASTA is the single most common format in all of genomics, and almost every
bioinformatics task begins by parsing one. The format looks trivial until you meet real files: sequences
wrapped across multiple lines at 60 or 70 characters, headers carrying database identifiers and
descriptions, and blank lines between records. Writing this parser from scratch once — rather than
reaching straight for a library — is what teaches you why the format behaves the way it does.
Statement
You are given a multi-FASTA file on standard input. Each record begins with a header
line starting with >, followed by one or more lines of sequence which must be concatenated together.
For each record, in the order it appears in the file, print one line containing:
<identifier> <sequence length>
The identifier is the header text with the leading > removed, up to but not including the first
space. Ignore any description after that first space.
Input — read from standard input
| Variable | Type | Description |
|---|---|---|
fasta
line 1..n
|
str |
A multi-FASTA formatted block of text
1 <= records <= 50, total input <= 20000 characters
|
These variables are already read for you in the starter code on the right.
Output
str one line per record, identifier and concatenated sequence length separated by a single space
Sample Cases
>seq1 first sequence
ACGTACGTAC
GTACGT
>seq2
TTTT
seq1 16
seq2 4
>only
ACGT
only 4
Submit also runs your code against 4 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 <= number of records <= 50- Sequence lines may be wrapped at any width and may vary within a single record
- Blank lines may appear between records and must be ignored
- Identifiers contain no spaces; any text after the first space in a header is a description
Further Reading
- Accumulate sequence lines into a buffer and flush it when you hit the next
>or end of input. - Do not forget to emit the final record after the loop ends — this is the most common bug.
header[1:].split()[0]extracts the identifier cleanly in Python.