5.7 Exercises
129
gene[i], and a substring from index i up to, but not including j, is created by
gene[i:j].
a) Write a function freq(letter, text) that returns the frequency of the letter
letter in the string text, i.e., the number of occurrences of letter divided by
the length of text. Call the function to determine the frequency of C and G in the
gene string above. Compute the frequency by hand too.
b) Write a function pairs(letter, text) that counts how many times a pair
of the letter letter (e.g., GG) occurs within the string text. Use the function to
determine how many times the pair AA appears in the string gene above. Perform
a manual counting too to check the answer.
c) Write a function mystruct(text) that counts the number of a certain structure
in the string text. The structure is defined as G followed by A or T until a double
GG. Perform a manual search for the structure too to control the computations by
mystruct.
Filename: count_substrings.py.
Remarks You are supposed to solve the tasks using simple programming with loops
and variables. While a) and b) are quite straightforward, c) quickly involves demanding logic. However, there are powerful tools available in Python that can solve the
tasks efficiently in very compact code: a) text.count(letter)/len(text); b)
text.count(letter*2); c) len(re.findall(’G[AT]+?GG’, text)). That is,
there is rich functionality for analysis of text in Python and this is particularly useful
in analysis of gene sequences.
Exercise 5.8: Compute Combinations of Sets
Consider an ID number consisting of two letters and three digits, e.g., RE198. How
many different numbers can we have, and how can a program generate all these
combinations?
If a collection of n things can have m 1 variations of the first thing, m 2 of
the second and so on, the total number of variations of the collection equals
m 1 m 2 · · · m n . In particular, the ID number exemplified above can have 26 · 26 ·
10 · 10 · 10 = 676, 000 variations. To generate all the combinations, we must have
five nested for loops. The first two run over all letters A, B, and so on to Z, while
the next three run over all digits 0, 1, . . . , 9.
To convince yourself about this result, start out with an ID number on the form
A3 where the first part can vary among A, B, and C, and the digit can be among 1,
2, or 3. We must start with A and combine it with 1, 2, and 3, then continue with
B, combined with 1, 2, and 3, and finally combine C with 1, 2, and 3. A double for
loop does the work.
a) In a deck of cards, each card is a combination of a rank and a suit. There are 13
ranks: ace (A), 2, 3, 4, 5, 6, 7, 8, 9, 10, jack (J), queen (Q), king (K), and four
suits: clubs (C), diamonds (D), hearts (H), and spades (S). A typical card may be
D3. Write statements that generate a deck of cards, i.e., all the combinations CA,
C2, C3, and so on to SK.
129
gene[i], and a substring from index i up to, but not including j, is created by
gene[i:j].
a) Write a function freq(letter, text) that returns the frequency of the letter
letter in the string text, i.e., the number of occurrences of letter divided by
the length of text. Call the function to determine the frequency of C and G in the
gene string above. Compute the frequency by hand too.
b) Write a function pairs(letter, text) that counts how many times a pair
of the letter letter (e.g., GG) occurs within the string text. Use the function to
determine how many times the pair AA appears in the string gene above. Perform
a manual counting too to check the answer.
c) Write a function mystruct(text) that counts the number of a certain structure
in the string text. The structure is defined as G followed by A or T until a double
GG. Perform a manual search for the structure too to control the computations by
mystruct.
Filename: count_substrings.py.
Remarks You are supposed to solve the tasks using simple programming with loops
and variables. While a) and b) are quite straightforward, c) quickly involves demanding logic. However, there are powerful tools available in Python that can solve the
tasks efficiently in very compact code: a) text.count(letter)/len(text); b)
text.count(letter*2); c) len(re.findall(’G[AT]+?GG’, text)). That is,
there is rich functionality for analysis of text in Python and this is particularly useful
in analysis of gene sequences.
Exercise 5.8: Compute Combinations of Sets
Consider an ID number consisting of two letters and three digits, e.g., RE198. How
many different numbers can we have, and how can a program generate all these
combinations?
If a collection of n things can have m 1 variations of the first thing, m 2 of
the second and so on, the total number of variations of the collection equals
m 1 m 2 · · · m n . In particular, the ID number exemplified above can have 26 · 26 ·
10 · 10 · 10 = 676, 000 variations. To generate all the combinations, we must have
five nested for loops. The first two run over all letters A, B, and so on to Z, while
the next three run over all digits 0, 1, . . . , 9.
To convince yourself about this result, start out with an ID number on the form
A3 where the first part can vary among A, B, and C, and the digit can be among 1,
2, or 3. We must start with A and combine it with 1, 2, and 3, then continue with
B, combined with 1, 2, and 3, and finally combine C with 1, 2, and 3. A double for
loop does the work.
a) In a deck of cards, each card is a combination of a rank and a suit. There are 13
ranks: ace (A), 2, 3, 4, 5, 6, 7, 8, 9, 10, jack (J), queen (Q), king (K), and four
suits: clubs (C), diamonds (D), hearts (H), and spades (S). A typical card may be
D3. Write statements that generate a deck of cards, i.e., all the combinations CA,
C2, C3, and so on to SK.
