8.5. Exercises#
Each exercise carries the difficulty level indication:
[*] Easy
[**] Moderate
[***] Advanced
The highest difficulty level is meant for students who wish to challenge themselves and/or have previous experience in programming.
Exercise 8.11 ([*] Boolean logic)
In this set of exercises, we will combine basic Boolean operators to define when certain biological events are allowed to take place.
Exercise A: Cell cycle G1 checkpoint
Define 3 variables - DNA_damaged, E2F_present, and not_touching - and assign Boolean values to them. The statement can_pass_g1 should be true if there is enough space, no DNA damage, and no E2F proteins. You can try to assign different values to the three variables and see how these affect the outcome (can_pass_g1).
Use this as a guideline: DNA_damaged - true if damaged E2F_present - true if present not_touching - true if it is not overcrowded
# Your code here
can_pass_g1 = # Your code here
Exercise B: Cell cycle G2-M checkpoint
Define 2 variables - DNA_damaged and cyclinB_present - and assign Boolean values to them. The statement can_pass_g2m should read true if there is no DNA damage and if cyclinB is present. You can try to assign different values to the two variables and see how these affect the outcome (is the cell allowed to pass the cell cycle checkpoint).
Use this as a guideline: DNA_damaged - true if damaged cyclinB_present - true if present
# Your code here
can_pass_g2m = # Your code here
Exercise C: Enzyme production
Define 2 Boolean variables - MetA_present and MetB_present - and assign value to them. Based on these variables, define make_enzyme. The cell should start to produce the enzyme if metabolite A or metabolite B is present, but not both. How does changing values of MetA_present or MetB_present affect the outcome?
Use this as a guideline: MetA_present - true if present MetB_present - true if present
# Your code here
make_enzyme = # Your code here
Exercise D: (Bonus) Cancer
Rewrite Exercise A so that the behaviour would imitate that of cancer.
# Your code here
Exercise 8.12 ([*] Logical statements: debugging)
Exercise A: Predict True or False
For the 3 pieces of code below, predict whether they will return True or False without running the code. To check whether your prediction is correct, run the code with the needed print() statements.
# Code 1
gfp_prot_seq = ""
pred1 = bool(gfp_prot_seq)
# Your prediction for pred1 = True or False?
# Code 2
gfp_prot_seq = "MSKGEELFTGVVPILVELDGDVNGHKFSVSGEGEGDATYGKLTLKFICTTGKLPVPWPTLVTTFSYGVQCFSRYPDHMKQHDFFKSAMPEGYVQERTIFFKDDGNYKTRAEVKFEGDTLVNRIELKGIDFKEDGNILGHKLEYNYNSHNVYIMADKQKNGIKVNFKIRHNIEDGSVQLADHYQQNTPIGDGPVLLPDNHYLSTQSALSKDPNEKRDHMVLLEFVTAAGITHGMDELYK"
pred2 = bool(gfp_prot_seq)
# Your prediction for pred2 = True or False?
# Code 3
animal = "rabbit"
mammals = ["giraffe", "elephant", "lion", "rabbit", "guinea pig", "vole", "shrew", "human", "cat", "dog"]
reptiles = ["crocodile", "iguana", "alligator", "boa", "cobra", "python", "anole", "turtle", "tortoise", "tuatara"]
pred3 = animal in mammals
# Your prediction for pred3 = True or False?
Exercise B: Code debugging
Statements in each of the 3 pieces of code below should be true. Understand why they are not, and fix the code.
# Code 1
x = 1.4 * 1.5
print(f"Is x = 2.1? {bool(x == 2.1)}")
# Code 2
y = 'a' + 7
test_ar = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
print(f"Is y in the test_ar? {bool(y in test_ar)}")
# Code 3
test_ar = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
z = 7
print(f"Is z longer than the test array? {bool(z > len(test_ar))}")
Exercise 8.13 ([**] Lamp: debugging)
The code below is the implementation of the algorithm shown in Lamp_algorithm.png.
However, both logical and syntax bugs are introduced into the code. Try to fix them all and make a functional code implementation of the outlined algorithm.
Hint: there are six bugs.
lamp_working = "yes"
plugged_in = "no"
burned_bulb = "yes"
if lamp_works == no:
if plugged_in == "no":
print("Plug it in!")
else:
if burned_bulb = "yes":
print("Buy a new lamp :(")
else:
print("Replace the light bulb!)
else:
print("All is good :)")
Exercise 8.14 ([*] DNA sequence and if/else/elif)
DNA Sequence and If/Else/Elif
This set of exercises is meant to give practice with using the if, else, and elif in Python.
These will be done on DNA sequences, or rather strings that represent DNA sequences. This may be helpful in the future if bioinformatics.
Exercise: Pseudocode
For each of the exercises below, start by writing pseudocode on a piece of paper. Only once you’re done with pseudocode should you start working on Python code in VS Code.
Exercise A: DNA subsequence
Write an if statement to check if a DNA subsequence is in a longer sequence. Store the outcome in Boolean variable in_seq and print the value of in_seq. You do not need to use any loops.
You can define DNA_subsequence on your own. Check that your code works both for subsequences that are and are not contained in the sequence.
DNA = "GGGACATATGCTCCGATTAAAGCGCATGATAGCCCGATAGGTAGATAGGGGTTCACGACG"
DNA_subsequence = # Your code here
# Your code here
Exercise B: DNA subsequence II
Repeat the above, but use also an else statement to set in_seq to false if the DNA subsequence is not contained in the sequence. You do not need to use any loops.
You can define DNA_subsequence on your own. Check that your code works both for subsequences that are and are not contained in the sequence.
DNA = "GGGACATATGCTCCGATTAAAGCGCATGATAGCCCGATAGGTAGATAGGGGTTCACGACG"
DNA_subsequence = # Your code here
# Your code here
Exercise C: Handling multiple sequences
What if we want to check for presence of 2 different subsequences in our DNA sequence? Write code that informs the user through print statements whether subsequence_1, subsequence_2, none or both of them are present in the DNA sequence. You do not need to use any loops.
DNA = "GGGACATATGCTCCGATTAAAGCGCATGATAGCCCGATAGGTAGATAGGGGTTCACGACG"
DNA_subsequence_1 = "ATGCTCCG"
DNA_subsequence_2 = "TAGGTAGATATGGGTTC"
# Your code here
Exercise 8.15 ([**] DNA and RNA)
DNA is a double-stranded molecule containing two complementary strands. To make an mRNA, a part of a DNA strand is transcribed. The resulting mRNA has nucleotides complementary to the DNA template (note: RNA contains uracil instead of thymine).
Exercise: Pseudocode
For each of the exercises below, start by writing pseudocode on a piece of paper. Only once you’re done with pseudocode should you start working on Python code in VS Code.
Exercise A: DNA strands
Find the strand complementary to the given DNA strand. Note that the input DNA string may not be only made of A, T, G, and C. The program should be able to handle this: represent a failed match as an X.
Store the output string in DNA_complement variable.
DNA = "GPAGAGATTTYGTGACAGCATABGACGAGATTTTAGATACCCGATATATAGAHCG"
# Your code here
Exercise B: Transcription
Transcribe the given DNA sequence into an mRNA sequence. Note that the input DNA string may not be only made of A, T, G, and C. The program should be able to handle this: represent a failed match as an X.
Store the output string in mRNA variable.
DNA = "GPAGAGATTTYGTGACAGCATABGACGAGATTTTAGATACCCGATATATAGAHCG"
# Your code here
Exercise C (Bonus): Match-case
Do the same as in Exercise B, but use match-case.
DNA = "GPAGAGATTTYGTGACAGCATABGACGAGATTTTAGATACCCGATATATAGAHCG"
# Your code here
Exercise 8.16 ([**] Green fluorescent protein (GFP))
According to the central dogma in molecular biology, genetic information in biological systems flows from DNA to RNA to protein. To make an mRNA, a part of a DNA strand is transcribed. The resulting mRNA has nucleotides complementary to the DNA template (note: RNA contains uracil instead of thymine). To produce a protein, ribosomes then use codons (sets of 3 nucleotides) on mRNA, which are matched with their complementary anticodones on aminoacid-carrying tRNAs.
Let’s go through the process of a) transcription, b) translation, and c) checking for mutations in GFP, everyone’s favourite marker protein.
Exercise: Pseudocode
For each of the exercises below, start by writing pseudocode on a piece of paper. Only once you’re done with pseudocode should you start working on Python code in VS Code.
Exercise A: GFP transcription
Write a for loop that will take the string GFP_DNA and convert it to the complementary RNA sequence. Put it into a string called GFP_RNA. You do not have to worry about reversing the order. You may assume the input is all capital letters and contains only A, T, C, and G. Return the GFP_RNA string as capital letters as well.
GFP_DNA = 'ATGAGTAAAGGCGAAGAGCTGTTCACTGGTGTCGTCCCTATTCTGGTGGAACTGGATGGTGATGTCA'\
+ 'ACGGTCATAAGTTTTCCGTGCGTGGCGAGGGTGAAGGTGACGCAACTAATGGTAAACTGACGCTGAAGTTCATC'\
+ 'TGTACTACTGGTAAACTGCCGGTACCTTGGCCGACTCTGGTAACGACGCTGACTTATGGTGTTCAGTGCTTTTC'\
+ 'TCGTTATCCGGACCATATGAAGCGACATGACTTCTTCAAGTCCGCCATGCCGGAAGGCTATGTGCAGGAACGCA'\
+ 'CGATTTCCTTTAAGGATGACGGCACGTACAAAACGCGTGCGGAAGTGAAATTTGAAGGCGATACCCTGGTAAAC'\
+ 'CGCATTGAGCTGAAAGGCATTGACTTTAAAGAAGACGGCAATATCCTGGGCCATAAGCTGGAATACAATTTTAA'\
+ 'CAGCCACAATGTTTACATCACCGCCGATAAACAAAAAAATGGCATTAAAGCGAATTTTAAAATTCGCCACAACG'\
+ 'TGGAGGATGGCAGCGTGCAGCTGGCTGATCACTACCAGCAAAACACTCCAATCGGTGATGGTCCTGTTCTGCTG'\
+ 'CCAGACAATCACTATCTGAGCTACCAAAGCGTTCTGTCTAAAGATCCGAACGAGAAACGCGATCATATGGTTCT'\
+ 'GCTGGAGTTCGTAACCGCAGCGGGCATCACGCATGGTATGGATGAACTGTACAAATAA'
# Your code here
Exercise B: GFP translation
Write a for loop that will take the output string GFP_RNA from Exercise A and return the protein code GFP_protein (as capitalized one letter amino acid codes). You cannot assume that the coding sequence is aligned with the start of your sequence (i.e., there may be nucleotides in the RNA sequence before the start codon).
Use standard RNA codon table from: https://en.wikipedia.org/wiki/DNA_and_RNA_codon_tables
# Your code here
Exercise C: GFP mutations
After trying to grow the cells with the protein you just translated, the cells were yellow. Turns out the colleagues who gave us the GFP actually gave us a GFP variant.
Write a code that gives a list of the amino acid mutations written as XNY, where X is the original amino acid, N is the position of the mutation, and Y is the new amino acid. Example: Mutation of K to G at position 55 is written as: K55G
As input you have:
GFP_protein - your sequence from the previous section
GFP_reference - a reference sequence
You may assume the sequences are the same length.
GFP_reference = 'MSKGEELFTGVVPILVELDGDVNGHKFSVSGEGEGDATYGKLTLKFICTTGKLPVPWPTL'\
+ 'VTTFSYGVQCFSRYPDHMKQHDFFKSAMPEGYVQERTIFFKDDGNYKTRAEVKFEGDTLV'\
+ 'NRIELKGIDFKEDGNILGHKLEYNYNSHNVYIMADKQKNGIKVNFKIRHNIEDGSVQLAD'\
+ 'HYQQNTPIGDGPVLLPDNHYLSTQSALSKDPNEKRDHMVLLEFVTAAGITHGMDELYK'
# Your code here
Exercise 8.17 ([**] Bacterial population modelling)
Bacterial population modelling
You are using 2 different types of bacterial populations to potentially use for pharmaceutical synthesis. You want to find out which population is growing the fastest. You have decided that the best way to find this out is by doing some simulations of the growth.
Note: ‘break’ and ‘continue’ are not necessarily required to solve the two exercises.
Exercise: Pseudocode
For each of the exercises below, start by writing pseudocode on a piece of paper. Only once you’re done with pseudocode should you start working on Python code in VS Code.
Exercise A: Which strain grows faster?
Write code in which you follow the growth of two bacterial populations, A and B.
We use the following information: initial_population - initial population of cells division_t - division time in minutes
If the population of B ever becomes greater than population of A, set is_B_faster to ‘True’. If not, set is_B_faster to ‘False’.
The maximum time (max_time) you have to simulate is 7 days.
initial_population_A = 120000 # cells
initial_population_B = 241000 # cells
division_t_A = 45 # min
division_t_B = 35 # min
# Your code here
Exercise B: Delayed division start
Do the same as in Exercise A (you can also reuse the code you just wrote), but this time also account for the time before the cells start dividing (start_t), which is different for two bacterial populations.
start_t_A = 700 # min
start_t_B = 1200 # min
# Your code here
Exercise C: Race to 1 million cells
Repeat the same idea as in Exercise B, trying to find the fastest growing strain. This time, however, you are given tuples corresponding to bacterial strains. Each tuple consists of the following: (initial_population, division_t, start_t).
Return the name of the fastest growing strain in ‘best_strain’, which is defined as the quickest strain to reach 1,000,000 cells.
strain_A = (100000, 30, 1000)
strain_B = (120000, 40, 800)
strain_C = (90000, 35, 500)
# Your code here
Exercise 8.18 ([**] Number guess game)
Imagine a number guessing game, where one person chooses a number (let’s say 31) and the other tries to guess it:
Question: “Is it 25?” Answer: “No, it is higher.” Question: “Is it 37? Answer: “No, it is lower.” Question: “Is it 33?”
Let’s implement this game in Python:
Define a number between 1 and 100.
Ask user to guess it via
input()function.After each guess, you have to say whether the answer is correct, or higher or lower than the guess.
The game finishes when the guess is correct.
# Your code here
Exercise 8.19 ([**] CRISPR implementation)
Fig. 8.1 CRISPR-Cas9 (figure by Marius Walter, figure source, figure license CC BY-SA 4.0).#
In this exercise you will implement a basic version of CRISPR-Cas9.
Cas9 protein contains a small piece of RNA called guide RNA (gRNA). This gRNA is used to scan a DNA sequence. If DNA sequence complementary to gRNA is found AND if there is a small protospace adjacent motif (PAM) immediately following the matched sequence, Cas9 will cleave the DNA 4 nucleotides before the PAM.
Replicate this behavior in Python code.
We will assume that the PAM sequence is NGG, where N stands for any nucleotide (A, C, T, or G). The last two nucleotides are G (guanine).
Inputs:
The DNA sequence to examine
The gRNA sequence to match against the DNA
Both sequences will be given in the 5’ to 3’ direction, the RNA is the complement to the DNA.
Output:
A list containing the sequence after it has been examined by Cas9
E.g., if a sequence was originally ‘ATTGGCC’ and cleaved after ATT, the list looks like this: [‘ATT’, ‘GGCC’].
Notes/Hints:
Start by writing pseudocode on a piece of paper. Only once you’re done with pseudocode should you start working on Python code in VS Code.
The input sequence may have more than 1 cleavage site, or no sites.
The input sequence and guide RNA are reverse complements.
There may be matches with the guide RNA, but not containing the PAM. These should not be cleaved.
You may find it useful to comment out the given DNA and RNA inputs and use your own “toy examples” while working on the code to test whether pieces of your code work.
replacefunction may be useful (see more at: https://www.w3schools.com/python/ref_string_replace.asp)
DNA_seq = "CTTTACGACAAGAGGTACGCTTTGTGAGCGCGCTCCGCCGGCCCTCGCGGACCCTTGCTACAAGCT\
CGACAGGATGTCTCCTCAGCATGTCCTACGTTGGAGGACCGCAACAGCGCCTGGCGGTGCGCACACAGCGGTC\
GACCGATTTGCGGAGTCCCAGGCCCGGCCGATCCCGTCCCGTACCCTGGTGGACCGTCTAGGGGTCACCTGCC\
CCTCCGCGATGATTAGATTGCCAGCCCGTAGCGTTTGCGGGTCGCCTACGCGGTCTCTCGGCTGCGCCGGGGA\
TTCGAGCTAGGCCTTTGTAGGCCTGCCGCTGCACAAGGGCCAAAACTCTAACCCTCCCGTTAGCGGGGCGTAC\
CTGGGAGAGGCCATGCCCACGAATGCGCGCCCCGCGGTTGCCTTTTGGGGGCGGGGTCGGCCCTCACGCCGGG\
GCCCCGTCGGCGCGTCTCTGCGACAAAGGACACCGCGCTGAGCCAAAAGGCTGGCGAGGAACCCGGGAAAAGG\
GCCCTGGCCCGGTACAGCCCGGGGCGGGGGCTACCCGGGCCTGGAGACCAAATTCGACCACGACCTAGGCCCG\
ACGCGCGGGGCCCATGGCGTAAGCCACAGCAGCTAGAACGCCTTCTCGATAGGCCCACGAATCTGATCGTTGC\
TATAATAGACGACCTGGCCACCTGGGGAGAATTGAGTTGTTCATGCGCCCCCGGGAAAAGGGCCCTGGCCGTG\
TACACTCCGTCTAGCCCGAACCGCGAGCCCCACACCCGACCTCGACATGGCCTGGTTGGGTGGGCGGGCGACG\
GGCGTAGGCCGACCACCGCCGTGGCTGAGACGGCCCCAGAAGAACGTCGCGGGTCAGGCGTTTGAGTGTAACG\
AGGAACGCTCTTGGTCCACTTGGGGGTTCCCAACAGGTCCAGCGCCCGAGGTGTCGAGATACTGGGGCGCGCG\
TGGTTGCGGCTACTGGTATATGGATGCACTGGGAGTCCTCTTTGGCCCGGAATGGCTGCACAGCTAACTGGGG\
GCTCTTGCCCCCATCGGAGCGCGGGAACGCGCCACCGCTGGCA"
gRNA_seq = "GGGCCCUUUUCCCGGG"
# Your code here
Exercise 8.20 ([**] Nanobots)
Simulate the growth of a population of nanobots in a Petri dish. It starts with an initial population of nanobots which have a specific growth rate (e.g., 1.5 per generation). The simulation runs for select number of generations, and at each generation, the simulation calculates the new population size by multiplying the current population by the growth rate. It then prints the current generation number and the population size. When the simulation is done, print “Nanobot population simulation complete.”.
Implement also the overpopulation condition for the Petri dish: if the population reaches 1 million, all nanobots die. Print a warning at which generation this happened.
If the starting population is zero or below, print a warning.
Try to experiment with different initial parameters.
# Initial population of nanobots
population = 10
# Growth rate (per generation)
growth_rate = 1.5
# Number of generations
generations = 5
# Your code here
Exercise 8.21 ([***] Roman numerals)
Write Python code which transfers decimal numerals into Roman numerals: I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, M = 1000
In Roman numerals, it is not allowed to have 4 identical characters consecutively. For example, if we want to convert 41 into Roman numerals, XXXI is wrong, and XLI is right (think of XLI as 50-10+1).
Exercise A: Use a random user input between 1 and 4000 and automatically rewrite it into Roman numerals.
In this part of the exercise, 1249 = MCCXXXXVIIII is allowed, so it is OK to have 4 consecutive identical characters.
# Your code here
Exercise B:
Adapt the code in order to check for 4 consecutive identical characters and correct the number, such that e.g. 1209 becomes MCCIX. The largest number is 3999, corresponding to MMMCMXCIX.
# Your code here