{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Solution - In Class Activity - Python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this notebook we will try to put some of the Python knowledge you acquired into use. For each exercise, you will see some code that has some bugs or asks you to write a little program. Simply fix the code cell to make code that runs correctly, or write the code to specification. Then add a markdown cell below your answer that says what you changed and why, or explains what your code is doing in English." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Importing and using existing functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 0: Random number generation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The following code imports the random number functionality in Python from the `numpy` library and uses to do generate random numbers. Why won't this code run?" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'numpy' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mnumpy\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrandom\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mnpr\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"My random number is \"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnumpy\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrandom\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrandom\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'numpy' is not defined" ] } ], "source": [ "import numpy.random as npr\n", "\n", "print(\"My random number is \", numpy.random.random())" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "My random number is 0.5185895455925532\n" ] } ], "source": [ "# Solution 1\n", "import numpy.random as npr\n", "print(\"My random number is \", npr.random())" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "My random number is 0.3396647640432414\n" ] } ], "source": [ "# Solution 2\n", "import numpy\n", "print(\"My random number is \", numpy.random.random())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 1: Random sample" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The next line chooses a random sample of 5 elements from a list. What is wrong here?" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (, line 5)", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m5\u001b[0m\n\u001b[0;31m npr.choice(my_list,size=5,replace=False)\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], "source": [ "import numpy.random as npr\n", "\n", "my_list = list(range(100)\n", " \n", "npr.choice(my_list,size=5,replace=False)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([55, 97, 22, 18, 25])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Solution\n", "import numpy.random as npr\n", "my_list = list(range(100)) \n", "npr.choice(my_list,size=5,replace=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 2: Looking things up" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the above example when you get it to work what does the `choice()` function do? The documentation for this function is available by googling for \"numpy.random choice\". Add your answer in a cell below. Then look up the `random()` function used in Problem 0. What does it do?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Answer\n", "`choice(a)` picks random elements of a 1D array `a`. You can choose how many elements to pick with `size`, and whether or not to sample with replacement based on `replace`\n", "\n", "`random()` generates a random number uniformly from 0 to 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Writing a function" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the problem that remain you will need to write little function that accomplish various goals. Let's verify your understanding of functions." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 3: Defining a function" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What does this function do? Write a new cell below the function definition that calls the function using five different inputs and verify the outputs match your expectation." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "def my_function(x, y):\n", " return x+y" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n", "11\n", "5\n", "6\n", "6\n" ] } ], "source": [ "# Solution : the function adds two numbers together!\n", "print(my_function(0,10))\n", "print(my_function(1,10))\n", "print(my_function(1,4))\n", "print(my_function(2,4))\n", "print(my_function(1,5))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 4: What is not great about this function?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This function works but is not ideal, why?" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "def my_function_2(x, y, z):\n", " return x+y\n", "\n", "# Solution : the argument z is doing nothing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 5: Write you own function" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write your own custom function named `catchy_name()` to write a program that takes as input two arguments which are assumed to be strings. Return a new string which is the concatenations of the first two letters of each of the inputs. e.g. \n", "```\n", "catchy_name(\"South\",\"Houston\") -> \"SoHo\"\n", "catchy_name(\"Jennifer\", \"Lopez\") -> \"JeLo\"\n", "```" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "SoHo\n", "JeLo\n" ] } ], "source": [ "# Solution\n", "def catchy_name(name1,name2):\n", " return name1[:2] + name2[:2]\n", "\n", "print(catchy_name(\"South\",\"Houston\"))\n", "print(catchy_name(\"Jennifer\", \"Lopez\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Strings" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 6: Cat-Dog" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write a function called `catdog` that returns True if a input string given has the word cat and dog an equal number of times. For example, \n", "```\n", "cat_dog('catdog') # shoudl return True\n", "cat_dog('catcat') # should return false\n", "cat_dog('1cat1cadodog') # should return true\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Google 'python count occurrences in string' to find a existing function of method that counts the number of times a substring appears in a string to get started." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "False\n", "True\n" ] } ], "source": [ "# Solution\n", "def cat_dog(s):\n", " ncat = s.count('cat')\n", " ndog = s.count('dog')\n", " return ncat==ndog\n", "\n", "print(cat_dog('catdog'))\n", "print(cat_dog('catcat'))\n", "print(cat_dog('1cat1cadodog'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Lists" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 7: Summing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write a function call `summing()` that takes a list of numbers as input and return the sum of all the numbers. Use a for loop to do this.\n", "\n", "```\n", "summing([1, 2, 3]) → 6\n", "summing([5, 11, 2]) → 18\n", "summming([7, 0, 0]) → 7\n", "```" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6\n", "18\n", "7\n" ] } ], "source": [ "# Solution\n", "def summing(mylist):\n", " mysum = 0\n", " for x in mylist:\n", " mysum += x\n", " return mysum\n", "\n", "print(summing([1, 2, 3]))\n", "print(summing([5, 11, 2]))\n", "print(summing([7, 0, 0]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 8: Biggest difference" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "Given an array length 1 or more of ints, return the difference between the largest and smallest values in the array. Note: the built-in `min` and `max` functions.\n", "\n", "```\n", "big_diff([10, 3, 5, 6]) → 7\n", "big_diff([7, 2, 10, 9]) → 8\n", "big_diff([2, 10, 7, 2]) → 8\n", "```" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "7\n", "8\n", "8\n" ] } ], "source": [ "# Solution\n", "def big_diff(mylist):\n", " return max(mylist)-min(mylist)\n", "\n", "print(big_diff([10, 3, 5, 6]))\n", "print(big_diff([7, 2, 10, 9]))\n", "print(big_diff([2, 10, 7, 2]))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.7.9" } }, "nbformat": 4, "nbformat_minor": 4 }