Example: says('Hallo') returns

Paolo: \"Hallo\"

written in this way\n", "\n", "Test function **says** with three different input strings, one of them with an apostrophe and one of them with a double quotation inside." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define a function **F** with three parameters which tells the user the values of the parameters and then returns a (anyone, but please not all sums or all multiplications!) mathematical operation on the parameters." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Try function **F** with the parameters in order." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Try function **F** with the same parameters but not in order, giving obviously still the same result." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Consider the function **mystery** here below which uses the function **test** which returns a boolean and for which you do not have any idea of what does it do." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def mystery(a,b):\n", " if test(a):\n", " return b\n", " return a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Which of the following functions returns always the same as the function **mystery**, for every possible function

\"Big\" when x is larger than 200; \n", "

\"Small\" when x is smaller than 100;\n", "

\"Average\" in the other cases. \n", "

Build it first without using Elif and then using Elif.\n", "

Test it!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define function **median** which takes as parameters three numbers and returns the median, i.e. the number in the middle according to numerical order. You may use other functions we built in class, but in this case write them here. You may not use min and max functions.\n", "

Examples: median(1,2,3) returns 2, median(9,3,6) returns 6, median(7,8,7) returns 7.\n", "\n", "Hint: There are a lot of different solutions to this problem. Think how would you do it manually slowly step by step if your brain were able to analyse only two numbers at the same time (and not three, as human brain is able to do). Do it on paper slowly with a practical example, do not try to write the code if you are not able to do it on paper!\n", "\n", "Test the function with the three examples above." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define a function

I hope that at elementary school you studied the rhyme \"30 days has September...\" https://youtu.be/BYshA2ybFdY" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define a function

In case you do not know, the rule for leap years is that a year is leap if it is divisible by 4 (such as 2016), with the exception of years which are divisible by 100 which are not leap (such as 1900) except when divisible by 400 which are leap (for example 2000).\n", "

To check whether a number n is divisible by 4 the trick is n%4==0 (the operator % gives the remainder of the integer division)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Redefine a function