Python: Control Flow
Session 2: Control flow
Content
- Conditionals: if, else, and elif. Break.
- Loops: for, while, range
- Functions: def
Reading
https://docs.python.org/3/tutorial/Chapter 4.
Grading
The lab reports are a training to write scientific texts and an important part of the course. To pass you need to provide a final version of the lab report within 7 days after the lab. You have a chance but it is not mandatory to submit a preliminary version and receive feedback on it. The preliminary version needs to be submitted within 72 hours of the lab and feedback will be provided at the latest 24 hours before the deadline of the assignment. If the final report it not submitted in time or it contains an error you will get an Fx on the lab course. This means that you have to submit an updated lab report within 7 days after the exam and that you will receive an E on the entire course. If this does not occur you will have to re-register for the course next time it is given (normally next year) and complete the missing parts that year and you can still not receive a grade higher than E.
Exercises
Only the mandatory assignments should be handed in. The file name should be yourname_yoursurname_sess2.py. Ignore accents and special characters. Before submitting, please make sure your code is correct and send it to david.menendez.hurtado@scilifelab.se.
Don’t hesitate to ask us to clarify if you any have questions.
Introductory exercises
- Given two integers a and b, compute the sum of all even integers between a and b, inclusively and return it.
- An ancient Chinese puzzle says (it may have been slightly modified):
We count 35 heads and 94 legs among the ducks and the platypuses in a rivers. How many ducks and how many platypuses do we have?
Write a program to solve it by brute force, ie, by trying all possible combinations of ducks and platypuses and see if the number of legs match.
Note that a platypus has four legs.
- Write a function to compute the factorial of a number. (
5! = 5 * 4 * 3 * 2) - Write a function
f(n)that computes the nth Fibonacci number (https://en.wikipedia.org/wiki/Fibonacci_number) using the definition. - Write a function
l(n)that computes the nth Lucas number (https://en.wikipedia.org/wiki/Lucas_number). - Write a function
lucas(n, P, Q)that computes the nth number of a Lucas sequence (https://en.wikipedia.org/wiki/Lucas_sequence). - Same as above, but if P or Q are not provided, it should return the Fibonacci numbers.
Mandatory Assignments
- (
*) Create a function that takes a string of nucleotides as input and returns its GC content as a number between 0 and 1. Call itcompute_gc. - (
*) Make a functiondist(a, b)that takes two DNA sequences of the same length and returns the Hamming distance between them, i.e., the number of symbols at the same position that differ. For example,dist('GGGAT', 'GGCCT') = 2, becauseGA -> CCand everything else remains the same. - (
*) Make a function that takes two characters as input and returns 1 if they are identical and 0 otherwise. Call itscore(a,b) - (
*) Write a function that takes two strings and counts the matches. If the strings are'GGGAT'and'GGCCT'the function should return 3.
Additional Assignments
- Define a function that computes the double factorial (
9!! = 9 * 7 * 5 * 3 * 1;4!! = 4 * 2). It should be calleddouble_fact(n). - Write a function that given a sorted list and an element of that list, finds its position using a binary search: Compare your element with the middle one, if it is equal to your element, return its position. Else, if your element is larger, select the top half, otherwise select the lower half and repeat.Example: find the index of
3in[1, 3, 4, 5, 8].- Look at the middle position:
4 > 3, so we select the lower half:[1, 3, 4]. - Compute the new middle, index 2:
3, we found our value!
- Look at the middle position:
- Following the idea of the previous exercise, implement the bisection method to find the root to the polinomial p(x) = x3 − x2 + x − 1. Hint: it is between 0 and 2.2. The idea behind bisection is to bracket the solution between two values, and iteratively reduce the interval until it is small enough. It works as follows:
- Guess two values, u and d, such as sign(p(u))! = sign(p(d)). Hence, the root lies between u and d.
- Compute the middle point m = (u + d)/2
- Compute p(m). If it has the same sign as p(u), m is our new u. Else, d.
- Repeat until we have cornered our solution enough.
The mathematically inclined may be interested in implementing regula falsi and Newton’s method as well.