From 6cef46d16b73a1db305a40529a2ba37fb60e7cb3 Mon Sep 17 00:00:00 2001 From: Jerry-G77 <60523882+Jerry-G77@users.noreply.github.com> Date: Fri, 7 Feb 2020 14:50:41 -0600 Subject: [PATCH 1/9] Add files via upload --- Labs/Lab-1/Copy_of_Lab_1.ipynb | 879 +++++++++++++++++++++++++++++++++ 1 file changed, 879 insertions(+) create mode 100644 Labs/Lab-1/Copy_of_Lab_1.ipynb diff --git a/Labs/Lab-1/Copy_of_Lab_1.ipynb b/Labs/Lab-1/Copy_of_Lab_1.ipynb new file mode 100644 index 0000000..acb9e7a --- /dev/null +++ b/Labs/Lab-1/Copy_of_Lab_1.ipynb @@ -0,0 +1,879 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "colab": { + "name": "Copy of Lab-1.ipynb", + "provenance": [], + "collapsed_sections": [] + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "O5vg8KKRq0sy", + "colab_type": "text" + }, + "source": [ + "# Lab 1\n", + "\n", + "## Python Notebooks on Google Colab\n", + "\n", + "Data 1401's Labs, Homework, and Exams will be all in form of iPython notebooks. You may already be familiar with python notebooks if you have used Jupyter before, for example in Data 1301. If so, you are welcome to use whatever means you have to run Jupyter notebooks for this course, though you may get limited support. Our primary means of running python notebooks will be through [Google Colab](https://colab.research.google.com) and we will be storing files on google drive.\n", + "\n", + "You will need a google account. If you do not have one or you wish to use a different account for this course, please follow [these instructions](https://edu.gcfglobal.org/en/googledriveanddocs/getting-started-with-google-drive/1/) to make an account.\n", + "\n", + "Once you are ready with your account, you can continue in Colab. Click on the following badge to open this notebook in Colab:\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-1/Lab-1.ipynb)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "FVt_1hPt1dAK", + "colab_type": "text" + }, + "source": [ + "## Notebooks in Colab\n", + "\n", + "You now are presumably in Colab. Word of caution, by default, Google Colab does not save your notebooks, so if you close your session, you will loose your work.\n", + "\n", + "So first thing: from the file menu above select \"Save a copy in Drive\"." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "x0JBL_RFrDDj", + "colab_type": "text" + }, + "source": [ + "## Storing Notebooks in Google Drive\n", + "A better way to work is to save your notebooks directly into Google Drive and upload directly to Git (where you will be downloading and uploading your homework). In order properly setup Git, we'll need to work more directly in your Google Drive.\n", + "\n", + "On the left sidebar, press the file icon to see a listing of files accessibile to this Notebook. Then press \"Mount Drive\" and follow the instructions to mount your Google Drive in this notebook. A new cell will be inserted into this notebook, which after you run by pressing the play button will instruct you to follow a link to log into your Google Account and enable access to your Drive in another tab. Finally you will copy a link from the new tab back into the cell in this notebook. Once you are done, press refresh under files in the left sidebar and you should have \"drive/My Drive\" appear." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "JQFImC0XcpAl", + "colab_type": "code", + "outputId": "c0b9ec3f-d965-41a1-f482-f9f83edcfe87", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 129 + } + }, + "source": [ + "from google.colab import drive\n", + "drive.mount('/content/drive')" + ], + "execution_count": 1, + "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": { + "id": "hwJ6wJk3tiLv", + "colab_type": "text" + }, + "source": [ + "## Github\n", + "All the class material will be stored on github. You will also submit your homework using github. To do so, you will need a github account.\n", + "\n", + "If you do not already have a github account or wish to create a new one for this course, create one:\n", + "* Browse to [github.com](https://github.com).\n", + "* Click the green “Sign up for GitHub”\tbutton.\n", + "* Follow instructions for creating an account.\n", + "* Make sure you remember your github username and password.\n", + "\n", + "Write an email to the course TA titled \"Data 1401: Github account\" with your github username (not your password) as the contents.\n", + "\n", + "## Google Groups\n", + "\n", + "Class annoucements will be made via google groups. If you did not already receive an invite to the class google group, had trouble with the invite, or wish to use a different email address, write an email to the course TA titled \"Data 1401: Google Group\" with your preferred email.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "TjfIzdQZqvzk", + "colab_type": "text" + }, + "source": [ + "## Introduction: Unix, Git, and Jupyter\n", + "\n", + "This lab aims to introduce you to basic Unix, familiarize you with iPython notebooks and get you setup to submit your homework.\n", + "*italicized text*" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "C_LmOgzFqvzp", + "colab_type": "text" + }, + "source": [ + "\n", + "\n", + "### Terminal, Shell, and ssh\n", + "\n", + "\n", + "The terminal is a simple program that generally runs another program, taking mostly keyboard input from you, passing it to this other program, and taking the output of the program and displaying on the screen for you.\n", + "\n", + "The terminal usually runs a program called a shell. Shells present a command prompt where you can type in commands, which are then executed when you press enter. In most shells, there are some special commands which the shell will execute. Everything else you type in, the shell will assume is a name of a program you want to run and arguments you want to pass that program. So if the shell doesn't recognize something you type in, it'll try to find a program with a name that is the same as the first word you gave it. \n", + "\n", + "### Shell in Colab\n", + "\n", + "Unfortunately, google Colab does not allow you to open a terminal window. Jupyter does, so if you are running in Jupyter (which most of you will not be), you may choose to open a terminal window by returning to the jupyter file list tab and selecting new terminal from the top right.\n", + "\n", + "For Colab, we will have to do something non-ideal, but functional. There are several ways to execute shell commands from within a python notebook. For example, you can use any shell command by putting \"!\" in front of the command:\n", + "\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "KJ5f-WO0wcAv", + "colab_type": "code", + "outputId": "a5e2e7fa-dfb2-482f-8d60-e5d30cda098b", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 109 + } + }, + "source": [ + "!ls\n", + "!echo \"----------\"\n", + "!ls sample_data" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "sample_data\n", + "----------\n", + "anscombe.json\t\t mnist_test.csv\n", + "california_housing_test.csv mnist_train_small.csv\n", + "california_housing_train.csv README.md\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "8f-n4AXFw-dD", + "colab_type": "text" + }, + "source": [ + "Unfortunately, every time you use \"!\" a new environment is created and the state reverted to the original state. Try to understand the difference between the following two sets of commands:\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "99nrBYTWxZJr", + "colab_type": "code", + "outputId": "70ec737c-e73c-452c-c000-5f7e671c4f7b", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 72 + } + }, + "source": [ + "!echo \"Technique 1:\"\n", + "!ls\n", + "!cd sample_data\n", + "!ls" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Technique 1:\n", + "sample_data\n", + "sample_data\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "2-Znf97Lxl-Z", + "colab_type": "code", + "outputId": "b2de5f6e-4601-4c17-c352-34f241f788ad", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 109 + } + }, + "source": [ + "!echo \"Technique 2:\"\n", + "!ls ; cd sample_data ;ls" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Technique 2:\n", + "sample_data\n", + "anscombe.json\t\t mnist_test.csv\n", + "california_housing_test.csv mnist_train_small.csv\n", + "california_housing_train.csv README.md\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "4x9n1rAkxyYl", + "colab_type": "text" + }, + "source": [ + "Notebooks allow a bit of \"magic\" (using \"%\") to avoid some of these limitations:\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "vLBPTX4rx3gd", + "colab_type": "code", + "outputId": "776a3c01-0a11-4986-eca7-133a2811b2ea", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 127 + } + }, + "source": [ + "!echo \"Technique 3:\"\n", + "!ls \n", + "%cd sample_data \n", + "!ls" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Technique 3:\n", + "sample_data\n", + "/content/sample_data\n", + "anscombe.json\t\t mnist_test.csv\n", + "california_housing_test.csv mnist_train_small.csv\n", + "california_housing_train.csv README.md\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "U8XpvPjcyH0w", + "colab_type": "text" + }, + "source": [ + "For our purposes, we are just going to explicitly start a new shell and interact with it in the output cell. Execute the following cell. You will be able to type and execute commands. Look around a bit using \"ls\" and \"cd. You can stop the cell from running by typing \"exit\"." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "MIDFitLZyuZy", + "colab_type": "code", + "outputId": "1f5b6cdc-f8d3-4569-e44a-38aa1873ce65", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 127 + } + }, + "source": [ + "!/bin/bash --noediting" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "bash: cannot set terminal process group (130): Inappropriate ioctl for device\n", + "bash: no job control in this shell\n", + "\u001b]0;root@13f4a56d8591: /content/sample_data\u0007\u001b[01;32mroot@13f4a56d8591\u001b[00m:\u001b[01;34m/content/sample_data\u001b[00m# echo $SHELL\n", + "/bin/bash\n", + "\u001b]0;root@13f4a56d8591: /content/sample_data\u0007\u001b[01;32mroot@13f4a56d8591\u001b[00m:\u001b[01;34m/content/sample_data\u001b[00m# exit\n", + "exit\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "e0Njfr07mjjY", + "colab_type": "code", + "colab": {} + }, + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "q-4hfZBywW25", + "colab_type": "text" + }, + "source": [ + "While in this instance your shell is running in a this notebook, you can also run terminals natively on your own computer. On Linux or MacOS, you just have to run a program called terminal. In Windows you can start a \"command prompt\". \n", + "\n", + "\n", + "Type in \"ls\" into the terminal and press enter. The shell will find a program called \"ls\", a standard tool in Unix, and run it. \"ls\" lists the contents (files and directories) of your current directory. If you are just starting in this course, you probably only see the git repository you cloned. \n", + "\n", + "A subtle point to realize here is that while the terminal is running in the browser that is running on the computer in front of you, the shell is actually running on a machine on google hardware. The shell prompt typically displays the name of the machine you are using. What you are not seeing is that there is an intermidate program between the terminal running on your computer and the shell running on google. This intermidary program is taking your input from the terminal sending it over the network to google and bringing back the responses for you terminal to display.\n", + "\n", + "A bit of extra information. If you start a terminal on your own computer, the shell runs locally. The \"ls\" command would then list contents of a directory on your computer. You can typically connect to Unix computers by evoking a shell running on that machine over the network. In this case, you would have to initiate this intermidiary program yourself. The program is called \"ssh\" (secure shell). You can \"ssh\" to another machine from your machine, by simply typing \"ssh\" followed by the machine name or IP address. Most likely you would be prompted for a password, after which you would dropped into the prompt of a shell running on the remote machine. \n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "51Eya4LBqvzs", + "colab_type": "text" + }, + "source": [ + "## Programs and Environment Variables\n", + "\n", + "You have a listing of your current directory, but you don't know where that directory resides. You can see what directory you are using the command \"pwd\" (print working directory). Issue the command and look at the response. You'll get a slash (\"/\") separated list, known as the path, of the directory hierarchy of your current working directory. On Colab, this will start with \"contents\"\n", + "\n", + "Now back to thinking about the command prompt. Since \"ls\" is a program, it most be stored somewhere. It is clearly not in your working directory, because you didn't see it when you executed \"ls\". We can ask the shell to tell us where it found \"ls\" using the \"which ls\" command. Note that \"which\" is also a program. \"which ls\" comes back with \"/bin/ls\", telling you the \"ls\" program is sitting in \"/bin\" directory of the system. \n", + "\n", + "Lets see what else is in there by issuing a \"ls /bin\" command. You will get a long list of programs. You can run any of these programs by just typing their names and pressing enter. You may be able to guess what some of these programs do, but if you want to know, most of them provide you help, using \"--help\" or \"-h\" flag. For example execute \"ls --help\". For more information about a program or command, you can use Unix's manual pages using the \"man\" command. Try typing \"man ls\". Note that you will need to press space to scroll through lengthy manual pages and \"q\" to exit back to the shell prompt. \n", + "\n", + "Another command interesting is \"echo\". \"echo\" simply prints whatever you put after it to the screen. Try executing \"echo Hello World.\"\n", + "\n", + "At this point, you may wonder how was it that the shell knew to look for programs in \"/bin\"? The shell keeps a list of places to look for programs an environment variable with the name \"PATH\". The shell keeps a table that map string variable names to string expressions. When the shell starts, its configuration files set some environment variables that it uses. You can see the full list of defined environment variables using the command \"printenv\".\n", + "\n", + "You can use a environment variable in a shell by prepending name of the variable with a dollar sign character (\"\\$\"). So you can print out the PATH environment variable using the command \"echo $PATH\". What you will see is a colon (\":\") separated list of directories that the shell will search (in order) whenever you type in anything.\n", + "\n", + "You can set you own environment variables. Different shells have different syntax. Lets first figure out what shell we are running. \n", + "\n", + "*Exercise 1:* Use the \"echo\" command to print out the value of the \"SHELL\" environment variable:" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "YS7YFiPwqvzu", + "colab_type": "text" + }, + "source": [ + "!/bin/bash --noediting\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "YoEgruUhqvzw", + "colab_type": "text" + }, + "source": [ + "## Navigating Directories\n", + "\n", + "You can change your current directory using the \"cd\" shell command. Note that \"cd\" is not a Unix program. Once in a directory, you can use the \"ls\" command to list the contents or \"pwd\" to remind yourself your current working directory. You can move back one level in your current directory hierarchy using \"cd ..\". In general \"..\" represents the path to a directory one level above your current directory, \"../..\" represents two levels up, and so on. \".\" represents the current directory. If you look at the PATH environment variable, you'll notice that the last item is \".\", telling the shell to look into your current directory for commands. Finally the \"~\" character always refers to your home directory.\n", + "\n", + "Some other file manipulation commands:\n", + "\n", + " - The \"mkdir\" command creates new directories. \n", + " - \"cp\" and \"mv\" allow you to copy and move (or rename) files, taking 2 arguments: the original path/filename and the target path/filename. \n", + " - The \"rm\" and \"rmdir\" commands remove (delete) files and directories.\n", + "\n", + "\n", + "*Exercise 2:* Using the \"cd\" command, navigate into \"drive/My\\ Drive\" directory. Create a new directory called \"Data-1441\", and another directory inside \"Data-1441\" called \"Lab-1-Solutions\". Perform the rest of the lab in this directory." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "A16VzZ3G0J8x", + "colab_type": "code", + "outputId": "3a8321f8-1fe7-4ea9-81d7-a3459671c659", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 423 + } + }, + "source": [ + "!/bin/bash --noediting" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "bash: cannot set terminal process group (130): Inappropriate ioctl for device\n", + "bash: no job control in this shell\n", + "\u001b]0;root@13f4a56d8591: /content/sample_data\u0007\u001b[01;32mroot@13f4a56d8591\u001b[00m:\u001b[01;34m/content/sample_data\u001b[00m# echo $PATH\n", + "/usr/local/nvidia/bin:/usr/local/cuda/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/tools/node/bin:/tools/google-cloud-sdk/bin:/opt/bin\n", + "\u001b]0;root@13f4a56d8591: /content/sample_data\u0007\u001b[01;32mroot@13f4a56d8591\u001b[00m:\u001b[01;34m/content/sample_data\u001b[00m# cd\n", + "\u001b]0;root@13f4a56d8591: ~\u0007\u001b[01;32mroot@13f4a56d8591\u001b[00m:\u001b[01;34m~\u001b[00m# ls\n", + "\u001b]0;root@13f4a56d8591: ~\u0007\u001b[01;32mroot@13f4a56d8591\u001b[00m:\u001b[01;34m~\u001b[00m# cd drive/My\\Drive\n", + "bash: cd: drive/MyDrive: No such file or directory\n", + "\u001b]0;root@13f4a56d8591: ~\u0007\u001b[01;32mroot@13f4a56d8591\u001b[00m:\u001b[01;34m~\u001b[00m# cd drive\\My Drive\n", + "bash: cd: too many arguments\n", + "\u001b]0;root@13f4a56d8591: ~\u0007\u001b[01;32mroot@13f4a56d8591\u001b[00m:\u001b[01;34m~\u001b[00m# cd drive/my-drive\n", + "bash: cd: drive/my-drive: No such file or directory\n", + "\u001b]0;root@13f4a56d8591: ~\u0007\u001b[01;32mroot@13f4a56d8591\u001b[00m:\u001b[01;34m~\u001b[00m# cd /content/sample_data\n", + "\u001b]0;root@13f4a56d8591: /content/sample_data\u0007\u001b[01;32mroot@13f4a56d8591\u001b[00m:\u001b[01;34m/content/sample_data\u001b[00m# cd drive/My-Drive\n", + "bash: cd: drive/My-Drive: No such file or directory\n", + "\u001b]0;root@13f4a56d8591: /content/sample_data\u0007\u001b[01;32mroot@13f4a56d8591\u001b[00m:\u001b[01;34m/content/sample_data\u001b[00m# cd drive\\My-Drive\n", + "bash: cd: driveMy-Drive: No such file or directory\n", + "\u001b]0;root@13f4a56d8591: /content/sample_data\u0007\u001b[01;32mroot@13f4a56d8591\u001b[00m:\u001b[01;34m/content/sample_data\u001b[00m# cd /drive/My\\Drive\n", + "bash: cd: /drive/MyDrive: No such file or directory\n", + "\u001b]0;root@13f4a56d8591: /content/sample_data\u0007\u001b[01;32mroot@13f4a56d8591\u001b[00m:\u001b[01;34m/content/sample_data\u001b[00m# exit\n", + "exit\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "o38c4lbsqvzy", + "colab_type": "text" + }, + "source": [ + "## Exploring Unix Filesystem\n", + "\n", + "You can look at the root directory of the system by issuing \"ls /\". As explained in lecture, Unix uses the file system to communicate with devices and between processes. \"/etc\" keeps the configuration files of the system. \"/bin\" and \"/sbin\" store most of the standard Unix programs. \"/usr\" stores installes programs and their associate files, with \"/usr/bin\" usually storing the commands you can run. \n", + "\n", + "*Exercise 3:* List the \"/dev\" directory. How many SSD storage devices do you see? How many partitions does each device have? (Answer in box below)" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "yNj2LXzP2ksl", + "colab_type": "code", + "colab": {} + }, + "source": [ + "!/bin/bash --noediting" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "7P9EG0KOqvz2", + "colab_type": "text" + }, + "source": [ + "## Text File Manipulation\n", + "\n", + "As explained in lecture, Unix stores most information in text files. For example, the list of all users and their home directories are stored in \"/etc/passwd\". Let get some familiarity with the most commonly used commands to manipulate files.\n", + "\n", + " - You can see the contents contents a file using the \"cat\" (concatenate) command. Try executing \"cat /etc/passwd\". You'll get a huge list that will go by your screen quickly. \n", + " \n", + " - To go through the file page by page, you can use the \"less\" or \"more\" commands. \n", + " \n", + " - You can see the first or last N (N=10 by default) lines of a file using \"head\" or \"tail\" commands. For example \"tail -20 /etc/passwd\" will list the last 20 lines. \n", + " \n", + " - You can search a test file using the \"grep\" command, which takes a string keyword as the first argument and a filename as the second, and by default prints out every line in the file that contrains the string. So for example you can do \"grep \\$USER /etc/passwd\" to find the line corresponding to your account. Some useful flags: \n", + " \n", + " - \"-i\" ignores the case of the keyword\n", + " - \"-v\" display those lines that do NOT match \n", + " - \"-n\" precede each matching line with the line number \n", + " - \"-c\" print only the total count of matched lines \n", + " \n", + " For example \"grep -c \\$USER /etc/passwd\" should show that you are in the password file just once. \n", + " \n", + " - The \"wc\" (word count) command counts the number of lines, words, and characters in a file. By default \"wc\" gives you all three numbers, but \"-w\", \"-l\", or \"-c\" flags \n", + "\n", + "*Exercise 4:* Count how many lines in the password file contain the letter \"w\". " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "UlsANMuf2qMs", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 127 + }, + "outputId": "0bb56bf2-2777-45c1-e61a-fd79188ae777" + }, + "source": [ + "!/bin/bash --noediting" + ], + "execution_count": 2, + "outputs": [ + { + "output_type": "stream", + "text": [ + "bash: cannot set terminal process group (124): Inappropriate ioctl for device\n", + "bash: no job control in this shell\n", + "\u001b]0;root@858cf0527b93: /content\u0007\u001b[01;32mroot@858cf0527b93\u001b[00m:\u001b[01;34m/content\u001b[00m# grep \"w\" /etc/passwd | wc -l\n", + "3\n", + "\u001b]0;root@858cf0527b93: /content\u0007\u001b[01;32mroot@858cf0527b93\u001b[00m:\u001b[01;34m/content\u001b[00m# exit\n", + "exit\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "SZuhLbD8qvz5", + "colab_type": "text" + }, + "source": [ + "## Redirection\n", + "\n", + "Unix provides programs \"pipes\" for input and output. Most of what you see on the screen when you run a program was written to the \"stdout\" (standard output) pipe. Other pipes are \"stdin\" (standard input) and \"stderr\" (standard error), where error messages are written.\n", + "\n", + "As discussed in lecture, the basic commands of are simple, but you can chain them to do complicated things. Redirection is how you chain these commands, directing the output of one command to the input of the next.\n", + "\n", + "As an example, consider the \"cat\" command. Cat takes stdin and outputs it to stdout. Type \"cat\" and press enter and confirm. You can get back to the command prompt by pressing \"control-c\" (sends terminate singal) or \"control-d\" (end of file character). Note that from now on we will use the convention: \"control-d\" = \"^D\"\n", + "\n", + "*Exercise 5a:* Using \"cat\" and indirection you can write things into a file. The \">\" 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 From fbc72c237a0d82f3addccca17cffc9bf2c4a70e3 Mon Sep 17 00:00:00 2001 From: Jerry-G77 <60523882+Jerry-G77@users.noreply.github.com> Date: Thu, 13 Feb 2020 10:36:48 -0600 Subject: [PATCH 2/9] Created using Colaboratory --- Copy_of_Lab_2.ipynb | 1074 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 1074 insertions(+) create mode 100644 Copy_of_Lab_2.ipynb 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 From c5546bfedc7d50f3f3f96b78aad4787b7337c483 Mon Sep 17 00:00:00 2001 From: Jerry-G77 <60523882+Jerry-G77@users.noreply.github.com> Date: Fri, 14 Feb 2020 14:52:51 -0600 Subject: [PATCH 3/9] Add files via upload --- Labs/Lab-2/Lab_2-solutions.ipynb | 1231 ++++++++++++++++++++++++++++++ 1 file changed, 1231 insertions(+) create mode 100644 Labs/Lab-2/Lab_2-solutions.ipynb 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] Date: Fri, 28 Feb 2020 11:33:09 -0600 Subject: [PATCH 4/9] Add files via upload I could only get the rows and forward diagonal. I wasn't able to write an algorithm that checks columns or reverse diagonal --- Labs/Lab-3/Lab-3-Solutions.ipynb | 721 +++++++++++++++++++++++++++++++ 1 file changed, 721 insertions(+) create mode 100644 Labs/Lab-3/Lab-3-Solutions.ipynb diff --git a/Labs/Lab-3/Lab-3-Solutions.ipynb b/Labs/Lab-3/Lab-3-Solutions.ipynb new file mode 100644 index 0000000..a89f739 --- /dev/null +++ b/Labs/Lab-3/Lab-3-Solutions.ipynb @@ -0,0 +1,721 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "colab": { + "name": "Copy of Copy of Lab-3-part-3.ipynb", + "provenance": [], + "collapsed_sections": [] + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "y3z1vdNVKJB7", + "colab_type": "text" + }, + "source": [ + "# Lab 3- Tic Tac Toe\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-3/Lab-3.ipynb)\n", + "\n", + "In this lab your will build a n x n Tic Tac Toe game. As you do the exercises, make sure your solutions work for any size Tic Tac Toe game. " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ro6p7KHrKJB-", + "colab_type": "text" + }, + "source": [ + "*Exercise 1:* Write a function that creates an n by n matrix (of list of lists) which will represent the state of a Tie Tac Toe game. Let 0, 1, and 2 represent empty, \"X\", or \"O\".\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "deletable": true, + "editable": true, + "id": "nK4kuWiPKJCC", + "colab_type": "code", + "colab": {} + }, + "source": [ + "def TTT_board (num_space):\n", + " empty = 0\n", + " X = 1\n", + " O = 2\n", + " board = []\n", + " for i in range (num_space):\n", + " row=list()\n", + " for j in range(num_space):\n", + " row.append(empty)\n", + "\n", + " board.append(row)\n", + " board[2][3]=1\n", + " return board" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "deletable": true, + "editable": true, + "id": "eYFuqYCsKJCL", + "colab_type": "code", + "outputId": "3a32c8b8-703d-43cd-c037-0a7c39cbdcda", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 109 + } + }, + "source": [ + "TTT_board(5)" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "[[0, 0, 0, 0, 0],\n", + " [0, 0, 0, 0, 0],\n", + " [0, 0, 0, 1, 0],\n", + " [0, 0, 0, 0, 0],\n", + " [0, 0, 0, 0, 0]]" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 9 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "rzfxjmpRSjst", + "colab_type": "code", + "colab": {} + }, + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "deletable": true, + "editable": true, + "id": "qejcH-C3KJCR", + "colab_type": "text" + }, + "source": [ + "*Exercise 2:* Write a function that takes a `n` by `n` matrix representing a tic-tac-toe game, and returns -1, 0, 1, or 2 indicating the game is incomplete, the game is a draw, player 1 has won, or player 2 has one, respectively. Here are some example inputs you can use to test your code:" + ] + }, + { + "cell_type": "code", + "metadata": { + "deletable": true, + "editable": true, + "id": "YHD0P859KJCT", + "colab_type": "code", + "colab": {} + }, + "source": [ + "winner_is_2 = [[2, 2, 0],\n", + "\t[2, 1, 0],\n", + "\t[2, 1, 1]]\n", + "\n", + "winner_is_1 = [[1, 2, 0],\n", + "\t[2, 1, 0],\n", + "\t[2, 1, 1]]\n", + "\n", + "winner_is_also_1 = [[0, 1, 0],\n", + "\t[2, 1, 0],\n", + "\t[2, 1, 1]]\n", + "\n", + "no_winner = [[1, 2, 0],\n", + "\t[2, 1, 0],\n", + "\t[2, 1, 2]]\n", + "\n", + "also_no_winner = [[1, 2, 0],\n", + "\t[2, 1, 0],\n", + "\t[2, 1, 0]]" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "hl_XRFFNUOd8", + "colab_type": "code", + "colab": {} + }, + "source": [ + "def proto(result):\n", + " column_winner=0\n", + " row_winner=0\n", + " incomplete=0\n", + " game_status=-1\n", + " n=len(result[0])\n", + " #column check\n", + " player_1_col=0\n", + " player_2_col=0\n", + " diag_1=0\n", + " diag_2=0\n", + " empty_space=0\n", + " for j in range(len(result)):\n", + " player_1_rows=0\n", + " player_2_rows=0\n", + " row_winner=0\n", + " back=-1\n", + " #Row check\n", + " for k in range(len(result[0])):\n", + " if result[j][k]==1:\n", + " player_1_rows+=1\n", + " if result[j][k]==2:\n", + " player_2_rows+=1\n", + " if result [j][k]==0:\n", + " incomplete+=1\n", + " if player_1_rows==n:\n", + " row_winner=1\n", + " game_status=row_winner\n", + " if player_2_rows==n:\n", + " row_winner=2\n", + " game_status=row_winner\n", + " if incomplete==n:\n", + " print(\"number of zeros=\",incomplete)\n", + " print(\" row check-in loop:P1=\",player_1_rows,\"P2=\",player_2_rows)\n", + " print(\"row=\",j,\"column=\",k,row_winner,\"\\n\")\n", + " #diagonal check\n", + " diag_winner=0\n", + " if j==k:\n", + " if result[j][k]==1 or result[j][back]==1:\n", + " diag_1+=1\n", + " print(\"final cell in checked row=\",result[j][back])\n", + " if result[j][k]==2 or result[j][back]==2:\n", + " diag_2+=1\n", + " back-=1 \n", + " print(\"1st cell in diag check row=\",result[j][back])\n", + " print(\"diag_1=\",diag_1,\"diag_2=\",diag_2,\"back counter=\",back,\"\\n\") \n", + " if diag_1==n:\n", + " diag_winner=1\n", + " game_status=diag_winner\n", + " if diag_2==n:\n", + " diag_winner=2\n", + " game_status=diag_winner\n", + " if incomplete==n:\n", + " diag_winner=-1\n", + " game_status=diag_winner\n", + " #Column check\n", + " if result[j][k]==1:\n", + " player_1_col+=1\n", + " if result[j][k]==2:\n", + " player_2_col+=1\n", + " if result[j][k]==0:\n", + " incomplete+=1\n", + " if player_1_col==n:\n", + " column_winner=1\n", + " game_status=column_winner\n", + " if player_2_col==n:\n", + " column_winner=2\n", + " game_status=column_winner\n", + " if game_status!=1 and game_status!=2 and incomplete!=0:\n", + " print(\"The game is a draw\") \n", + " print(\"winner is\",game_status)\n", + " print(\"diagonal count=\",diag_1)\n", + " return game_status" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "deletable": true, + "editable": true, + "id": "CZk2yDmPKJCe", + "colab_type": "code", + "outputId": "a5f1ec52-87f1-4150-f909-8a82b19a1b4a", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 1000 + } + }, + "source": [ + "proto([[1,1,3,4],[2,1,2,2],[1,2,1,2],[0,1,2,1]])" + ], + "execution_count": 32, + "outputs": [ + { + "output_type": "stream", + "text": [ + " row check-in loop:P1= 1 P2= 0\n", + "row= 0 column= 0 0 \n", + "\n", + "final cell in checked row= 4\n", + "1st cell in diag check row= 3\n", + "diag_1= 1 diag_2= 0 back counter= -2 \n", + "\n", + " row check-in loop:P1= 2 P2= 0\n", + "row= 0 column= 1 0 \n", + "\n", + " row check-in loop:P1= 2 P2= 0\n", + "row= 0 column= 2 0 \n", + "\n", + " row check-in loop:P1= 2 P2= 0\n", + "row= 0 column= 3 0 \n", + "\n", + " row check-in loop:P1= 0 P2= 1\n", + "row= 1 column= 0 0 \n", + "\n", + " row check-in loop:P1= 1 P2= 1\n", + "row= 1 column= 1 0 \n", + "\n", + "final cell in checked row= 2\n", + "1st cell in diag check row= 2\n", + "diag_1= 2 diag_2= 1 back counter= -2 \n", + "\n", + " row check-in loop:P1= 1 P2= 2\n", + "row= 1 column= 2 0 \n", + "\n", + " row check-in loop:P1= 1 P2= 3\n", + "row= 1 column= 3 0 \n", + "\n", + " row check-in loop:P1= 1 P2= 0\n", + "row= 2 column= 0 0 \n", + "\n", + " row check-in loop:P1= 1 P2= 1\n", + "row= 2 column= 1 0 \n", + "\n", + " row check-in loop:P1= 2 P2= 1\n", + "row= 2 column= 2 0 \n", + "\n", + "final cell in checked row= 2\n", + "1st cell in diag check row= 1\n", + "diag_1= 3 diag_2= 2 back counter= -2 \n", + "\n", + " row check-in loop:P1= 2 P2= 2\n", + "row= 2 column= 3 0 \n", + "\n", + " row check-in loop:P1= 0 P2= 0\n", + "row= 3 column= 0 0 \n", + "\n", + " row check-in loop:P1= 1 P2= 0\n", + "row= 3 column= 1 0 \n", + "\n", + " row check-in loop:P1= 1 P2= 1\n", + "row= 3 column= 2 0 \n", + "\n", + " row check-in loop:P1= 2 P2= 1\n", + "row= 3 column= 3 0 \n", + "\n", + "final cell in checked row= 1\n", + "1st cell in diag check row= 2\n", + "diag_1= 4 diag_2= 2 back counter= -2 \n", + "\n", + "winner is 1\n", + "diagonal count= 4\n" + ], + "name": "stdout" + }, + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "1" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 32 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "deletable": true, + "editable": true, + "id": "-Dx0OKWGKJCi", + "colab_type": "text" + }, + "source": [ + "*Exercise 3:* Write a function that takes 2 integers `n` and `m` as input and draws a `n` by `m` game board. For example the following is a 3x3 board:\n", + "```\n", + " --- --- --- \n", + " | | | | \n", + " --- --- --- \n", + " | | | | \n", + " --- --- --- \n", + " | | | | \n", + " --- --- --- \n", + " ```" + ] + }, + { + "cell_type": "code", + "metadata": { + "deletable": true, + "editable": true, + "id": "eLmEHx2iKJCi", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Write you solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "deletable": true, + "editable": true, + "id": "6NK7rBGVKJCo", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Test your solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "deletable": true, + "editable": true, + "id": "38wcw3hVKJCs", + "colab_type": "text" + }, + "source": [ + "*Exercise 4:* Modify exercise 3, so that it takes a matrix of the form from exercise 2 and draws a tic-tac-tie board with \"X\"s and \"O\"s. " + ] + }, + { + "cell_type": "code", + "metadata": { + "deletable": true, + "editable": true, + "id": "ttiGCPG0KJCu", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Write you solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "deletable": true, + "editable": true, + "id": "vWSIjdNCKJCy", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Test your solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "deletable": true, + "editable": true, + "id": "tgTg2ohwKJC4", + "colab_type": "text" + }, + "source": [ + "*Exercise 5:* Write a function that takes a game board, player number, and `(x,y)` coordinates and places \"X\" or \"O\" in the correct location of the game board. Make sure that you only allow filling previously empty locations. Return `True` or `False` to indicate successful placement of \"X\" or \"O\"." + ] + }, + { + "cell_type": "code", + "metadata": { + "deletable": true, + "editable": true, + "id": "3Jfnl0MfKJC5", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Write you solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "deletable": true, + "editable": true, + "id": "UzBvZb6SKJC8", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Test your solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "deletable": true, + "editable": true, + "id": "o6Ibs9uNKJDB", + "colab_type": "text" + }, + "source": [ + "*Exercise 6:* Modify Exercise 4 to show column and row labels so that players can specify location using \"A2\" or \"C1\"." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "VlTa-VLGKJDC", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Write you solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "deletable": true, + "editable": true, + "id": "6ewYNJVQKJDG", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Test your solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "deletable": true, + "editable": true, + "id": "NcZNWEuOKJDJ", + "colab_type": "text" + }, + "source": [ + "*Exercise 7:* Write a function that takes a board, player number, and location specified as in exercise 6 and then calls exercise 5 to correctly modify the board. " + ] + }, + { + "cell_type": "code", + "metadata": { + "deletable": true, + "editable": true, + "id": "ruLXG_oOKJDL", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Write you solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "deletable": true, + "editable": true, + "id": "9od7IfYhKJDP", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Test your solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "deletable": true, + "editable": true, + "id": "3VynF5giKJDS", + "colab_type": "text" + }, + "source": [ + "*Exercise 8:* Write a function is called with a board and player number, takes input from the player using python's `input`, and modifies the board using your function from exercise 7. Note that you should keep asking for input until you have gotten a valid input that results in a valid move." + ] + }, + { + "cell_type": "code", + "metadata": { + "deletable": true, + "editable": true, + "id": "VccpTH-4KJDU", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Write you solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "deletable": true, + "editable": true, + "id": "4Osp2FMVKJDX", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Test your solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "deletable": true, + "editable": true, + "id": "5l84b0rPKJDa", + "colab_type": "text" + }, + "source": [ + "*Exercise 9:* Use all of the previous exercises to implement a full tic-tac-toe game, where an appropriate board is drawn, 2 players are repeatedly asked for a location coordinates of where they wish to place a mark, and the game status is checked until a player wins or a draw occurs." + ] + }, + { + "cell_type": "code", + "metadata": { + "deletable": true, + "editable": true, + "id": "QPW3YuPjKJDb", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Write you solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "deletable": true, + "editable": true, + "id": "Yoq0viEwKJDe", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Test your solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "deletable": true, + "editable": true, + "id": "wvq73NL3KJDh", + "colab_type": "text" + }, + "source": [ + "*Exercise 10:* Test that your game works for 5x5 Tic Tac Toe. " + ] + }, + { + "cell_type": "code", + "metadata": { + "deletable": true, + "editable": true, + "id": "8L0fl1gyKJDi", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Test your solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "deletable": true, + "editable": true, + "id": "Wiip51ggKJDk", + "colab_type": "text" + }, + "source": [ + "*Exercise 11: (Extra Credit)* Develop a version of the game where one player is the computer. Note that you don't need to do an extensive seach for the best move. You can have the computer simply protect against loosing and otherwise try to win with straight or diagonal patterns." + ] + }, + { + "cell_type": "code", + "metadata": { + "deletable": true, + "editable": true, + "id": "Mk5nXcEhKJDl", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Write you solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "deletable": true, + "editable": true, + "id": "eBnMXlhAKJDp", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Test your solution here" + ], + "execution_count": 0, + "outputs": [] + } + ] +} \ No newline at end of file From 0e46de4dd5e58ad724f870e4a52eb2e6c5407e7c Mon Sep 17 00:00:00 2001 From: Jerry-G77 <60523882+Jerry-G77@users.noreply.github.com> Date: Fri, 28 Feb 2020 14:35:04 -0600 Subject: [PATCH 5/9] Delete Lab-3-Solutions.ipynb --- Labs/Lab-3/Lab-3-Solutions.ipynb | 721 ------------------------------- 1 file changed, 721 deletions(-) delete mode 100644 Labs/Lab-3/Lab-3-Solutions.ipynb diff --git a/Labs/Lab-3/Lab-3-Solutions.ipynb b/Labs/Lab-3/Lab-3-Solutions.ipynb deleted file mode 100644 index a89f739..0000000 --- a/Labs/Lab-3/Lab-3-Solutions.ipynb +++ /dev/null @@ -1,721 +0,0 @@ -{ - "nbformat": 4, - "nbformat_minor": 0, - "metadata": { - "kernelspec": { - "name": "python3", - "display_name": "Python 3" - }, - "colab": { - "name": "Copy of Copy of Lab-3-part-3.ipynb", - "provenance": [], - "collapsed_sections": [] - } - }, - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "id": "y3z1vdNVKJB7", - "colab_type": "text" - }, - "source": [ - "# Lab 3- Tic Tac Toe\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-3/Lab-3.ipynb)\n", - "\n", - "In this lab your will build a n x n Tic Tac Toe game. As you do the exercises, make sure your solutions work for any size Tic Tac Toe game. " - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "ro6p7KHrKJB-", - "colab_type": "text" - }, - "source": [ - "*Exercise 1:* Write a function that creates an n by n matrix (of list of lists) which will represent the state of a Tie Tac Toe game. Let 0, 1, and 2 represent empty, \"X\", or \"O\".\n" - ] - }, - { - "cell_type": "code", - "metadata": { - "deletable": true, - "editable": true, - "id": "nK4kuWiPKJCC", - "colab_type": "code", - "colab": {} - }, - "source": [ - "def TTT_board (num_space):\n", - " empty = 0\n", - " X = 1\n", - " O = 2\n", - " board = []\n", - " for i in range (num_space):\n", - " row=list()\n", - " for j in range(num_space):\n", - " row.append(empty)\n", - "\n", - " board.append(row)\n", - " board[2][3]=1\n", - " return board" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "deletable": true, - "editable": true, - "id": "eYFuqYCsKJCL", - "colab_type": "code", - "outputId": "3a32c8b8-703d-43cd-c037-0a7c39cbdcda", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 109 - } - }, - "source": [ - "TTT_board(5)" - ], - "execution_count": 0, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "[[0, 0, 0, 0, 0],\n", - " [0, 0, 0, 0, 0],\n", - " [0, 0, 0, 1, 0],\n", - " [0, 0, 0, 0, 0],\n", - " [0, 0, 0, 0, 0]]" - ] - }, - "metadata": { - "tags": [] - }, - "execution_count": 9 - } - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "rzfxjmpRSjst", - "colab_type": "code", - "colab": {} - }, - "source": [ - "" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true, - "id": "qejcH-C3KJCR", - "colab_type": "text" - }, - "source": [ - "*Exercise 2:* Write a function that takes a `n` by `n` matrix representing a tic-tac-toe game, and returns -1, 0, 1, or 2 indicating the game is incomplete, the game is a draw, player 1 has won, or player 2 has one, respectively. Here are some example inputs you can use to test your code:" - ] - }, - { - "cell_type": "code", - "metadata": { - "deletable": true, - "editable": true, - "id": "YHD0P859KJCT", - "colab_type": "code", - "colab": {} - }, - "source": [ - "winner_is_2 = [[2, 2, 0],\n", - "\t[2, 1, 0],\n", - "\t[2, 1, 1]]\n", - "\n", - "winner_is_1 = [[1, 2, 0],\n", - "\t[2, 1, 0],\n", - "\t[2, 1, 1]]\n", - "\n", - "winner_is_also_1 = [[0, 1, 0],\n", - "\t[2, 1, 0],\n", - "\t[2, 1, 1]]\n", - "\n", - "no_winner = [[1, 2, 0],\n", - "\t[2, 1, 0],\n", - "\t[2, 1, 2]]\n", - "\n", - "also_no_winner = [[1, 2, 0],\n", - "\t[2, 1, 0],\n", - "\t[2, 1, 0]]" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "hl_XRFFNUOd8", - "colab_type": "code", - "colab": {} - }, - "source": [ - "def proto(result):\n", - " column_winner=0\n", - " row_winner=0\n", - " incomplete=0\n", - " game_status=-1\n", - " n=len(result[0])\n", - " #column check\n", - " player_1_col=0\n", - " player_2_col=0\n", - " diag_1=0\n", - " diag_2=0\n", - " empty_space=0\n", - " for j in range(len(result)):\n", - " player_1_rows=0\n", - " player_2_rows=0\n", - " row_winner=0\n", - " back=-1\n", - " #Row check\n", - " for k in range(len(result[0])):\n", - " if result[j][k]==1:\n", - " player_1_rows+=1\n", - " if result[j][k]==2:\n", - " player_2_rows+=1\n", - " if result [j][k]==0:\n", - " incomplete+=1\n", - " if player_1_rows==n:\n", - " row_winner=1\n", - " game_status=row_winner\n", - " if player_2_rows==n:\n", - " row_winner=2\n", - " game_status=row_winner\n", - " if incomplete==n:\n", - " print(\"number of zeros=\",incomplete)\n", - " print(\" row check-in loop:P1=\",player_1_rows,\"P2=\",player_2_rows)\n", - " print(\"row=\",j,\"column=\",k,row_winner,\"\\n\")\n", - " #diagonal check\n", - " diag_winner=0\n", - " if j==k:\n", - " if result[j][k]==1 or result[j][back]==1:\n", - " diag_1+=1\n", - " print(\"final cell in checked row=\",result[j][back])\n", - " if result[j][k]==2 or result[j][back]==2:\n", - " diag_2+=1\n", - " back-=1 \n", - " print(\"1st cell in diag check row=\",result[j][back])\n", - " print(\"diag_1=\",diag_1,\"diag_2=\",diag_2,\"back counter=\",back,\"\\n\") \n", - " if diag_1==n:\n", - " diag_winner=1\n", - " game_status=diag_winner\n", - " if diag_2==n:\n", - " diag_winner=2\n", - " game_status=diag_winner\n", - " if incomplete==n:\n", - " diag_winner=-1\n", - " game_status=diag_winner\n", - " #Column check\n", - " if result[j][k]==1:\n", - " player_1_col+=1\n", - " if result[j][k]==2:\n", - " player_2_col+=1\n", - " if result[j][k]==0:\n", - " incomplete+=1\n", - " if player_1_col==n:\n", - " column_winner=1\n", - " game_status=column_winner\n", - " if player_2_col==n:\n", - " column_winner=2\n", - " game_status=column_winner\n", - " if game_status!=1 and game_status!=2 and incomplete!=0:\n", - " print(\"The game is a draw\") \n", - " print(\"winner is\",game_status)\n", - " print(\"diagonal count=\",diag_1)\n", - " return game_status" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "deletable": true, - "editable": true, - "id": "CZk2yDmPKJCe", - "colab_type": "code", - "outputId": "a5f1ec52-87f1-4150-f909-8a82b19a1b4a", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 1000 - } - }, - "source": [ - "proto([[1,1,3,4],[2,1,2,2],[1,2,1,2],[0,1,2,1]])" - ], - "execution_count": 32, - "outputs": [ - { - "output_type": "stream", - "text": [ - " row check-in loop:P1= 1 P2= 0\n", - "row= 0 column= 0 0 \n", - "\n", - "final cell in checked row= 4\n", - "1st cell in diag check row= 3\n", - "diag_1= 1 diag_2= 0 back counter= -2 \n", - "\n", - " row check-in loop:P1= 2 P2= 0\n", - "row= 0 column= 1 0 \n", - "\n", - " row check-in loop:P1= 2 P2= 0\n", - "row= 0 column= 2 0 \n", - "\n", - " row check-in loop:P1= 2 P2= 0\n", - "row= 0 column= 3 0 \n", - "\n", - " row check-in loop:P1= 0 P2= 1\n", - "row= 1 column= 0 0 \n", - "\n", - " row check-in loop:P1= 1 P2= 1\n", - "row= 1 column= 1 0 \n", - "\n", - "final cell in checked row= 2\n", - "1st cell in diag check row= 2\n", - "diag_1= 2 diag_2= 1 back counter= -2 \n", - "\n", - " row check-in loop:P1= 1 P2= 2\n", - "row= 1 column= 2 0 \n", - "\n", - " row check-in loop:P1= 1 P2= 3\n", - "row= 1 column= 3 0 \n", - "\n", - " row check-in loop:P1= 1 P2= 0\n", - "row= 2 column= 0 0 \n", - "\n", - " row check-in loop:P1= 1 P2= 1\n", - "row= 2 column= 1 0 \n", - "\n", - " row check-in loop:P1= 2 P2= 1\n", - "row= 2 column= 2 0 \n", - "\n", - "final cell in checked row= 2\n", - "1st cell in diag check row= 1\n", - "diag_1= 3 diag_2= 2 back counter= -2 \n", - "\n", - " row check-in loop:P1= 2 P2= 2\n", - "row= 2 column= 3 0 \n", - "\n", - " row check-in loop:P1= 0 P2= 0\n", - "row= 3 column= 0 0 \n", - "\n", - " row check-in loop:P1= 1 P2= 0\n", - "row= 3 column= 1 0 \n", - "\n", - " row check-in loop:P1= 1 P2= 1\n", - "row= 3 column= 2 0 \n", - "\n", - " row check-in loop:P1= 2 P2= 1\n", - "row= 3 column= 3 0 \n", - "\n", - "final cell in checked row= 1\n", - "1st cell in diag check row= 2\n", - "diag_1= 4 diag_2= 2 back counter= -2 \n", - "\n", - "winner is 1\n", - "diagonal count= 4\n" - ], - "name": "stdout" - }, - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "1" - ] - }, - "metadata": { - "tags": [] - }, - "execution_count": 32 - } - ] - }, - { - "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true, - "id": "-Dx0OKWGKJCi", - "colab_type": "text" - }, - "source": [ - "*Exercise 3:* Write a function that takes 2 integers `n` and `m` as input and draws a `n` by `m` game board. For example the following is a 3x3 board:\n", - "```\n", - " --- --- --- \n", - " | | | | \n", - " --- --- --- \n", - " | | | | \n", - " --- --- --- \n", - " | | | | \n", - " --- --- --- \n", - " ```" - ] - }, - { - "cell_type": "code", - "metadata": { - "deletable": true, - "editable": true, - "id": "eLmEHx2iKJCi", - "colab_type": "code", - "colab": {} - }, - "source": [ - "# Write you solution here" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "deletable": true, - "editable": true, - "id": "6NK7rBGVKJCo", - "colab_type": "code", - "colab": {} - }, - "source": [ - "# Test your solution here" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true, - "id": "38wcw3hVKJCs", - "colab_type": "text" - }, - "source": [ - "*Exercise 4:* Modify exercise 3, so that it takes a matrix of the form from exercise 2 and draws a tic-tac-tie board with \"X\"s and \"O\"s. " - ] - }, - { - "cell_type": "code", - "metadata": { - "deletable": true, - "editable": true, - "id": "ttiGCPG0KJCu", - "colab_type": "code", - "colab": {} - }, - "source": [ - "# Write you solution here" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "deletable": true, - "editable": true, - "id": "vWSIjdNCKJCy", - "colab_type": "code", - "colab": {} - }, - "source": [ - "# Test your solution here" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true, - "id": "tgTg2ohwKJC4", - "colab_type": "text" - }, - "source": [ - "*Exercise 5:* Write a function that takes a game board, player number, and `(x,y)` coordinates and places \"X\" or \"O\" in the correct location of the game board. Make sure that you only allow filling previously empty locations. Return `True` or `False` to indicate successful placement of \"X\" or \"O\"." - ] - }, - { - "cell_type": "code", - "metadata": { - "deletable": true, - "editable": true, - "id": "3Jfnl0MfKJC5", - "colab_type": "code", - "colab": {} - }, - "source": [ - "# Write you solution here" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "deletable": true, - "editable": true, - "id": "UzBvZb6SKJC8", - "colab_type": "code", - "colab": {} - }, - "source": [ - "# Test your solution here" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true, - "id": "o6Ibs9uNKJDB", - "colab_type": "text" - }, - "source": [ - "*Exercise 6:* Modify Exercise 4 to show column and row labels so that players can specify location using \"A2\" or \"C1\"." - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "VlTa-VLGKJDC", - "colab_type": "code", - "colab": {} - }, - "source": [ - "# Write you solution here" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "deletable": true, - "editable": true, - "id": "6ewYNJVQKJDG", - "colab_type": "code", - "colab": {} - }, - "source": [ - "# Test your solution here" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true, - "id": "NcZNWEuOKJDJ", - "colab_type": "text" - }, - "source": [ - "*Exercise 7:* Write a function that takes a board, player number, and location specified as in exercise 6 and then calls exercise 5 to correctly modify the board. " - ] - }, - { - "cell_type": "code", - "metadata": { - "deletable": true, - "editable": true, - "id": "ruLXG_oOKJDL", - "colab_type": "code", - "colab": {} - }, - "source": [ - "# Write you solution here" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "deletable": true, - "editable": true, - "id": "9od7IfYhKJDP", - "colab_type": "code", - "colab": {} - }, - "source": [ - "# Test your solution here" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true, - "id": "3VynF5giKJDS", - "colab_type": "text" - }, - "source": [ - "*Exercise 8:* Write a function is called with a board and player number, takes input from the player using python's `input`, and modifies the board using your function from exercise 7. Note that you should keep asking for input until you have gotten a valid input that results in a valid move." - ] - }, - { - "cell_type": "code", - "metadata": { - "deletable": true, - "editable": true, - "id": "VccpTH-4KJDU", - "colab_type": "code", - "colab": {} - }, - "source": [ - "# Write you solution here" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "deletable": true, - "editable": true, - "id": "4Osp2FMVKJDX", - "colab_type": "code", - "colab": {} - }, - "source": [ - "# Test your solution here" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true, - "id": "5l84b0rPKJDa", - "colab_type": "text" - }, - "source": [ - "*Exercise 9:* Use all of the previous exercises to implement a full tic-tac-toe game, where an appropriate board is drawn, 2 players are repeatedly asked for a location coordinates of where they wish to place a mark, and the game status is checked until a player wins or a draw occurs." - ] - }, - { - "cell_type": "code", - "metadata": { - "deletable": true, - "editable": true, - "id": "QPW3YuPjKJDb", - "colab_type": "code", - "colab": {} - }, - "source": [ - "# Write you solution here" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "deletable": true, - "editable": true, - "id": "Yoq0viEwKJDe", - "colab_type": "code", - "colab": {} - }, - "source": [ - "# Test your solution here" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true, - "id": "wvq73NL3KJDh", - "colab_type": "text" - }, - "source": [ - "*Exercise 10:* Test that your game works for 5x5 Tic Tac Toe. " - ] - }, - { - "cell_type": "code", - "metadata": { - "deletable": true, - "editable": true, - "id": "8L0fl1gyKJDi", - "colab_type": "code", - "colab": {} - }, - "source": [ - "# Test your solution here" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true, - "id": "Wiip51ggKJDk", - "colab_type": "text" - }, - "source": [ - "*Exercise 11: (Extra Credit)* Develop a version of the game where one player is the computer. Note that you don't need to do an extensive seach for the best move. You can have the computer simply protect against loosing and otherwise try to win with straight or diagonal patterns." - ] - }, - { - "cell_type": "code", - "metadata": { - "deletable": true, - "editable": true, - "id": "Mk5nXcEhKJDl", - "colab_type": "code", - "colab": {} - }, - "source": [ - "# Write you solution here" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "deletable": true, - "editable": true, - "id": "eBnMXlhAKJDp", - "colab_type": "code", - "colab": {} - }, - "source": [ - "# Test your solution here" - ], - "execution_count": 0, - "outputs": [] - } - ] -} \ No newline at end of file From 8127f115901dfcafab0bfe4536a2b324003179a7 Mon Sep 17 00:00:00 2001 From: Jerry-G77 <60523882+Jerry-G77@users.noreply.github.com> Date: Fri, 28 Feb 2020 14:36:50 -0600 Subject: [PATCH 6/9] Add files via upload it can find winner, exercise 2 --- Labs/Lab-3/lab-3-solutions.ipynb | 682 +++++++++++++++++++++++++++++++ 1 file changed, 682 insertions(+) create mode 100644 Labs/Lab-3/lab-3-solutions.ipynb diff --git a/Labs/Lab-3/lab-3-solutions.ipynb b/Labs/Lab-3/lab-3-solutions.ipynb new file mode 100644 index 0000000..0a1a94a --- /dev/null +++ b/Labs/Lab-3/lab-3-solutions.ipynb @@ -0,0 +1,682 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "colab": { + "name": "Copy of Copy of Lab-3-part-3.ipynb", + "provenance": [], + "collapsed_sections": [] + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "y3z1vdNVKJB7", + "colab_type": "text" + }, + "source": [ + "# Lab 3- Tic Tac Toe\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-3/Lab-3.ipynb)\n", + "\n", + "In this lab your will build a n x n Tic Tac Toe game. As you do the exercises, make sure your solutions work for any size Tic Tac Toe game. " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ro6p7KHrKJB-", + "colab_type": "text" + }, + "source": [ + "*Exercise 1:* Write a function that creates an n by n matrix (of list of lists) which will represent the state of a Tie Tac Toe game. Let 0, 1, and 2 represent empty, \"X\", or \"O\".\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "deletable": true, + "editable": true, + "id": "nK4kuWiPKJCC", + "colab_type": "code", + "colab": {} + }, + "source": [ + "def TTT_board (num_space):\n", + " empty = 0\n", + " X = 1\n", + " O = 2\n", + " board = []\n", + " for i in range (num_space):\n", + " row=list()\n", + " for j in range(num_space):\n", + " row.append(empty)\n", + "\n", + " board.append(row)\n", + " board[2][3]=1\n", + " return board" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "deletable": true, + "editable": true, + "id": "eYFuqYCsKJCL", + "colab_type": "code", + "outputId": "3a32c8b8-703d-43cd-c037-0a7c39cbdcda", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 109 + } + }, + "source": [ + "TTT_board(5)" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "[[0, 0, 0, 0, 0],\n", + " [0, 0, 0, 0, 0],\n", + " [0, 0, 0, 1, 0],\n", + " [0, 0, 0, 0, 0],\n", + " [0, 0, 0, 0, 0]]" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 9 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "rzfxjmpRSjst", + "colab_type": "code", + "colab": {} + }, + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "deletable": true, + "editable": true, + "id": "qejcH-C3KJCR", + "colab_type": "text" + }, + "source": [ + "*Exercise 2:* Write a function that takes a `n` by `n` matrix representing a tic-tac-toe game, and returns -1, 0, 1, or 2 indicating the game is incomplete, the game is a draw, player 1 has won, or player 2 has one, respectively. Here are some example inputs you can use to test your code:" + ] + }, + { + "cell_type": "code", + "metadata": { + "deletable": true, + "editable": true, + "id": "YHD0P859KJCT", + "colab_type": "code", + "colab": {} + }, + "source": [ + "winner_is_2 = [[2, 2, 0],\n", + "\t[2, 1, 0],\n", + "\t[2, 1, 1]]\n", + "\n", + "winner_is_1 = [[1, 2, 0],\n", + "\t[2, 1, 0],\n", + "\t[2, 1, 1]]\n", + "\n", + "winner_is_also_1 = [[0, 1, 0],\n", + "\t[2, 1, 0],\n", + "\t[2, 1, 1]]\n", + "\n", + "no_winner = [[1, 2, 0],\n", + "\t[2, 1, 0],\n", + "\t[2, 1, 2]]\n", + "\n", + "also_no_winner = [[1, 2, 0],\n", + "\t[2, 1, 0],\n", + "\t[2, 1, 0]]" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "hl_XRFFNUOd8", + "colab_type": "code", + "colab": {} + }, + "source": [ + "def proto(result):\n", + " column_winner=0\n", + " row_winner=0\n", + " incomplete=0\n", + " game_status=-1\n", + " n=len(result[0])\n", + " #column check\n", + " diag_1=0\n", + " diag_2=0\n", + " empty_space=0\n", + " for j in range(len(result)):\n", + " player_1_col=0\n", + " player_2_col=0\n", + " player_1_rows=0\n", + " player_2_rows=0\n", + " row_winner=0\n", + " back=-1\n", + " #Row check\n", + " for k in range(len(result[0])):\n", + " if result[j][k]==1:\n", + " player_1_rows+=1\n", + " if result[j][k]==2:\n", + " player_2_rows+=1\n", + " if result [j][k]==0:\n", + " incomplete+=1\n", + " if player_1_rows==n:\n", + " row_winner=1\n", + " game_status=row_winner\n", + " if player_2_rows==n:\n", + " row_winner=2\n", + " game_status=row_winner\n", + " if game_status!=-1:\n", + " print(\"winner is player=\",game_status,\"\\n\")\n", + " #print(\" row check-in loop:P1=\",player_1_rows,\"P2=\",player_2_rows)\n", + " #print(\"row=\",j,\"column=\",k,row_winner,\"\\n\")\n", + " #diagonal check\n", + " diag_winner=0\n", + " if j==k:\n", + " if result[j][k]==1:\n", + " diag_1+=1\n", + " if result[j][k]==2:\n", + " diag_2+=1\n", + " if result[j][k]==result[k][j]:\n", + " if result[j][k]==1:\n", + " diag_1+=1\n", + " if result[j][k]==2:\n", + " diag_2+=1 \n", + " if diag_1==n:\n", + " diag_winner=1\n", + " game_status=diag_winner\n", + " if diag_2==n:\n", + " diag_winner=2\n", + " game_status=diag_winner\n", + " if incomplete==n:\n", + " diag_winner=-1\n", + " game_status=diag_winner\n", + " if game_status!=-1:\n", + " print(\"winning player=\",game_status,\"\\n\")\n", + " #Column check\n", + " print(\"row=\",j,\"column=\",k)\n", + " if result[k][j]==1:\n", + " player_1_col+=1\n", + " print(\"cell=\",result[k][j],\"play 1 col=\",player_1_col)\n", + " if result[k][j]==2:\n", + " player_2_col+=1\n", + " print(\"cell=\",result[k][j],\"play 2 col=\",player_2_col,\"\\n\")\n", + " if result[k][j]==0:\n", + " incomplete+=1\n", + " if player_1_col==n:\n", + " column_winner=1\n", + " game_status=column_winner\n", + " if player_2_col==n:\n", + " column_winner=2\n", + " game_status=column_winner\n", + " if game_status!=-1:\n", + " print(\"the winning player=\",game_status,\"\\n\")\n", + " return game_status\n", + " print(\"game status=\",game_status,\"\\n\")\n", + " if game_status!=1 and game_status!=2 and incomplete!=0:\n", + " print(\"The game is a draw\") \n", + " print(\"winner is\",game_status)\n", + " print(\"diagonal count=\",diag_1)\n", + " return game_status" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "deletable": true, + "editable": true, + "id": "CZk2yDmPKJCe", + "colab_type": "code", + "outputId": "505c95ca-863f-4327-f985-9126b4fd8d95", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 348 + } + }, + "source": [ + "proto([[1,2,2],[2,2,0],[2,0,1]])" + ], + "execution_count": 40, + "outputs": [ + { + "output_type": "stream", + "text": [ + "row= 0 column= 0\n", + "cell= 1 play 1 col= 1\n", + "row= 0 column= 1\n", + "cell= 2 play 2 col= 1 \n", + "\n", + "row= 0 column= 2\n", + "cell= 2 play 2 col= 2 \n", + "\n", + "game status= -1 \n", + "\n", + "winning player= 2 \n", + "\n", + "row= 1 column= 0\n", + "cell= 2 play 2 col= 1 \n", + "\n", + "the winning player= 2 \n", + "\n" + ], + "name": "stdout" + }, + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "2" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 40 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "deletable": true, + "editable": true, + "id": "-Dx0OKWGKJCi", + "colab_type": "text" + }, + "source": [ + "*Exercise 3:* Write a function that takes 2 integers `n` and `m` as input and draws a `n` by `m` game board. For example the following is a 3x3 board:\n", + "```\n", + " --- --- --- \n", + " | | | | \n", + " --- --- --- \n", + " | | | | \n", + " --- --- --- \n", + " | | | | \n", + " --- --- --- \n", + " ```" + ] + }, + { + "cell_type": "code", + "metadata": { + "deletable": true, + "editable": true, + "id": "eLmEHx2iKJCi", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Write you solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "deletable": true, + "editable": true, + "id": "6NK7rBGVKJCo", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Test your solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "deletable": true, + "editable": true, + "id": "38wcw3hVKJCs", + "colab_type": "text" + }, + "source": [ + "*Exercise 4:* Modify exercise 3, so that it takes a matrix of the form from exercise 2 and draws a tic-tac-tie board with \"X\"s and \"O\"s. " + ] + }, + { + "cell_type": "code", + "metadata": { + "deletable": true, + "editable": true, + "id": "ttiGCPG0KJCu", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Write you solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "deletable": true, + "editable": true, + "id": "vWSIjdNCKJCy", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Test your solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "deletable": true, + "editable": true, + "id": "tgTg2ohwKJC4", + "colab_type": "text" + }, + "source": [ + "*Exercise 5:* Write a function that takes a game board, player number, and `(x,y)` coordinates and places \"X\" or \"O\" in the correct location of the game board. Make sure that you only allow filling previously empty locations. Return `True` or `False` to indicate successful placement of \"X\" or \"O\"." + ] + }, + { + "cell_type": "code", + "metadata": { + "deletable": true, + "editable": true, + "id": "3Jfnl0MfKJC5", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Write you solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "deletable": true, + "editable": true, + "id": "UzBvZb6SKJC8", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Test your solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "deletable": true, + "editable": true, + "id": "o6Ibs9uNKJDB", + "colab_type": "text" + }, + "source": [ + "*Exercise 6:* Modify Exercise 4 to show column and row labels so that players can specify location using \"A2\" or \"C1\"." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "VlTa-VLGKJDC", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Write you solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "deletable": true, + "editable": true, + "id": "6ewYNJVQKJDG", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Test your solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "deletable": true, + "editable": true, + "id": "NcZNWEuOKJDJ", + "colab_type": "text" + }, + "source": [ + "*Exercise 7:* Write a function that takes a board, player number, and location specified as in exercise 6 and then calls exercise 5 to correctly modify the board. " + ] + }, + { + "cell_type": "code", + "metadata": { + "deletable": true, + "editable": true, + "id": "ruLXG_oOKJDL", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Write you solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "deletable": true, + "editable": true, + "id": "9od7IfYhKJDP", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Test your solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "deletable": true, + "editable": true, + "id": "3VynF5giKJDS", + "colab_type": "text" + }, + "source": [ + "*Exercise 8:* Write a function is called with a board and player number, takes input from the player using python's `input`, and modifies the board using your function from exercise 7. Note that you should keep asking for input until you have gotten a valid input that results in a valid move." + ] + }, + { + "cell_type": "code", + "metadata": { + "deletable": true, + "editable": true, + "id": "VccpTH-4KJDU", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Write you solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "deletable": true, + "editable": true, + "id": "4Osp2FMVKJDX", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Test your solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "deletable": true, + "editable": true, + "id": "5l84b0rPKJDa", + "colab_type": "text" + }, + "source": [ + "*Exercise 9:* Use all of the previous exercises to implement a full tic-tac-toe game, where an appropriate board is drawn, 2 players are repeatedly asked for a location coordinates of where they wish to place a mark, and the game status is checked until a player wins or a draw occurs." + ] + }, + { + "cell_type": "code", + "metadata": { + "deletable": true, + "editable": true, + "id": "QPW3YuPjKJDb", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Write you solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "deletable": true, + "editable": true, + "id": "Yoq0viEwKJDe", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Test your solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "deletable": true, + "editable": true, + "id": "wvq73NL3KJDh", + "colab_type": "text" + }, + "source": [ + "*Exercise 10:* Test that your game works for 5x5 Tic Tac Toe. " + ] + }, + { + "cell_type": "code", + "metadata": { + "deletable": true, + "editable": true, + "id": "8L0fl1gyKJDi", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Test your solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "deletable": true, + "editable": true, + "id": "Wiip51ggKJDk", + "colab_type": "text" + }, + "source": [ + "*Exercise 11: (Extra Credit)* Develop a version of the game where one player is the computer. Note that you don't need to do an extensive seach for the best move. You can have the computer simply protect against loosing and otherwise try to win with straight or diagonal patterns." + ] + }, + { + "cell_type": "code", + "metadata": { + "deletable": true, + "editable": true, + "id": "Mk5nXcEhKJDl", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Write you solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "deletable": true, + "editable": true, + "id": "eBnMXlhAKJDp", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Test your solution here" + ], + "execution_count": 0, + "outputs": [] + } + ] +} \ No newline at end of file From 958ba41e9c6fe6f6663df4bff1c580106ba223cd Mon Sep 17 00:00:00 2001 From: Jerry-G77 <60523882+Jerry-G77@users.noreply.github.com> Date: Fri, 27 Mar 2020 14:49:02 -0500 Subject: [PATCH 7/9] 1st one --- Labs/Lab-4/Lab_4_solutions.ipynb | 735 +++++++++++++++++++++++++++++++ 1 file changed, 735 insertions(+) create mode 100644 Labs/Lab-4/Lab_4_solutions.ipynb diff --git a/Labs/Lab-4/Lab_4_solutions.ipynb b/Labs/Lab-4/Lab_4_solutions.ipynb new file mode 100644 index 0000000..0b3ebab --- /dev/null +++ b/Labs/Lab-4/Lab_4_solutions.ipynb @@ -0,0 +1,735 @@ +{ + "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-4.ipynb", + "provenance": [] + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "Bu3CYr0bWPa-", + "colab_type": "text" + }, + "source": [ + "## Lab 4\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-4/Lab-4.ipynb)\n", + "\n", + "In this lab we will become familiar with distributions, histograms, and functional programming. \n", + "\n", + "\n", + "### Uniform Distribution\n", + "Lets start with generating some fake random data. You can get a random number between 0 and 1 using the python random module as follow:" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Iiz7WW1gWPbB", + "colab_type": "code", + "outputId": "a9b9217f-30a8-4ab0-ccc8-dedffcffe9a3", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "source": [ + "import random\n", + "x=random.random()\n", + "print(\"The Value of x is\", x)" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "The Value of x is 0.14273481367167185\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "YuCcV5WOWPbN", + "colab_type": "text" + }, + "source": [ + "Everytime you call random, you will get a new number.\n", + "\n", + "*Exercise 1:* Using random, write a function `generate_uniform(N, mymin, mymax)`, that returns a python list containing N random numbers between specified minimum and maximum value. Note that you may want to quickly work out on paper how to turn numbers between 0 and 1 to between other values. " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "WxbXWqW4WPbP", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Skeleton\n", + "def generate_uniform(N,x_min,x_max):\n", + " out = []\n", + " import random\n", + " for i in range(N):\n", + " temp=random.random()\n", + " temp2=(x_max-x_min)*temp + x_min\n", + " out.append(temp2)\n", + " # Fill in your solution here \n", + " \n", + " ### END SOLUTION\n", + " return out" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "EUhbQlL-WPbV", + "colab_type": "code", + "outputId": "a8f0bd78-3d5c-4596-9422-c81bdb272944", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 108 + } + }, + "source": [ + "# Test your solution here\n", + "data=generate_uniform(1000,-10,10)\n", + "print (\"Data Type:\", type(data))\n", + "print (\"Data Length:\", len(data))\n", + "if len(data)>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 From db224fa74720776548a07def350eca394a7beab3 Mon Sep 17 00:00:00 2001 From: Jerry-G77 <60523882+Jerry-G77@users.noreply.github.com> Date: Fri, 3 Apr 2020 14:40:12 -0500 Subject: [PATCH 8/9] Add files via upload I worked on it but I couldn't get it to work like it should --- Exams/Mid-term/Exam_solutions.ipynb | 338 ++++++++++++++++++++++++++++ 1 file changed, 338 insertions(+) create mode 100644 Exams/Mid-term/Exam_solutions.ipynb 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 Date: Fri, 17 Apr 2020 11:58:53 -0500 Subject: [PATCH 9/9] Add files via upload --- Labs/Lab-5/Lab_5_solutions.ipynb | 270 +++++++++++++++++++++++++++++++ 1 file changed, 270 insertions(+) create mode 100644 Labs/Lab-5/Lab_5_solutions.ipynb 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