{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": true, "deletable": true, "editable": true }, "source": [ "## IF-FOR-WHILE revisited\n", "\n", "### 1. IF\n" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false, "deletable": true, "editable": true }, "source": [ "A. Define a function that takes a name and a phonebook as input parameters and prints the phone number for a given name if the name is listed in the phonebook.\n", "\n", "```python\n", "phonebook = {'Mara': 491761132347,\n", " 'Daniel': 491761262348,\n", " 'Stefan': None,\n", " 'Kate': 491734263358,\n", " 'Ana': None,\n", " 'Joerg': 4917756708022,\n", " 'Marc': 4917822451089}\n", "\n", "def get_number(name,phonebook):\n", "\t# implement the code here\n", "\n", "get_number(\"Kate\", phonebook)\n", "get_number(\"Katharina\", phonebook)\n", "\n", "```\n", "Output\n", "```python\n", "Number of Kate is 491734263358\n", "No entry named Katharina\n", "```" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "B. Define a function that takes a name and a phonebook as input parameters and prints the phone number for a given name if the name is listed in the phonebook with existing number.\n", "```python\n", "phonebook = {'Mara': 491761132347,\n", " 'Daniel': 491761262348,\n", " 'Stefan': None,\n", " 'Kate': 491734263358,\n", " 'Ana': None,\n", " 'Joerg': 4917756708022,\n", " 'Marc': 4917822451089}\n", "\n", "def get_number(name,phonebook):\n", "\t# implement the code here\n", "\n", "get_number(\"Kate\", phonebook)\n", "get_number(\"Katharina\", phonebook)\n", "get_number(\"Ana\", phonebook)\n", "\n", "```\n", "Output\n", "```python\n", "Number of Kate is 491734263358\n", "No entry named Katharina\n", "Missing number for entry Ana\n", "```" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "### 2. FOR" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false, "deletable": true, "editable": true }, "source": [ "C. Define a function that for a given phonebook as an input parameter, displays of phonebook's content by name and reports the missing number cases.\n", "```python\n", "phonebook = {'Mara': 491761132347,\n", " 'Daniel': 491761262348,\n", " 'Stefan': None,\n", " 'Kate': 491734263358,\n", " 'Ana': None,\n", " 'Joerg': 4917756708022,\n", " 'Marc': 4917822451089}\n", "\n", "def display(phonebook):\n", "\t# implement the code here\n", "\n", "display(phonebook)\n", "\n", "```\n", "Output\n", "```python\n", "Marc: 4917822451089\n", "Ana: missing number\n", "Daniel: 491761262348\n", "Kate: 491734263358\n", "Joerg: 4917756708022\n", "Mara: 491761132347\n", "Stefan: missing number\n", "\n", "Found 2 cases with missing numbers.\n", "```" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false, "deletable": true, "editable": true }, "source": [ "D. Define a function that takes a stop_name and a phonebook as an input parameters and displays the phonebook's content by name until it reaches the entry named stop_name.\n", "```python\n", "phonebook = {'Mara': 491761132347,\n", " 'Daniel': 491761262348,\n", " 'Stefan': None,\n", " 'Kate': 491734263358,\n", " 'Ana': None,\n", " 'Joerg': 4917756708022,\n", " 'Marc': 4917822451089}\n", "\n", "def display(stop_name, phonebook):\n", "\t# implement the code here\n", "\n", "display(\"Kate\", phonebook)\n", "display(\"Katharina\", phonebook)\n", "display(\"Katharina\", {})\n", "\n", "```\n", "Output\n", "```python\n", "Marc:4917822451089\n", "Missing number for entry Ana\n", "Daniel:491761262348\n", "Kate:491734263358\n", "Found entry named Kate - stop printing\n", "\n", "Marc:4917822451089\n", "Missing number for entry Ana\n", "Daniel:491761262348\n", "Kate:491734263358\n", "Joerg:4917756708022\n", "Mara:491761132347\n", "Missing number for entry Stefan\n", "Full phonebook displayed, no entry named Katharina found.\n", "\n", "Empty phonebook, nothing to display.\n", "\n", "```" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false, "deletable": true, "editable": true }, "source": [ "E. Define a function that calculates similarity of proteins based on their expression across tissues.The function accepts a dictionary of protein names as a single input parameter. Each protein name P is associated with set of tissues TP in which the protein is highly expressed. Calculate the similarity between two proteins P (with tissue set TP) and Q (with tissue set TQ) as the ratio of |overlap(TP, TQ)|/|union(TP,TQ)|, with |X| means size of the set X. Report for each protein pair the ratio in percent with 2 signifiant digits after the decimal point.\n", "\n", "```python\n", "proteins = {'P1': {'liver', 'kidney', 'blood'},\n", " 'P2': {'liver', 'blood'},\n", " 'P3': {'bone', 'muscle'},\n", " 'P4': {'brain', 'blood'}}\n", "\n", "\n", "def similarity(proteins):\n", "\t# implement the code here\n", " \n", "similarity(proteins)\n", "```\n", "Output\n", "```python\n", "P1 P2 66.67\n", "P1 P3 0.0\n", "P1 P4 25.0\n", "P2 P3 0.0\n", "P2 P4 33.33\n", "P3 P4 0.0\n", "```\n", "E.1.Think how to use nested loops, without using any if statement and still make only 6 prints for the given dictionary 'proteins'. Tipp: check the ranges." ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "### 3. WHILE" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false, "deletable": true, "editable": true }, "source": [ "F. Find a non-negative integer number N such that sum of the first N non-negative integers is equal to given integer number S , e.g. 28\n", "\n", "Output\n", "```python\n", "In loop N = 1 SUM = 1\n", "In loop N = 2 SUM = 3\n", "In loop N = 3 SUM = 6\n", "In loop N = 4 SUM = 10\n", "In loop N = 5 SUM = 15\n", "In loop N = 6 SUM = 21\n", "In loop N = 7 SUM = 28\n", "The sequence found is \n", "[0, 1, 2, 3, 4, 5, 6, 7]\n", "The sum of the sequence elements is\n", "28\n", "```\n", "F.1. Carefully set the loop termination condition to avoid infinite loop if there is no such number N, e.g. when S is 30.\n", "Output\n", "```python\n", "In loop N = 1 SUM = 1\n", "In loop N = 2 SUM = 3\n", "In loop N = 3 SUM = 6\n", "In loop N = 4 SUM = 10\n", "In loop N = 5 SUM = 15\n", "In loop N = 6 SUM = 21\n", "In loop N = 7 SUM = 28\n", "In loop N = 8 SUM = 36\n", "There is no sequence with sum of its elements equal to 30\n", "```\n", "\n", "F.2. Implement a similiar code that will search for the largest positive integer N, such that the product of the first N positive integers will be equal to a given value P, e.g. 720.\n", "Output\n", "```python\n", "The sequence found is \n", "[1, 2, 3, 4, 5, 6]\n", "The product of the sequence elements is\n", "720\n", "```" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "H. Define a single function that depending on the input parameter flag will search for sequence of first N postive integers such that their sum (in case flag is 'sum') or their product (in case flag is 'prod') will be equal to the value of the second input parameter threshold.\n", "```python\n", "def find_seq(flag, # string with two possible values 'sum' and 'prod' \n", " threshhold # positive integer\n", " ):\n", "{\n", " # Check validity of the function parameters, e.g. flag has to be string with only two possible values\n", " # If at least one check fails print message 'Invalid values passed to function parameters' and exit the function returning None\n", "\n", " # Evaluate the branch of the code corresponding to the flag value\n", "\n", " # Return sequence or empty list if such sequence does not exist\n", "\n", "}\n", "\n", "# Call the function\n", "s = find_seq('sum', 56)\n", "print(s)\n", "p = find_seq('prod', 720)\n", "print(p)\n", "n = find_seq('sum', -1) \n", "n = find_seq(56, 'sum')\n", "```\n", "Result:\n", "```python\n", "[]\n", "[1, 2, 3, 4, 5, 6]\n", "Invalid vaues passed to function arguments\n", "Invalid vaues passed to function arguments\n", "```\n", "\n", "**Note: The order and meaning of parameters in Python functions is important. Always take care what the function returns, especially if the output will be used as input in other functions.**\n" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "## Sources\n", "\n", "https://docs.python.org/3.5/" ] } ], "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.5.2" } }, "nbformat": 4, "nbformat_minor": 0 }