diff --git a/Copy_of_Lab_2.ipynb b/Copy_of_Lab_2.ipynb new file mode 100644 index 0000000..2815f82 --- /dev/null +++ b/Copy_of_Lab_2.ipynb @@ -0,0 +1,1074 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Copy of Lab-2.ipynb", + "provenance": [], + "collapsed_sections": [], + "include_colab_link": true + }, + "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.1" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "uk7yc0nadBGa" + }, + "source": [ + "# Lab 2\n", + "\n", + "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github//afarbin/DATA1401-Spring-2020/blob/master/Labs/Lab-2/Lab-2.ipynb)\n", + "\n", + "## Submitting lab solutions\n", + "\n", + "At the end of the previous lab, you should have set up a \"Solutions\" directory in your Google Drive, with a fork of the class git repository that pull from Dr. Farbin's verison and pushes to your own fork. \n", + "\n", + "Unfortunately due to a typo in the previous lab, you probably forked the 2019 version of the gitlab repository for this course. Unless you noticed and corrected the error, you'll have to fork again.\n", + "\n", + "In addition, due to some problems with the setup in Google Colab, we will be submitting our solutions to your fork using the web interface. Instructions on how to use the command-line are in this notebook, but we suggest you do not follow them unless you are working in a jupyter notebook and not Google Colab." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "VykgQ4UCVsW6", + "colab_type": "code", + "outputId": "049d2566-639e-4260-cbd4-26aadb77560f", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 129 + } + }, + "source": [ + "from google.colab import drive\n", + "drive.mount('/content/drive')" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Go to this URL in a browser: https://accounts.google.com/o/oauth2/auth?client_id=947318989803-6bn6qk8qdgf4n4g3pfee6491hc0brc4i.apps.googleusercontent.com&redirect_uri=urn%3aietf%3awg%3aoauth%3a2.0%3aoob&response_type=code&scope=email%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdocs.test%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdrive%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdrive.photos.readonly%20https%3a%2f%2fwww.googleapis.com%2fauth%2fpeopleapi.readonly\n", + "\n", + "Enter your authorization code:\n", + "··········\n", + "Mounted at /content/drive\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "OMNaOnRksNK3" + }, + "source": [ + "You may also choose to delete the fork from your GitHub account. " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "1w56RBLWWD2V", + "colab_type": "code", + "outputId": "21abe0af-cfc0-4ed2-8904-904717dfaedf", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 72 + } + }, + "source": [ + "%cd /content/drive/My\\ Drive\n", + "!ls" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "/content/drive/My Drive\n", + "'Analysis and Comparison.gdoc'\t Data1401Labs\t SERE.docx\n", + "'Colab Notebooks'\t\t L2toons2018.gdoc\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "J_R64sQDqv0A" + }, + "source": [ + "## Repeating last steps of Lab 1\n", + "\n", + "### Create your own fork\n", + "We will create a new fork where you can keep track and submit your work, following [these instructions](https://help.github.com/articles/fork-a-repo/).\n", + "\n", + "Goto to github.com and log in.\n", + "\n", + "Next, create a fork of the [2020 class repository](https://github.com/afarbin/DATA1401-Spring-2020). Click the link and press the \"Fork\" button on the top right. Select your repository as where you want to place the fork.\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "D23-k7kAWR_A", + "colab_type": "code", + "outputId": "70df4532-23e8-4fe4-ee55-be2770ae1ae3", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "source": [ + "\n", + "\n", + "!mkdir Data-1401-Repo\n", + "%cd Data-1401-Repo\n", + "\n" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "/content/drive/My Drive/Data-1401-Repo\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "edTvE6rOqv0C" + }, + "source": [ + "### Make a local clone (Advanced)\n", + "\n", + "Before we get started, please mount your Google Drive using by clicking the file icon on the left, then clicking \"Mount Drive\", and following the instructions as you did in the previous lab.\n", + "\n", + "If you did complete Lab 1 and therefore created a 2019 fork and a local clone in you Google Drive, delete the local clone:\n", + "\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "hHRjdC9kXZFA", + "colab_type": "code", + "outputId": "18fef7a6-0b7e-4fa9-c50d-037048555b18", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 109 + } + }, + "source": [ + "!git clone https://github.com/Jerry-G77/DATA1401-Spring-2020\n" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Cloning into 'DATA1401-Spring-2020'...\n", + "remote: Enumerating objects: 109, done.\u001b[K\n", + "remote: Total 109 (delta 0), reused 0 (delta 0), pack-reused 109\u001b[K\n", + "Receiving objects: 100% (109/109), 28.84 MiB | 26.49 MiB/s, done.\n", + "Resolving deltas: 100% (23/23), done.\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "XuK4zzBBXZDm", + "colab_type": "code", + "outputId": "6f5bf73b-4a25-41fe-e605-d6135ae331b1", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 54 + } + }, + "source": [ + "%cd DATA1401-Spring-2020\n", + "!ls" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "/content/drive/My Drive/Data-1401-Repo/DATA1401-Spring-2020\n", + "Labs Lectures\tREADME.md syllabus.pdf\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "2u6B-rfNr1wN", + "colab": {} + }, + "source": [ + "!rm -rf drive/My\\ Drive/Data-1401-Repo" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "BDVI5nu8-2RH" + }, + "source": [ + "Now we will check out your fork in your Google Drive / Colab. If you will be doing everything on your own computer instead of Google Colab/Drive, you are welcome to install Git on your computer and perform the following steps (appropriately modified) on your computer instead.\n", + "\n", + "Start by listing the contents of your current directory." + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "e5tXg0f8qv0D", + "outputId": "337aa867-b57d-4d3f-bac5-960cb0e181c2", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 72 + } + }, + "source": [ + "%cd /content/drive/My\\ Drive\n", + "!ls" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "/content/drive/My Drive\n", + "'Analysis and Comparison.gdoc'\t Data1401Labs\t L2toons2018.gdoc\n", + "'Colab Notebooks'\t\t Data-1401-Repo SERE.docx\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "WYsyYcg1qv0J" + }, + "source": [ + "Make a new directory:" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "Z7noY1hMqv0L", + "outputId": "57d0cad6-f74d-4ca2-d5a5-3b006f3ce824", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 54 + } + }, + "source": [ + "!mkdir Data-1401-Repo\n", + "%cd Data-1401-Repo" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "mkdir: cannot create directory ‘Data-1401-Repo’: File exists\n", + "/content/drive/My Drive/Data-1401-Repo\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "fwsBdTnYqv0Q" + }, + "source": [ + "From the github page for your fork, press the green \"Clone or download\" button and copy the URL.\n", + "\n", + "Goto to your notebook and use the following command to clone the repository, pasting the URL you just copied:\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "8w42MH6Jqv0S", + "outputId": "9f47cbf7-73ec-40d5-e23d-ee12fa29c802", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "source": [ + "# What you past here should look like:\n", + "!git clone https://github.com/Jerry-G77/DATA1401-Spring-2020.git\n", + "\n" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "fatal: destination path 'DATA1401-Spring-2020' already exists and is not an empty directory.\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "cOAuqTVUqv0V" + }, + "source": [ + "Go into the directory:" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "b1Ew4tEZqv0X", + "outputId": "b02f528d-5709-4961-cee1-1e25cc5effde", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 54 + } + }, + "source": [ + "%cd DATA1401-Spring-2020\n", + "!ls" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "/content/drive/My Drive/Data-1401-Repo/DATA1401-Spring-2020\n", + "Labs Lectures\tREADME.md syllabus.pdf\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "IrhWToc-qv0a" + }, + "source": [ + "We will now connect your fork to the original so you can pull changes from there. \n", + "\n", + "Check remote status:" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "JxtMYR-9qv0c", + "outputId": "fc408e3a-a5a0-403a-b561-f15f8877c0d6", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 54 + } + }, + "source": [ + "!git remote -v" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "origin\thttps://github.com/Jerry-G77/DATA1401-Spring-2020 (fetch)\n", + "origin\thttps://github.com/Jerry-G77/DATA1401-Spring-2020 (push)\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "9ud3X0fBqv0f" + }, + "source": [ + "Now use the original class URL to set your upstream:" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "pgJlKxBqqv0h", + "colab": {} + }, + "source": [ + "!git remote add upstream https://github.com/afarbin/DATA1401-Spring-2020.git" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "id2yUEt9qv0k", + "outputId": "9b9a4e00-de62-4004-abb3-fd23597bc0f4", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 90 + } + }, + "source": [ + "!git remote -v" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "origin\thttps://github.com/Jerry-G77/DATA1401-Spring-2020 (fetch)\n", + "origin\thttps://github.com/Jerry-G77/DATA1401-Spring-2020 (push)\n", + "upstream\thttps://github.com/afarbin/DATA1401-Spring-2020.git (fetch)\n", + "upstream\thttps://github.com/afarbin/DATA1401-Spring-2020.git (push)\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "sAkgeJ6Iqv0n" + }, + "source": [ + "From now on, you can get the newest version of class material by using:" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "AGDsfTFLqv0o", + "colab": {} + }, + "source": [ + "!git pull" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "u9RAhs5b4vXY" + }, + "source": [ + "You should be setup now.\n", + "\n", + "## Make your for Private\n", + "\n", + "As a final step, go back to your fork in GitHub and click the \"gear\" icon to change the settings. Select \"Options\" on the left and scroll all the way down. Then click on \"Make Private\" to make your repository private. \n", + "\n", + "Next select the collaborators on the left and add your Professor and TA as collaborators. " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "i1q1wv0IqqBL" + }, + "source": [ + "## Working on and Submitting Labs\n", + "\n", + "As mentioned in class, you are welcome to not use Google Colab and instead install and use your own instance of Jupyter to work on the labs.\n", + "\n", + "No matter where you do you work, you will submit your work into your fork of the class repository in GitHub. \n", + "\n", + "### Updating your fork\n", + "\n", + "The class repository will be updated several times a week. But your fork will not be updated, unless you do so explicitly. There are two ways for you to update your fork:\n", + "\n", + "1. Use GitHub's web interface to make a pull request from the course base to your fork.\n", + " * Goto your fork in GitHub.\n", + " * Press \"New Pull Request\"\n", + " * You should see option to select base/head repository that allows you to select \"afarbin/Data1401-Spring-2020\" as the base. If not press \"compare accross forks\".\n", + " * Make sure you select your fork as the head.\n", + " * Press \"Create a pull request\".\n", + " * Press \"Merge pull request\".\n", + " * Press \"Confirm pull\".\n", + "\n", + "2. Go to your clone of your fork in Google Drive (or local on your computer) that setup above and do a `git pull`. (Advanced)\n", + "\n", + "### Working on your labs\n", + "\n", + "If you are working in Google Colab, you should immediately save a copy of you labs into your Google Drive as soon as you start working, and save frequently so you don't loose your work. These copies of the labs will appear in drive/My Drive/Colab Notebooks. \n", + "\n", + "### Submitting your labs\n", + "\n", + "Once you are done with a lab and are ready to submit, you have several options:\n", + "\n", + "1. You can download the lab to your local computer and them commit to your GitHub fork via GitHub's web interface. \n", + "\n", + " * Download by selecting \"Download .ipynb\" from the file menu.\n", + " * Appropriately rename the downloaded file (for example Lab-1-Solutions.ipynb).\n", + " * On github, navigate to the directory for the specific lab.\n", + " * Click on upload and upload your solutions.\n", + "\n", + "2. (Advanced) You can copy the lab into your fork and commit/push using the command-line. Here's how:\n", + "\n", + " * Using \"cd\" command navigate to the clone of your fork in Google Drive (or local) and do `git remote -v` to verify that things are correctly setup.\n", + "\n", + " * If you are working on Google Colab, your copy of lab with your solutions is stored in contents/drive/My Drive/Colab Notebooks. Locate the your lab 1 notebook and copy and rename it into the same directory in your fork. \n", + "\n", + "For example: (Note we are using the full paths here just to make sure everything works... but if you are already in the fork directory, you don't need the full path everywhere)." + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "CtPQXoQMdBGg", + "colab": {} + }, + "source": [ + "!cp /content/drive/My\\ Drive/Colab\\ Notebooks/Copy\\ of\\ Lab-2.ipynb /content/drive/My\\ Drive/Data-1401-Repo/DATA1401-Spring-2020/Labs/Lab-2/Lab-2-Solution.ipynb" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "b04FrL7sdBGr" + }, + "source": [ + "The reason we are renaming the file is due to a complication that you may experience when pulling updates into your fork. If a file was updated after your last pull and modifications, your modifications will likely cause a merge conflict, which can be a headache to resolve. Creating a new file side-steps this problem.\n", + "\n", + "Now that you have a new file in your fork, add the file into local repository:" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "2o9JmQ1QdBGs", + "colab": {} + }, + "source": [ + "!git add /content/drive/My\\ Drive/Data-1401-Repo/DATA1401-Spring-2020/Labs/Lab-2/Lab-2-Solution.ipynb" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "HX4xOctmdBG0" + }, + "source": [ + "You only need to add a file once. Next, commit this file to your local copy of the repository. If this is the first time you are doing a commit, you will have to tell git your github username." + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "Tn9jf5VXdBG1", + "colab": {} + }, + "source": [ + "# Uncomment and modify lines below if needed\n", + "#!git config --global user.email \"you@example.com\"\n", + "#!git config --global user.name \"Your Name\"\n", + "!git commit -a -m \"My Lab 2 Solutions\"" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "JfKXTlv1dBG5" + }, + "source": [ + "You are required to provide a text message when commiting files, and the `-m` option is the nicest way to do it. If you do not supply a message, you will find yourself in a text editor (most likely vi) which is difficult to use and forced to enter a message.\n", + "\n", + "You will need to commit your changes every time you wish to submit any changes. So if you keep working or come back to a lab, make sure you commit your changes.\n", + "\n", + "Now that you committed your changes, you will need to push these changes to the fork of the package in your github account:" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "L7UZOLYAdBG6", + "colab": {} + }, + "source": [ + "!git push" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "3dI_D9PAdBG-" + }, + "source": [ + "You will likely need to supply you git username and password.\n", + "\n", + "Your lab is now available for grading. Remember that unless you commit and push your work, it will not be seen.\n", + "\n", + "From now on, use this procedure to submit your solutions to labs, including the remainder of this lab. \n", + "\n", + "You can work in your Solutions directory if you like. But note that it may be a good practice to use the \"File\" menu to duplicate and remain labs when you first start. Besides letting you avoid having to do the copy later, you will have a copy of the original notebook, in case you delete something and can pull updates, in case of bug fixes." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "q-gMWiE4dBG_" + }, + "source": [ + "## Python Programming\n", + "\n", + "In the remainder of this lab you will practice python by solving some simple exercises. \n", + "\n", + "*Exercise 1:* Write 2 functions `even(x)` and `odd(x)` that take an integer and returns True if the input is even or odd, otherwise returns False. Use cell below for your solution. Use the subsequent cell to demonstrate that your solution works. Feel free to add additional cell as needed using the \"+\" button on the button bar above. " + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "jwN5jff1dBG_", + "colab": {} + }, + "source": [ + "# Write you solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "xNJAcodhdBHB", + "colab": {} + }, + "source": [ + "# Test your solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "KL_pyzG8dBHD" + }, + "source": [ + "*Exercise 2:* Write a function that takes a list of numbers as input and returns a list of the subset of elements that are less that 10. Test your solution." + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "g8nt0wnldBHE", + "colab": {} + }, + "source": [ + "# Write you solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "__HTUWA1dBHH", + "colab": {} + }, + "source": [ + "# Test your solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "T0cx91JudBHK" + }, + "source": [ + "*Exercise 3:* Write a function that takes a number `x_max` as input and returns a function that performs the same task as exercise 2, but for `x_max` instead of 10." + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "PqummMcmdBHK", + "colab": {} + }, + "source": [ + "# Write you solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "evRYemjXdBHN", + "colab": {} + }, + "source": [ + "# Test your solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "h0I8M27LdBHP" + }, + "source": [ + "*Exercise 4:* Write a function that takes an interger as input and returns a list of all divisors of that number." + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "k6GUpDyrdBHP", + "colab": {} + }, + "source": [ + "# Write you solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "YDvRmft-dBHR", + "colab": {} + }, + "source": [ + "# Test your solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "IOWnLMvxdBHT" + }, + "source": [ + "*Exercise 5:* Write a function that takes 2 lists as input and returns a list that contains only the elements that are common between the lists (without duplicates). Make sure your program works on two lists of different sizes." + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "tR31bnTDdBHT", + "colab": {} + }, + "source": [ + "# Write you solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "IJDf6ebYdBHV", + "colab": {} + }, + "source": [ + "# Test your solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "3vA7M9pldBHX" + }, + "source": [ + "*Exercise 6:* Write a function that reads takes a string and returns `True` if the string is a palindrome. (A palindrome is a string that reads the same forwards and backwards.)" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "ncyMDzp6dBHX", + "colab": {} + }, + "source": [ + "# Write you solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "v5cmVQ6MdBHZ", + "colab": {} + }, + "source": [ + "# Test your solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "fT35xwandBHc" + }, + "source": [ + "*Exercise 7:* Write a Rock-Paper-Scissors game function, that takes 2 strings, the inputs of player 1 and player 2, and output 1 or 2 corresponding to which player wins, or 0 if draw.\n", + "\n", + "Implement a Rock-Paper-Scissors game by soliciting input from 2 players, testing with this function, and repeating if there is a draw.\n", + "\n", + "Remember the rules:\n", + "\n", + "* Rock beats scissors\n", + "* Scissors beats paper\n", + "* Paper beats rock" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "U1_HxxaWdBHd", + "colab": {} + }, + "source": [ + "# Write you solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "ooR2ldZBdBHf", + "colab": {} + }, + "source": [ + "# Test your solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "dJQzXNKzdBHh" + }, + "source": [ + "*Exercise 8:* Write a function that takes a integer `n` as input and \n", + "outputs a list of the first `n` Fibonnaci numbers.\n", + "\n", + "The Fibonnaci seqence is a sequence of numbers where the next number in the sequence is the sum of the previous two numbers in the sequence. The sequence looks like this: 1, 1, 2, 3, 5, 8, 13, …)" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "G_4ooRXTdBHh", + "colab": {} + }, + "source": [ + "# Write you solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "rzK5FskJdBHj", + "colab": {} + }, + "source": [ + "# Test your solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "q6c_AskadBHl" + }, + "source": [ + "*Exercise 9:* Write a function that takes a string of consisting of several words and returns a string that reverses the order of the words.\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "aJdXX6FHdBHl", + "colab": {} + }, + "source": [ + "# Write you solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "nQyhnLZ_dBHn", + "colab": {} + }, + "source": [ + "# Test your solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "NFSmRaSydBHq" + }, + "source": [ + "*Exercise 10:* Write a guessing game program that will repeatedly guess a number that the users picks, with the user indicating higher or lower, until it correctly guesses the number." + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "Ie2E1JzCdBHr", + "colab": {} + }, + "source": [ + "# Write you solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "6T8YdWSMdBHs", + "colab": {} + }, + "source": [ + "# Test your solution here" + ], + "execution_count": 0, + "outputs": [] + } + ] +} \ No newline at end of file diff --git a/Exams/Mid-term/Exam_solutions.ipynb b/Exams/Mid-term/Exam_solutions.ipynb new file mode 100644 index 0000000..1c07cea --- /dev/null +++ b/Exams/Mid-term/Exam_solutions.ipynb @@ -0,0 +1,338 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "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.1" + }, + "colab": { + "name": "Copy of Copy of Exam.ipynb", + "provenance": [] + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "H5NZY3CptBek", + "colab_type": "text" + }, + "source": [ + "# Mid-term Exam\n", + "\n", + "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github//afarbin/DATA1401-Spring-2020/blob/master/Exams/Mid-term/Exam.ipynb)\n", + "\n", + "Add cells to this notebook as you need for your solutions and your test of your solutions." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Vr_B2gQ3tBem", + "colab_type": "text" + }, + "source": [ + "1. Write a function `first_alphabetically(lst)` that takes a list `lst` of strings and returns the string that is alphabetically first. For example, calling your function with the list of states:" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "KRexz7sgtBen", + "colab_type": "code", + "colab": {} + }, + "source": [ + "states=['Mississippi', 'Maryland', 'Delaware', 'Connecticut', 'Virginia', 'Utah', 'Kansas',\n", + " 'Wyoming', 'Indiana', 'Louisiana', 'Missouri', 'Illinois', 'Minnesota', 'Vermont', \n", + " 'New Mexico', 'North Dakota', 'Wisconsin', 'Tennessee', 'New York', 'Oklahoma', \n", + " 'Colorado', 'Pennsylvania', 'West Virginia', 'Alabama', 'Montana', 'Texas', \n", + " 'Washington', 'Michigan', 'New Hampshire', 'Arkansas', 'Hawaii', 'Iowa', \n", + " 'Idaho', 'Kentucky', 'Ohio', 'Nebraska', 'Alaska', 'Oregon', 'South Dakota', \n", + " 'New Jersey', 'Florida', 'Georgia', 'Rhode Island', 'Arizona', 'Maine', \n", + " 'South Carolina', 'California', 'Nevada', 'Massachusetts', 'North Carolina']" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "skPSfpimtBew", + "colab_type": "text" + }, + "source": [ + "should return the string `\"Alabama\"`. Note that you can compare strings:" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "NeW2Ew0ftBex", + "colab_type": "code", + "colab": {} + }, + "source": [ + "print(\"A\">\"B\")\n", + "print(\"B\">\"A\")\n", + "print(\"A\">\"a\")\n", + "print(\"bca\">\"bbc\")" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "oC1olgVztBe3", + "colab_type": "text" + }, + "source": [ + "Make sure your implementation isn't case sensitive. Do not use python's built-in `min`, `max`, `sort` or any other sort function you find." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "nnfnuJpv5X_f", + "colab_type": "code", + "outputId": "1f4d7716-e073-4a91-b0a0-b7dd05d02df6", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 55 + } + }, + "source": [ + "print(states)" + ], + "execution_count": 94, + "outputs": [ + { + "output_type": "stream", + "text": [ + "['Mississippi', 'Maryland', 'Delaware', 'Connecticut', 'Virginia', 'Utah', 'Kansas', 'Wyoming', 'Indiana', 'Louisiana', 'Missouri', 'Illinois', 'Minnesota', 'Vermont', 'New Mexico', 'North Dakota', 'Wisconsin', 'Tennessee', 'New York', 'Oklahoma', 'Colorado', 'Pennsylvania', 'West Virginia', 'Alabama', 'Montana', 'Texas', 'Washington', 'Michigan', 'New Hampshire', 'Arkansas', 'Hawaii', 'Iowa', 'Idaho', 'Kentucky', 'Ohio', 'Nebraska', 'Alaska', 'Oregon', 'South Dakota', 'New Jersey', 'Florida', 'Georgia', 'Rhode Island', 'Arizona', 'Maine', 'South Carolina', 'California', 'Nevada', 'Massachusetts', 'North Carolina']\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "J6kOvJHnwBQe", + "colab_type": "code", + "colab": {} + }, + "source": [ + "def first_alphabetically(lst):\n", + " element_count=len(lst)\n", + " temp_sort=lst\n", + " #print(temp_sort)\n", + " #while temp_sort[0]!=\"Alabama\":\n", + " for i in range(element_count):\n", + " min_index=i\n", + " #print(\"1st for loop: min_index\",min_index)\n", + " for j in range(i+1,element_count):\n", + " count=j\n", + " if temp_sort[count]==temp_sort[min_index]:\n", + " count+=1 \n", + " #print(\"count=\",count,\"min_index=\",min_index)\n", + " if lst[j]=bin_edges[i] and d\" symbol directs stdout into a file. Try \"cat > favorite-colors-list.txt\" and then type in your 3 favorite colors, each on it's own line. Use \"^D\" to end your input." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "H5vxtcXnqvz6", + "colab_type": "text" + }, + "source": [ + "Use \"cat\", \"more\", or \"less\" to confirm that you file is as you expect it. \">>\" allows you to append to the file. \n", + "\n", + "*Exercise 5b:* Append 2 more colors to your file." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "twRKNaGy3XGw", + "colab_type": "code", + "colab": {} + }, + "source": [ + "!/bin/bash --noediting" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "DZODNKiAqvz8", + "colab_type": "text" + }, + "source": [ + "The \"sort\" command sorts what it sees on stdin. Instead of taking input from the terminal, you can direct the shell to take stdin from a file using \"<\". Try \"sort < favorite-color-list.txt\" and \"sort < favorite-color-list.txt > sorted-favorite-color-list.txt\".\n", + "\n", + "Finally, instead of piping input / output into files, you can directly chain one program into another using \"|\". So for example, you can do \"cat /etc/passwd | grep -i \\$USER | wc -l\". \n", + "\n", + "*Exercise 5c:* Use indirection to count the number of users on TACC with your first name. Copy the command you used into box below." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "oP9XlZl_3iZD", + "colab_type": "code", + "colab": {} + }, + "source": [ + "!/bin/bash --noediting" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "v5IaZXNyqvz_", + "colab_type": "text" + }, + "source": [ + "## Git\n", + "\n", + "`git` is a Version Control System (VCS), typically used to organize the source code of software project but also good source of documents or web-pages. An instance of `git` server stores repositories, each typically containing the code relevant to a specific project. Users create local `clones` of repositories, change and develop the local copies of the code, `commit` the changes to their local repository, `push` to the server as a contribution, \n", + "`pull` updates from the server, and `merge` changes between local and remote versions. \n", + "\n", + "Besides cloning, repositories can be branched or forked. A repository generally starts with a `master` branch that evolves as push requests are merged in. Creating a new branch from an existing branch creates a snapshot of the which can evolve independently or be merged in later. Branches are easy to make and delete, and can serve various purposes. They can represent a stable version of software package. Or a parallel development for different operating system. A fork of a repository is a standalone instance of the repository which can be stored and managed independently from the original, where you can work independently without constraints or interference. \n", + "\n", + "[GitHub](github.com) provides a massive publically accessible instance of a `git` system besides sharing code, projects can be developed by the open source community. It provides tools for managing your repository and a wiki for documentation. Contributions to public software on GitHub generally require making a merge request which would be judged by the managers of the repository. That's why most software packages enourage you to create a new fork, so you can work independently.\n", + "\n", + "Lets take a look at some repositories:\n", + "\n", + "* [This class](https://github.com/afarbin/DATA1401-Spring-2020)\n", + "\n", + "\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "J_R64sQDqv0A", + "colab_type": "text" + }, + "source": [ + "## Plan\n", + "\n", + "You made a clone of the class repository at start of this lab. We will create a new fork where you can keep track and submit your work, following [these instructions](https://help.github.com/articles/fork-a-repo/).\n", + "\n", + "Goto to github.com and log in.\n", + "\n", + "Next, lets create a fork of the [class repository](https://github.com/afarbin/DATA1401-Spring-2019). Click the link and press the \"Fork\" button on the top right. Select your repository as where you want to place the fork.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "edTvE6rOqv0C", + "colab_type": "text" + }, + "source": [ + "Now we will check out your fork in your Google Drive / Colab.\n", + "\n", + "Note: Jupyter allows you to run shell directly in a notebook. We will use `!` and `%` to call shell commands directly in this notebook. Follow along yourself. Either create a new notebook or open a terminal. \n", + "\n", + "Start by listing the contents of your current directory." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "e5tXg0f8qv0D", + "colab_type": "code", + "colab": {} + }, + "source": [ + "%cd /content/drive/My\\ Drive\n", + "!ls" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "WYsyYcg1qv0J", + "colab_type": "text" + }, + "source": [ + "Make a new directory:" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Z7noY1hMqv0L", + "colab_type": "code", + "colab": {} + }, + "source": [ + "!mkdir Data-1401-Repo\n", + "%cd Data-1401-Repo" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "fwsBdTnYqv0Q", + "colab_type": "text" + }, + "source": [ + "From the github page for your fork, press the green \"Clone or download\" button and copy the URL.\n", + "\n", + "Goto to your notebook and use the following command to clone the repository, pasting the URL you just copied:\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "8w42MH6Jqv0S", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# What you past here should look like:\n", + "#!git clone https://github.com//DATA1401-Spring-2020.git" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "cOAuqTVUqv0V", + "colab_type": "text" + }, + "source": [ + "Go into the directory:" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "b1Ew4tEZqv0X", + "colab_type": "code", + "colab": {} + }, + "source": [ + "%cd DATA1401-Spring-2020\n", + "!ls" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "IrhWToc-qv0a", + "colab_type": "text" + }, + "source": [ + "We will now connect your fork to the original so you can pull changes from there. \n", + "\n", + "Check remote status:" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "JxtMYR-9qv0c", + "colab_type": "code", + "colab": {} + }, + "source": [ + "!git remote -v" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "9ud3X0fBqv0f", + "colab_type": "text" + }, + "source": [ + "Now use the original class URL to set your upstream:" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "pgJlKxBqqv0h", + "colab_type": "code", + "colab": {} + }, + "source": [ + "!git remote add upstream https://github.com/afarbin/DATA1401-Spring-2020.git" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "id2yUEt9qv0k", + "colab_type": "code", + "colab": {} + }, + "source": [ + "!git remote -v" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "sAkgeJ6Iqv0n", + "colab_type": "text" + }, + "source": [ + "From now on, you can get the newest version of class material by using:" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "AGDsfTFLqv0o", + "colab_type": "code", + "colab": {} + }, + "source": [ + "!git pull" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "u9RAhs5b4vXY", + "colab_type": "text" + }, + "source": [ + "We will submit your Lab 1 using git at the next Lab." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "PPfGmFQI40HR", + "colab_type": "code", + "colab": {} + }, + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + } + ] +} \ No newline at end of file diff --git a/Labs/Lab-2/Lab_2-solutions.ipynb b/Labs/Lab-2/Lab_2-solutions.ipynb new file mode 100644 index 0000000..c2246d8 --- /dev/null +++ b/Labs/Lab-2/Lab_2-solutions.ipynb @@ -0,0 +1,1231 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Copy of Copy of Copy of Lab-2.ipynb", + "provenance": [], + "collapsed_sections": [] + }, + "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.1" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "uk7yc0nadBGa" + }, + "source": [ + "# Lab 2\n", + "\n", + "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github//afarbin/DATA1401-Spring-2020/blob/master/Labs/Lab-2/Lab-2.ipynb)\n", + "\n", + "## Submitting lab solutions\n", + "\n", + "At the end of the previous lab, you should have set up a \"Solutions\" directory in your Google Drive, with a fork of the class git repository that pull from Dr. Farbin's verison and pushes to your own fork. \n", + "\n", + "Unfortunately due to a typo in the previous lab, you probably forked the 2019 version of the gitlab repository for this course. Unless you noticed and corrected the error, you'll have to fork again.\n", + "\n", + "In addition, due to some problems with the setup in Google Colab, we will be submitting our solutions to your fork using the web interface. Instructions on how to use the command-line are in this notebook, but we suggest you do not follow them unless you are working in a jupyter notebook and not Google Colab." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "VykgQ4UCVsW6", + "colab_type": "code", + "outputId": "98b1347b-df19-4ef5-f825-dddcd3e092eb", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 129 + } + }, + "source": [ + "from google.colab import drive\n", + "drive.mount('/content/drive')" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Go to this URL in a browser: https://accounts.google.com/o/oauth2/auth?client_id=947318989803-6bn6qk8qdgf4n4g3pfee6491hc0brc4i.apps.googleusercontent.com&redirect_uri=urn%3aietf%3awg%3aoauth%3a2.0%3aoob&response_type=code&scope=email%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdocs.test%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdrive%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdrive.photos.readonly%20https%3a%2f%2fwww.googleapis.com%2fauth%2fpeopleapi.readonly\n", + "\n", + "Enter your authorization code:\n", + "··········\n", + "Mounted at /content/drive\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "OMNaOnRksNK3" + }, + "source": [ + "You may also choose to delete the fork from your GitHub account. " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "1w56RBLWWD2V", + "colab_type": "code", + "outputId": "21abe0af-cfc0-4ed2-8904-904717dfaedf", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 72 + } + }, + "source": [ + "%cd /content/drive/My\\ Drive\n", + "!ls" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "/content/drive/My Drive\n", + "'Analysis and Comparison.gdoc'\t Data1401Labs\t SERE.docx\n", + "'Colab Notebooks'\t\t L2toons2018.gdoc\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "J_R64sQDqv0A" + }, + "source": [ + "## Repeating last steps of Lab 1\n", + "\n", + "### Create your own fork\n", + "We will create a new fork where you can keep track and submit your work, following [these instructions](https://help.github.com/articles/fork-a-repo/).\n", + "\n", + "Goto to github.com and log in.\n", + "\n", + "Next, create a fork of the [2020 class repository](https://github.com/afarbin/DATA1401-Spring-2020). Click the link and press the \"Fork\" button on the top right. Select your repository as where you want to place the fork.\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "D23-k7kAWR_A", + "colab_type": "code", + "outputId": "70df4532-23e8-4fe4-ee55-be2770ae1ae3", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "source": [ + "\n", + "\n", + "!mkdir Data-1401-Repo\n", + "%cd Data-1401-Repo\n", + "\n" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "/content/drive/My Drive/Data-1401-Repo\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "edTvE6rOqv0C" + }, + "source": [ + "### Make a local clone (Advanced)\n", + "\n", + "Before we get started, please mount your Google Drive using by clicking the file icon on the left, then clicking \"Mount Drive\", and following the instructions as you did in the previous lab.\n", + "\n", + "If you did complete Lab 1 and therefore created a 2019 fork and a local clone in you Google Drive, delete the local clone:\n", + "\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "hHRjdC9kXZFA", + "colab_type": "code", + "outputId": "18fef7a6-0b7e-4fa9-c50d-037048555b18", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 109 + } + }, + "source": [ + "!git clone https://github.com/Jerry-G77/DATA1401-Spring-2020\n" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Cloning into 'DATA1401-Spring-2020'...\n", + "remote: Enumerating objects: 109, done.\u001b[K\n", + "remote: Total 109 (delta 0), reused 0 (delta 0), pack-reused 109\u001b[K\n", + "Receiving objects: 100% (109/109), 28.84 MiB | 26.49 MiB/s, done.\n", + "Resolving deltas: 100% (23/23), done.\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "XuK4zzBBXZDm", + "colab_type": "code", + "outputId": "6f5bf73b-4a25-41fe-e605-d6135ae331b1", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 54 + } + }, + "source": [ + "%cd DATA1401-Spring-2020\n", + "!ls" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "/content/drive/My Drive/Data-1401-Repo/DATA1401-Spring-2020\n", + "Labs Lectures\tREADME.md syllabus.pdf\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "2u6B-rfNr1wN", + "colab": {} + }, + "source": [ + "!rm -rf drive/My\\ Drive/Data-1401-Repo" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "BDVI5nu8-2RH" + }, + "source": [ + "Now we will check out your fork in your Google Drive / Colab. If you will be doing everything on your own computer instead of Google Colab/Drive, you are welcome to install Git on your computer and perform the following steps (appropriately modified) on your computer instead.\n", + "\n", + "Start by listing the contents of your current directory." + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "e5tXg0f8qv0D", + "outputId": "337aa867-b57d-4d3f-bac5-960cb0e181c2", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 72 + } + }, + "source": [ + "%cd /content/drive/My\\ Drive\n", + "!ls" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "/content/drive/My Drive\n", + "'Analysis and Comparison.gdoc'\t Data1401Labs\t L2toons2018.gdoc\n", + "'Colab Notebooks'\t\t Data-1401-Repo SERE.docx\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "WYsyYcg1qv0J" + }, + "source": [ + "Make a new directory:" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "Z7noY1hMqv0L", + "outputId": "57d0cad6-f74d-4ca2-d5a5-3b006f3ce824", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 54 + } + }, + "source": [ + "!mkdir Data-1401-Repo\n", + "%cd Data-1401-Repo" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "mkdir: cannot create directory ‘Data-1401-Repo’: File exists\n", + "/content/drive/My Drive/Data-1401-Repo\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "fwsBdTnYqv0Q" + }, + "source": [ + "From the github page for your fork, press the green \"Clone or download\" button and copy the URL.\n", + "\n", + "Goto to your notebook and use the following command to clone the repository, pasting the URL you just copied:\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "8w42MH6Jqv0S", + "outputId": "9f47cbf7-73ec-40d5-e23d-ee12fa29c802", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "source": [ + "# What you past here should look like:\n", + "!git clone https://github.com/Jerry-G77/DATA1401-Spring-2020.git\n", + "\n" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "fatal: destination path 'DATA1401-Spring-2020' already exists and is not an empty directory.\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "cOAuqTVUqv0V" + }, + "source": [ + "Go into the directory:" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "b1Ew4tEZqv0X", + "outputId": "b02f528d-5709-4961-cee1-1e25cc5effde", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 54 + } + }, + "source": [ + "%cd DATA1401-Spring-2020\n", + "!ls" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "/content/drive/My Drive/Data-1401-Repo/DATA1401-Spring-2020\n", + "Labs Lectures\tREADME.md syllabus.pdf\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "IrhWToc-qv0a" + }, + "source": [ + "We will now connect your fork to the original so you can pull changes from there. \n", + "\n", + "Check remote status:" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "JxtMYR-9qv0c", + "outputId": "fc408e3a-a5a0-403a-b561-f15f8877c0d6", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 54 + } + }, + "source": [ + "!git remote -v" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "origin\thttps://github.com/Jerry-G77/DATA1401-Spring-2020 (fetch)\n", + "origin\thttps://github.com/Jerry-G77/DATA1401-Spring-2020 (push)\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "9ud3X0fBqv0f" + }, + "source": [ + "Now use the original class URL to set your upstream:" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "pgJlKxBqqv0h", + "colab": {} + }, + "source": [ + "!git remote add upstream https://github.com/afarbin/DATA1401-Spring-2020.git" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "id2yUEt9qv0k", + "outputId": "9b9a4e00-de62-4004-abb3-fd23597bc0f4", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 90 + } + }, + "source": [ + "!git remote -v" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "origin\thttps://github.com/Jerry-G77/DATA1401-Spring-2020 (fetch)\n", + "origin\thttps://github.com/Jerry-G77/DATA1401-Spring-2020 (push)\n", + "upstream\thttps://github.com/afarbin/DATA1401-Spring-2020.git (fetch)\n", + "upstream\thttps://github.com/afarbin/DATA1401-Spring-2020.git (push)\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "sAkgeJ6Iqv0n" + }, + "source": [ + "From now on, you can get the newest version of class material by using:" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "AGDsfTFLqv0o", + "colab": {} + }, + "source": [ + "!git pull" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "u9RAhs5b4vXY" + }, + "source": [ + "You should be setup now.\n", + "\n", + "## Make your for Private\n", + "\n", + "As a final step, go back to your fork in GitHub and click the \"gear\" icon to change the settings. Select \"Options\" on the left and scroll all the way down. Then click on \"Make Private\" to make your repository private. \n", + "\n", + "Next select the collaborators on the left and add your Professor and TA as collaborators. " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "i1q1wv0IqqBL" + }, + "source": [ + "## Working on and Submitting Labs\n", + "\n", + "As mentioned in class, you are welcome to not use Google Colab and instead install and use your own instance of Jupyter to work on the labs.\n", + "\n", + "No matter where you do you work, you will submit your work into your fork of the class repository in GitHub. \n", + "\n", + "### Updating your fork\n", + "\n", + "The class repository will be updated several times a week. But your fork will not be updated, unless you do so explicitly. There are two ways for you to update your fork:\n", + "\n", + "1. Use GitHub's web interface to make a pull request from the course base to your fork.\n", + " * Goto your fork in GitHub.\n", + " * Press \"New Pull Request\"\n", + " * You should see option to select base/head repository that allows you to select \"afarbin/Data1401-Spring-2020\" as the base. If not press \"compare accross forks\".\n", + " * Make sure you select your fork as the head.\n", + " * Press \"Create a pull request\".\n", + " * Press \"Merge pull request\".\n", + " * Press \"Confirm pull\".\n", + "\n", + "2. Go to your clone of your fork in Google Drive (or local on your computer) that setup above and do a `git pull`. (Advanced)\n", + "\n", + "### Working on your labs\n", + "\n", + "If you are working in Google Colab, you should immediately save a copy of you labs into your Google Drive as soon as you start working, and save frequently so you don't loose your work. These copies of the labs will appear in drive/My Drive/Colab Notebooks. \n", + "\n", + "### Submitting your labs\n", + "\n", + "Once you are done with a lab and are ready to submit, you have several options:\n", + "\n", + "1. You can download the lab to your local computer and them commit to your GitHub fork via GitHub's web interface. \n", + "\n", + " * Download by selecting \"Download .ipynb\" from the file menu.\n", + " * Appropriately rename the downloaded file (for example Lab-1-Solutions.ipynb).\n", + " * On github, navigate to the directory for the specific lab.\n", + " * Click on upload and upload your solutions.\n", + "\n", + "2. (Advanced) You can copy the lab into your fork and commit/push using the command-line. Here's how:\n", + "\n", + " * Using \"cd\" command navigate to the clone of your fork in Google Drive (or local) and do `git remote -v` to verify that things are correctly setup.\n", + "\n", + " * If you are working on Google Colab, your copy of lab with your solutions is stored in contents/drive/My Drive/Colab Notebooks. Locate the your lab 1 notebook and copy and rename it into the same directory in your fork. \n", + "\n", + "For example: (Note we are using the full paths here just to make sure everything works... but if you are already in the fork directory, you don't need the full path everywhere)." + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "CtPQXoQMdBGg", + "colab": {} + }, + "source": [ + "!cp /content/drive/My\\ Drive/Colab\\ Notebooks/Copy\\ of\\ Lab-2.ipynb /content/drive/My\\ Drive/Data-1401-Repo/DATA1401-Spring-2020/Labs/Lab-2/Lab-2-Solution.ipynb" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "b04FrL7sdBGr" + }, + "source": [ + "The reason we are renaming the file is due to a complication that you may experience when pulling updates into your fork. If a file was updated after your last pull and modifications, your modifications will likely cause a merge conflict, which can be a headache to resolve. Creating a new file side-steps this problem.\n", + "\n", + "Now that you have a new file in your fork, add the file into local repository:" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "2o9JmQ1QdBGs", + "outputId": "c6528601-2896-4839-8d6c-88e99ad12aa6", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "source": [ + "!git add /content/drive/My\\ Drive/Data-1401-Repo/DATA1401-Spring-2020/Labs/Lab-2/Lab-2-Solution.ipynb" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "fatal: not a git repository (or any of the parent directories): .git\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "HX4xOctmdBG0" + }, + "source": [ + "You only need to add a file once. Next, commit this file to your local copy of the repository. If this is the first time you are doing a commit, you will have to tell git your github username." + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "Tn9jf5VXdBG1", + "colab": {} + }, + "source": [ + "# Uncomment and modify lines below if needed\n", + "#!git config --global user.email \"you@example.com\"\n", + "#!git config --global user.name \"Your Name\"\n", + "!git commit -a -m \"My Lab 2 Solutions\"" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "JfKXTlv1dBG5" + }, + "source": [ + "You are required to provide a text message when commiting files, and the `-m` option is the nicest way to do it. If you do not supply a message, you will find yourself in a text editor (most likely vi) which is difficult to use and forced to enter a message.\n", + "\n", + "You will need to commit your changes every time you wish to submit any changes. So if you keep working or come back to a lab, make sure you commit your changes.\n", + "\n", + "Now that you committed your changes, you will need to push these changes to the fork of the package in your github account:" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "L7UZOLYAdBG6", + "colab": {} + }, + "source": [ + "!git push" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "3dI_D9PAdBG-" + }, + "source": [ + "You will likely need to supply you git username and password.\n", + "\n", + "Your lab is now available for grading. Remember that unless you commit and push your work, it will not be seen.\n", + "\n", + "From now on, use this procedure to submit your solutions to labs, including the remainder of this lab. \n", + "\n", + "You can work in your Solutions directory if you like. But note that it may be a good practice to use the \"File\" menu to duplicate and remain labs when you first start. Besides letting you avoid having to do the copy later, you will have a copy of the original notebook, in case you delete something and can pull updates, in case of bug fixes." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "q-gMWiE4dBG_" + }, + "source": [ + "## Python Programming\n", + "\n", + "In the remainder of this lab you will practice python by solving some simple exercises. \n", + "\n", + "*Exercise 1:* Write 2 functions `even(x)` and `odd(x)` that take an integer and returns True if the input is even or odd, otherwise returns False. Use cell below for your solution. Use the subsequent cell to demonstrate that your solution works. Feel free to add additional cell as needed using the \"+\" button on the button bar above. " + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "jwN5jff1dBG_", + "colab": {} + }, + "source": [ + "def even(x):\n", + " if x%2==0 :\n", + " return True\n", + " else :\n", + " return False" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "TdpU9Bz_JXxm", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "d2780390-5dbd-4b76-c562-c1594b070d29" + }, + "source": [ + "even(4)\n" + ], + "execution_count": 2, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "True" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 2 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "xNJAcodhdBHB", + "colab": {} + }, + "source": [ + "def odd(x):\n", + " if x%2!=0:\n", + " return True\n", + " else:\n", + " return False" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "C5NSytu9LHlo", + "colab_type": "text" + }, + "source": [ + "" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Bm-lxREpLg2h", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "b03a9c8c-59c7-41c8-89ec-d3e670394135" + }, + "source": [ + "odd(9)" + ], + "execution_count": 5, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "True" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 5 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "KL_pyzG8dBHD" + }, + "source": [ + "*Exercise 2:* Write a function that takes a list of numbers as input and returns a list of the subset of elements that are less that 10. Test your solution." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Gxo6_hMdMJFf", + "colab_type": "code", + "colab": {} + }, + "source": [ + "Numbers = [2,3,4,5,7,8,9,10,11]" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "g8nt0wnldBHE", + "colab": {} + }, + "source": [ + "def numbers_list(in_list):\n", + " out=list()\n", + " for i in range(len(in_list)):\n", + " if in_list[i]<10:\n", + " out.append(in_list[i])\n", + " print(out[i])\n", + " return out" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "__HTUWA1dBHH", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 146 + }, + "outputId": "61d82638-d877-4bd6-ee21-3a536230b6ce" + }, + "source": [ + "x=numbers_list(Numbers)" + ], + "execution_count": 22, + "outputs": [ + { + "output_type": "stream", + "text": [ + "2\n", + "3\n", + "4\n", + "5\n", + "7\n", + "8\n", + "9\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "T0cx91JudBHK" + }, + "source": [ + "*Exercise 3:* Write a function that takes a number `x_max` as input and returns a function that performs the same task as exercise 2, but for `x_max` instead of 10." + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "PqummMcmdBHK", + "colab": {} + }, + "source": [ + "def function3 (x_max):\n", + " def numbers_list(in_list):\n", + " out=list()\n", + " for i in range(len(in_list)):\n", + " if in_list[i]0: \n", + " print (\"Type of Data Contents:\", type(data[0]))\n", + " print (\"Data Minimum:\", min(data))\n", + " print (\"Data Maximum:\", max(data))" + ], + "execution_count": 2, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Data Type: \n", + "Data Length: 1000\n", + "Type of Data Contents: \n", + "Data Minimum: -9.994836480954806\n", + "Data Maximum: 9.978098835944913\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "vc1YFLdLWPba", + "colab_type": "text" + }, + "source": [ + "*Exercise 2a:* \n", + "Write a function that computes the mean of values in a list." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Ht_TjVUjWPbb", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Skeleton\n", + "def mean(data):\n", + " m=0.\n", + " n=len(data)\n", + " sum=0\n", + " for i in range(n):\n", + " sum+=data[i]\n", + " m=sum/n\n", + " print(\"Mean is\",m,\"Sum is\",sum,\"number of items is\",n)\n", + " \n", + " ### BEGIN SOLUTION\n", + "\n", + " # Fill in your solution here \n", + " \n", + " ### END SOLUTION\n", + " \n", + " return m" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "ic2CBwIOWPbh", + "colab_type": "code", + "outputId": "9677bf8b-38ef-4d5c-f72c-b13531f1e6f5", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 53 + } + }, + "source": [ + "# Test your solution here\n", + "print (\"Mean of Data:\", mean(data))" + ], + "execution_count": 4, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Mean is -0.12396135853715075 Sum is -123.96135853715074 number of items is 1000\n", + "Mean of Data: -0.12396135853715075\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "tma2uhmUWPbl", + "colab_type": "text" + }, + "source": [ + "*Exercise 2b:* \n", + "Write a function that computes the variance of values in a list." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "qsCQh1OvWPbm", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Skeleton\n", + "def variance(data):\n", + " m=0.\n", + " n=len(data)\n", + " sum=0\n", + " diff=0\n", + " for i in range(n):\n", + " sum+=data[i]\n", + " m=sum/n \n", + " for i in range(n):\n", + " diff+=(data[i]-m)**2\n", + " #find variance after sum of difference\n", + " #of element of list=data and mean squared\n", + " m=diff/(n-1)\n", + " \n", + " ### END SOLUTION\n", + " \n", + " return m" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "ffQXFnBeWPbr", + "colab_type": "code", + "outputId": "4b5448f4-8bfa-427a-889a-e1b53d12b266", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "source": [ + "# Test your solution here\n", + "print (\"Variance of Data:\", variance(data))" + ], + "execution_count": 6, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Variance of Data: 34.80074865671235\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "6EMiPjV1WPbw", + "colab_type": "text" + }, + "source": [ + "## Histogramming" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "krpMlQxqWPbx", + "colab_type": "text" + }, + "source": [ + "*Exercise 3:* Write a function that bins the data so that you can create a histogram. An example of how to implement histogramming is the following logic:\n", + "\n", + "* User inputs a list of values `x` and optionally `n_bins` which defaults to 10.\n", + "* If not supplied, find the minimum and maximum (`x_min`,`x_max`) of the values in x.\n", + "* Determine the bin size (`bin_size`) by dividing the range of the function by the number of bins.\n", + "* Create an empty list of zeros of size `n_bins`, call it `hist`.\n", + "* Loop over the values in `x`\n", + " * Loop over the values in `hist` with index `i`:\n", + " * If x is between `x_min+i*bin_size` and `x_min+(i+1)*bin_size`, increment `hist[i].` \n", + " * For efficiency, try to use continue to goto the next bin and data point.\n", + "* Return `hist` and the list corresponding of the bin edges (i.e. of `x_min+i*bin_size`). " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "WMqsrrpjP6Jy", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "6df4e922-efe6-472a-b64b-0addb1f52458" + }, + "source": [ + "print(\"x_min=\",max(data))" + ], + "execution_count": 9, + "outputs": [ + { + "output_type": "stream", + "text": [ + "x_min= 9.978098835944913\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Yh1VHOZJP6G8", + "colab_type": "code", + "colab": {} + }, + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "czNwaZavWPby", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Solution\n", + "def histogram(x,n_bins=10,x_min=None,x_max=None):\n", + " if x_min==None or x_max==none:\n", + " x_min=min(data)\n", + " x_max=max(data)\n", + " bin_size=(x_max-x_min)/n_bins\n", + " hist=[0,0,0,0,0,0,0,0,0,0]\n", + " for i in range(len(x)):\n", + " if x[i]>x_min+(i*bin_size) and x[i]=mymin\n", + " return testrange\n", + "\n", + "# Examples:\n", + "F1=inrange(0,10)\n", + "F2=inrange(10,20)\n", + "\n", + "# Test of in_range\n", + "print (F1(0), F1(1), F1(10), F1(15), F1(20))\n", + "print (F2(0), F2(1), F2(10), F2(15), F2(20))\n", + "\n", + "print (\"Number of Entries passing F1:\", len(where(data,F1)))\n", + "print (\"Number of Entries passing F2:\", len(where(data,F2)))" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "cFs4ujidWPcQ", + "colab_type": "code", + "colab": {} + }, + "source": [ + "### BEGIN SOLUTION\n", + "\n", + " # Fill in your solution here \n", + " \n", + "### END SOLUTION" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "uYSuZg12WPcT", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Test your solution" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "E4VnOWp1WPcW", + "colab_type": "text" + }, + "source": [ + "*Exercise 7:* Repeat the previous exercise using `lambda` and the built-in python functions sum and map instead of your solution above. " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "7EWEjRAxWPcX", + "colab_type": "code", + "colab": {} + }, + "source": [ + "### BEGIN SOLUTION\n", + "\n", + " # Fill in your solution here \n", + " \n", + "### END SOLUTION" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Lp8g13ePWPca", + "colab_type": "text" + }, + "source": [ + "## Monte Carlo\n", + "\n", + "*Exercise 7:* Write a \"generator\" function called `generate_function(func,x_min,x_max,N)`, that instead of generating a flat distribution, generates a distribution with functional form coded in `func`. Note that `func` will always be > 0. \n", + "\n", + "Use the test function below and your histogramming functions above to demonstrate that your generator is working properly.\n", + "\n", + "Hint: A simple, but slow, solution is to a draw random number test_x within the specified range and another number p between the min and max of the function (which you will have to determine). If p<=function(test_x), then place test_x on the output. If not, repeat the process, drawing two new numbers. Repeat until you have the specified number of generated numbers, N. For this problem, it's OK to determine the min and max by numerically sampling the function. " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "N4Ymt2uzWPcb", + "colab_type": "code", + "colab": {} + }, + "source": [ + "def generate_function(func,x_min,x_max,N=1000):\n", + " out = list()\n", + " ### BEGIN SOLUTION\n", + "\n", + " # Fill in your solution here \n", + " \n", + " ### END SOLUTION\n", + " \n", + " return out" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "rPHpwH-lWPcf", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# A test function\n", + "def test_func(x,a=1,b=1):\n", + " return abs(a*x+b)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "2MpHvGwwWPch", + "colab_type": "text" + }, + "source": [ + "*Exercise 8:* Use your function to generate 1000 numbers that are normal distributed, using the `gaussian` function below. Confirm the mean and variance of the data is close to the mean and variance you specify when building the Gaussian. Histogram the data. " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "E286Kc1iWPci", + "colab_type": "code", + "colab": {} + }, + "source": [ + "import math\n", + "\n", + "def gaussian(mean, sigma):\n", + " def f(x):\n", + " return math.exp(-((x-mean)**2)/(2*sigma**2))/math.sqrt(math.pi*sigma)\n", + " return f\n", + "\n", + "# Example Instantiation\n", + "g1=gaussian(0,1)\n", + "g2=gaussian(10,3)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Vk6pge1LWPcl", + "colab_type": "text" + }, + "source": [ + "*Exercise 9:* Combine your `generate_function`, `where`, and `in_range` functions above to create an integrate function. Use your integrate function to show that approximately 68% of Normal distribution is within one variance." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Qn0N1Dm7WPcm", + "colab_type": "code", + "colab": {} + }, + "source": [ + "def integrate(func, x_min, x_max, n_points=1000):\n", + " \n", + " return integral" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "ozJNVumNWPco", + "colab_type": "code", + "colab": {} + }, + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + } + ] +} \ No newline at end of file diff --git a/Labs/Lab-5/Lab_5_solutions.ipynb b/Labs/Lab-5/Lab_5_solutions.ipynb new file mode 100644 index 0000000..804d805 --- /dev/null +++ b/Labs/Lab-5/Lab_5_solutions.ipynb @@ -0,0 +1,270 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "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.1" + }, + "colab": { + "name": "Copy of Lab-5.ipynb", + "provenance": [] + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "TRayJDtTQAZw", + "colab_type": "text" + }, + "source": [ + "# Lab 5- Object Oriented Programming\n", + "\n", + "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github//afarbin/DATA1401-Spring-2020/blob/master/Labs/Lab-5/Lab-5.ipynb)\n", + "\n", + "For all of the exercises below, make sure you provide tests of your solutions.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "BkPr8ZW_QAZz", + "colab_type": "text" + }, + "source": [ + "1. Write a \"counter\" class that can be incremented up to a specified maximum value, will print an error if an attempt is made to increment beyond that value, and allows reseting the counter. " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "3IhLYuKBRKRz", + "colab_type": "code", + "colab": {} + }, + "source": [ + "class counter:\n", + " def __init__(self,max):\n", + " self.max=max\n", + " self.current=0\n", + " def increment(self):\n", + " if self.current>self.max:\n", + " print(\"Maximum value reached\")\n", + " else:\n", + " self.current+=1\n", + " def reset(self):\n", + " current=0" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "5ThNyQGmRKOp", + "colab_type": "code", + "colab": {} + }, + "source": [ + "my_counter=counter(3)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "o0w-RjwXTBC0", + "colab_type": "code", + "outputId": "9b87b80e-a054-4963-acd8-211131da369f", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "source": [ + "my_counter.increment()\n", + "my_counter.increment()\n", + "my_counter.increment()\n", + "my_counter.increment()\n", + "my_counter.increment()" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Maximum value reached\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "aLaXFKvvQAZ0", + "colab_type": "text" + }, + "source": [ + "2. Copy and paste your solution to question 1 and modify it so that all the data held by the counter is private. Implement functions to check the value of the counter, check the maximum value, and check if the counter is at the maximum." + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "Z5yUXxpQ_RJq", + "colab": {} + }, + "source": [ + "class counter:\n", + " __max=0\n", + " def __init__(self,max):\n", + " self.__max=max\n", + " self.__current=0\n", + " def increment(self):\n", + " if self.__current>self.__max:\n", + " print(\"Maximum value reached\")\n", + " else:\n", + " print(\"counter=\",self.__current)\n", + " self.__current+=1\n", + " def reset(self):\n", + " __current=0" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "XxQsjWXoBCPM", + "colab_type": "code", + "colab": {} + }, + "source": [ + "my_counter=counter(3)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "d4wle1BVBKzE", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "fb20de4b-d5b4-49d0-9c2e-643a0a182c19" + }, + "source": [ + "my_counter.increment\n", + "my_counter.increment\n", + "my_counter.increment\n", + "my_counter.increment" + ], + "execution_count": 7, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + ">" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 7 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "mrn9XfkbQAZ1", + "colab_type": "text" + }, + "source": [ + "3. Implement a class to represent a rectangle, holding the length, width, and $x$ and $y$ coordinates of a corner of the object. Implement functions that compute the area and parameter of the rectangle. Make all data members private and privide accessors to retrieve values of data members. " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Jc304uk_QAZ3", + "colab_type": "text" + }, + "source": [ + "4. Implement a class to represent a circle, holding the radius and $x$ and $y$ coordinates of center of the object. Implement functions that compute the area and parameter of the rectangle. Make all data members private and privide accessors to retrieve values of data members. " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Qlu-uxbbQAZ3", + "colab_type": "text" + }, + "source": [ + "5. Implement a common base class for the classes implemented in 3 and 4 above which implements all common methods as dummy functions. Re-implement those classes to inherit from the base class and overload the functions accordingly. " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "HIg7kFBeQAZ5", + "colab_type": "text" + }, + "source": [ + "6. Implement an analogous triangle class." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "c8pdxTCpQAZ7", + "colab_type": "text" + }, + "source": [ + "7. Add a function to the object classes that test if a given set of $x$ and $y$ coordinates are inside of the object." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "u41XJJSjQAZ9", + "colab_type": "text" + }, + "source": [ + "8. Add a function to the object classes that return a list of up to 16 pairs of $x$ and $y$ points on the parameter of the object.\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "_H4bZf8KQAZ-", + "colab_type": "text" + }, + "source": [ + "9. Add a function in the base class of the object classes that returns true/false testing that the object overlaps with another object." + ] + } + ] +} \ No newline at end of file