{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercises: structuring code\n", "\n", "## Exercise 2.1 \n", "Write a function that takes as argument a number, and returns it divided by 2 if it is even, and unchanged if it is not." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "## Exercise 2.2\n", "Consider the following string :\n", "\n", "`\"Brave Sir Robin ran away. Bravely ran away away. When danger reared it's ugly head, he bravely turned his tail and fled. Brave Sir Robin turned about and gallantly he chickened out...\"`\n", "\n", "Build a dictionnary whose keys are characters, and whose values correspond to the number of time the character is present in the string." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "## Exercise 2.3\n", "Write a function that returns the reverse complement of a string containing a DNA sequence. \n", "Test your function with the sequence `ATAGAGCGATCGATCCCTAGCTA`, compare your results with what you can get using this online tool." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "## Exercise 2.4\n", "Write a function that accepts a DNA sequence as an argument and returns its %GC content." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "## Exercise 2.5\n", "A little bit of a puzzle:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "def mysterious_function(n):\n", " if n <= 1:\n", " return 1\n", " return n * mysterious_function(n - 1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. What does `mysterious_function` do?\n", "2. Write a function that gives the same result, but using a `for` loop." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "## Exercise 2.6\n", "The Collatz conjecture describes the following procedure:\n", "* Start with any positive integer `n`. \n", "* Then each term is obtained from the previous term as follows: if the previous term is even, the next term is one half of the previous term. If the previous term is odd, the next term is 3 times the previous term plus 1. \n", "\n", "The conjecture is that no matter the value of `n`, the sequence will always reach 1.\n", "1. Write a function that takes an integer `n` as argument and returns the number of steps it took to reach `1` following the described procedure.\n", "\n", "> You can test your function by checking that `13` leads to the following sequence of size 10:\n", ">\n", "> 13 -> 40 -> 20 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1\n", "\n", "2. How would you account for a case where the conjecture is false (i.e., the sequence never reaches 1) ?\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "## Exercise 2.7\n", "Write a function that takes a string of mixed letters and digits, and returns a list of the words (groups of letters) and numbers (group of digits) of the string. \n", "For instance, the string `\"Nobody0expects42the2048Spanish1492Inquisition!\"` \n", "should give `[ \"Nobody\" , 0 , \"expects\" , 42 , \"the\" , 2048 , \"Spanish\" , 1492 , \"Inquisition!\" ]`\n", "\n", "> This exercise can be a tad harder than what it looks like. Don't despair and take your time; you can make it !" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.5" } }, "nbformat": 4, "nbformat_minor": 4 }