{ "cells": [ { "cell_type": "markdown", "id": "73514e0b-dd70-436e-9ceb-1b7c01e3f27c", "metadata": {}, "source": [ "This notebook illustrates anonymous functions and functions to store data." ] }, { "cell_type": "markdown", "id": "1414c718-3e8c-408a-bede-da6980ca6c04", "metadata": {}, "source": [ "# 1. Anonymous Functions" ] }, { "cell_type": "markdown", "id": "c3fd9069-e9a7-40c2-9716-f57b3174bc25", "metadata": {}, "source": [ "Consider the guessing of a secret number. With `lambda` we can make a function which acts as an *oracle*. The oracle is a function which returns `True` or `False` depending on whether the input of the oracle matches the secret or not." ] }, { "cell_type": "code", "execution_count": 1, "id": "9d3b1a85-c8ca-402a-aa5b-67ce6ba6854a", "metadata": {}, "outputs": [], "source": [ "def make_oracle():\n", " \"\"\"\n", " Returns an oracle for a single digit number.\n", " \"\"\"\n", " from random import randint\n", " nbr = randint(0, 9)\n", " return lambda x: x == nbr" ] }, { "cell_type": "code", "execution_count": 2, "id": "185665fd-0256-4597-a6b3-58932dd11e0a", "metadata": {}, "outputs": [], "source": [ "oracle = make_oracle()" ] }, { "cell_type": "code", "execution_count": 3, "id": "a5796990-cda4-4d01-9677-a963d38099d1", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "function" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(oracle)" ] }, { "cell_type": "code", "execution_count": 4, "id": "96a4cda4-a359-4a06-9e6f-b7131d82c08e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "oracle(0)" ] }, { "cell_type": "code", "execution_count": 5, "id": "91bed94b-8609-4652-91a6-fda192730966", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "oracle(1)" ] }, { "cell_type": "markdown", "id": "246b5e50-7cd1-4b0d-b08c-a4f627d60d8f", "metadata": {}, "source": [ "Without any feedback on whether our guess was too small or too large, the game gets tedious. With a list comprehension we can get the answer quickly." ] }, { "cell_type": "code", "execution_count": 6, "id": "89c45ae4-f229-4e17-abb8-6076eabb75bf", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[False, False, False, False, False, True, False, False, False, False]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L = [oracle(x) for x in range(10)]\n", "L" ] }, { "cell_type": "code", "execution_count": 7, "id": "087c8f5a-d636-4db9-96a6-57a24c4b5197", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "idx = L.index(True)\n", "idx" ] }, { "cell_type": "code", "execution_count": 8, "id": "46e62967-2e72-45e1-8f52-d6c6bfd6cc65", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "oracle(idx)" ] }, { "cell_type": "markdown", "id": "715f939e-3214-4fcb-8e92-211c2173f667", "metadata": {}, "source": [ "# 2. Monte Carlo with List Comprehensions" ] }, { "cell_type": "markdown", "id": "8bab96fc-0bda-45df-b8da-1f5dcbee6bc0", "metadata": {}, "source": [ "A Monte Carlo method to estimate $\\pi$ counts the number of points uniformly generated in a square that are in the unit disk." ] }, { "cell_type": "code", "execution_count": 9, "id": "af45d89a-2d53-40df-a228-03a4e717ea15", "metadata": {}, "outputs": [], "source": [ "from random import uniform as u" ] }, { "cell_type": "code", "execution_count": 10, "id": "19030634-07fd-490c-b503-905cbe1f403b", "metadata": {}, "outputs": [], "source": [ "N = 10**6" ] }, { "cell_type": "code", "execution_count": 11, "id": "d68f7cc3-8e92-4b82-b25f-27f8e8705c8a", "metadata": {}, "outputs": [], "source": [ "X = [u(0, 1) for _ in range(N)]" ] }, { "cell_type": "code", "execution_count": 12, "id": "fc6cdae7-e679-486f-b898-e28e4ced45ec", "metadata": {}, "outputs": [], "source": [ "Y = [u(0, 1) for _ in range(N)]" ] }, { "cell_type": "code", "execution_count": 13, "id": "e57db1f7-f968-43a6-a9b2-f0cf684fd295", "metadata": {}, "outputs": [], "source": [ "Z = list(zip(X, Y))" ] }, { "cell_type": "code", "execution_count": 14, "id": "076be143-ff5d-4ed9-b1c5-8790c2616ee5", "metadata": {}, "outputs": [], "source": [ "# Z do this only for small N" ] }, { "cell_type": "code", "execution_count": 15, "id": "91a83c32-a1a1-4072-871b-439e43a8c8b4", "metadata": {}, "outputs": [], "source": [ "R = [(x, y) for (x, y) in Z if x**2 + y**2 <= 1]" ] }, { "cell_type": "code", "execution_count": 16, "id": "3a76100f-25d4-456f-9c15-6f223e46540b", "metadata": {}, "outputs": [], "source": [ "# R only for small N" ] }, { "cell_type": "code", "execution_count": 17, "id": "b3097712-e7d1-49c5-8663-a8f1af178388", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.14318" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "4.0*len(R)/N" ] }, { "cell_type": "markdown", "id": "b0cdecf9-b50a-4b29-b034-34ec96e6a2b6", "metadata": {}, "source": [ "# 3. Storing Data in Functions" ] }, { "cell_type": "markdown", "id": "ac32d4ce-c6a4-4220-8609-b90ce1299559", "metadata": {}, "source": [ "We can add a list or dictionary with a default empty list or dictionary to a function. This default argument is useful to store data." ] }, { "cell_type": "markdown", "id": "515ead4f-855c-409a-b9f0-e6a45f7cd40a", "metadata": {}, "source": [ "Consider again the guessing of a secret number." ] }, { "cell_type": "code", "execution_count": 18, "id": "d7bc750f-2137-496b-904b-21ccbdc9a275", "metadata": {}, "outputs": [], "source": [ "from random import randint" ] }, { "cell_type": "code", "execution_count": 19, "id": "728483ee-05ce-4e8a-878d-8e2441c46541", "metadata": {}, "outputs": [], "source": [ "def guess(data={}, **options):\n", " \"\"\"\n", " Prompts to a guess a secret number\n", " stored in the dictionary data.\n", " The dictionary is printed if verbose.\n", " \"\"\"\n", " if len(data) == 0:\n", " data['secret'] = randint(0, 9)\n", " if 'verbose' in options:\n", " if options['verbose']:\n", " print('data =', data)\n", " nbr = int(input('Make a guess : '))\n", " return nbr == data['secret']" ] }, { "cell_type": "markdown", "id": "c076ff81-18e3-4f39-a5f5-00541c397f8c", "metadata": {}, "source": [ "The `verbose=True` option shows the dictionary `data`." ] }, { "cell_type": "code", "execution_count": 20, "id": "090e2f08-6793-444f-9f84-db867dd4b6c5", "metadata": {}, "outputs": [ { "name": "stdin", "output_type": "stream", "text": [ "Make a guess : 0\n" ] }, { "data": { "text/plain": [ "False" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "guess()" ] }, { "cell_type": "code", "execution_count": 21, "id": "46424050-29bc-425d-987d-75394801202e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "data = {'secret': 9}\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ "Make a guess : 9\n" ] }, { "data": { "text/plain": [ "True" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "guess(verbose=True)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.10.10" } }, "nbformat": 4, "nbformat_minor": 5 }