From f7e613c96de210cd73f486355a0b826969474a8e Mon Sep 17 00:00:00 2001 From: justinkeas <60792432+justinkeas@users.noreply.github.com> Date: Fri, 7 Feb 2020 14:30:12 -0600 Subject: [PATCH 01/10] Add files via upload --- _Lab-1.ipynb solutions.ipynb | 1 + 1 file changed, 1 insertion(+) create mode 100644 _Lab-1.ipynb solutions.ipynb diff --git a/_Lab-1.ipynb solutions.ipynb b/_Lab-1.ipynb solutions.ipynb new file mode 100644 index 0000000..1519f7f --- /dev/null +++ b/_Lab-1.ipynb solutions.ipynb @@ -0,0 +1 @@ +{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":" Lab-1.ipynb solutions","provenance":[{"file_id":"1pFCXymfKHFjAO4PAAtXhGOltVMSTrXOr","timestamp":1581106736672},{"file_id":"https://github.com/afarbin/DATA1401-Spring-2020/blob/master/Labs/Lab-1/Lab-1.ipynb","timestamp":1581103948023}],"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":"O5vg8KKRq0sy"},"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":"code","metadata":{"id":"qpVKcvQYJHZM","colab_type":"code","outputId":"3b2a1056-f6b1-4587-d1e6-2c94961f9c8f","executionInfo":{"status":"ok","timestamp":1581104116480,"user_tz":360,"elapsed":75741,"user":{"displayName":"justin keas","photoUrl":"","userId":"03309259762371215646"}},"colab":{"base_uri":"https://localhost:8080/","height":121}},"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":"FVt_1hPt1dAK"},"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":{"colab_type":"text","id":"x0JBL_RFrDDj"},"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":"markdown","metadata":{"colab_type":"text","id":"hwJ6wJk3tiLv"},"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":{"colab_type":"text","id":"TjfIzdQZqvzk"},"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":{"colab_type":"text","id":"C_LmOgzFqvzp"},"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":{"colab_type":"code","id":"KJ5f-WO0wcAv","outputId":"9573de0b-8acb-41e5-f570-80fc7852bee8","executionInfo":{"status":"ok","timestamp":1581104567516,"user_tz":360,"elapsed":3396,"user":{"displayName":"justin keas","photoUrl":"","userId":"03309259762371215646"}},"colab":{"base_uri":"https://localhost:8080/","height":101}},"source":["!ls\n","!echo \"----------\"\n","!ls sample_data"],"execution_count":0,"outputs":[{"output_type":"stream","text":["drive 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":{"colab_type":"text","id":"8f-n4AXFw-dD"},"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":{"colab_type":"code","id":"99nrBYTWxZJr","outputId":"6da17a0d-4560-43e3-d599-16340885494f","executionInfo":{"status":"ok","timestamp":1581104625079,"user_tz":360,"elapsed":3493,"user":{"displayName":"justin keas","photoUrl":"","userId":"03309259762371215646"}},"colab":{"base_uri":"https://localhost:8080/","height":67}},"source":["!echo \"Technique 1:\"\n","!ls\n","!cd sample_data\n","!ls"],"execution_count":0,"outputs":[{"output_type":"stream","text":["Technique 1:\n","drive sample_data\n","drive sample_data\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"colab_type":"code","id":"2-Znf97Lxl-Z","outputId":"cad32cc5-8ca8-4191-f6b4-88764e8710cc","executionInfo":{"status":"ok","timestamp":1581104631920,"user_tz":360,"elapsed":2520,"user":{"displayName":"justin keas","photoUrl":"","userId":"03309259762371215646"}},"colab":{"base_uri":"https://localhost:8080/","height":101}},"source":["!echo \"Technique 2:\"\n","!ls ; cd sample_data ;ls"],"execution_count":0,"outputs":[{"output_type":"stream","text":["Technique 2:\n","drive 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":{"colab_type":"text","id":"4x9n1rAkxyYl"},"source":["Notebooks allow a bit of \"magic\" (using \"%\") to avoid some of these limitations:\n"]},{"cell_type":"code","metadata":{"colab_type":"code","id":"vLBPTX4rx3gd","outputId":"c0853e69-b8e3-423f-b7d7-0a7d8a2f5dfd","executionInfo":{"status":"ok","timestamp":1581104652689,"user_tz":360,"elapsed":2797,"user":{"displayName":"justin keas","photoUrl":"","userId":"03309259762371215646"}},"colab":{"base_uri":"https://localhost:8080/","height":118}},"source":["!echo \"Technique 3:\"\n","!ls \n","%cd sample_data \n","!ls"],"execution_count":0,"outputs":[{"output_type":"stream","text":["Technique 3:\n","drive 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":{"colab_type":"text","id":"U8XpvPjcyH0w"},"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":{"colab_type":"code","id":"MIDFitLZyuZy","outputId":"101876d5-aeb9-4230-b586-459858f696de","executionInfo":{"status":"ok","timestamp":1581104707901,"user_tz":360,"elapsed":32739,"user":{"displayName":"justin keas","photoUrl":"","userId":"03309259762371215646"}},"colab":{"base_uri":"https://localhost:8080/","height":84}},"source":["!/bin/bash --noediting"],"execution_count":0,"outputs":[{"output_type":"stream","text":["bash: cannot set terminal process group (127): Inappropriate ioctl for device\n","bash: no job control in this shell\n","\u001b]0;root@3af3fd4c4da6: /content/sample_data\u0007\u001b[01;32mroot@3af3fd4c4da6\u001b[00m:\u001b[01;34m/content/sample_data\u001b[00m# exit\n","exit\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"colab_type":"text","id":"q-4hfZBywW25"},"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":{"colab_type":"text","id":"51Eya4LBqvzs"},"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":{"colab_type":"text","id":"YS7YFiPwqvzu"},"source":["!/bin/bash --noediting"]},{"cell_type":"markdown","metadata":{"colab_type":"text","id":"YoEgruUhqvzw"},"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":{"colab_type":"code","id":"A16VzZ3G0J8x","outputId":"6dfe0a05-6410-43d6-e804-7e50943ee478","executionInfo":{"status":"ok","timestamp":1581105575299,"user_tz":360,"elapsed":4808,"user":{"displayName":"justin keas","photoUrl":"","userId":"03309259762371215646"}},"colab":{"base_uri":"https://localhost:8080/","height":101}},"source":["!cd \"drive/My\\Drive\"\n","!ls\n","!mkdir \"Data-1441\"\n","!cd \"Data-1441\"\n","!mkdir \"Lab-1-Solutions\"\n","\n"],"execution_count":0,"outputs":[{"output_type":"stream","text":["/bin/bash: line 0: cd: drive/My\\Drive: No such file or directory\n","anscombe.json\t\t Data-1441\t\t README.md\n","california_housing_test.csv mnist_test.csv\n","california_housing_train.csv mnist_train_small.csv\n","mkdir: cannot create directory ‘Data-1441’: File exists\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"colab_type":"text","id":"o38c4lbsqvzy"},"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":{"colab_type":"code","id":"yNj2LXzP2ksl","outputId":"a4024ec6-4c66-4c54-a7b9-5051db45c48f","executionInfo":{"status":"ok","timestamp":1581106187495,"user_tz":360,"elapsed":2402,"user":{"displayName":"justin keas","photoUrl":"","userId":"03309259762371215646"}},"colab":{"base_uri":"https://localhost:8080/","height":84}},"source":["!/dev\n","!ls\n","\n","\n","\n","\n"],"execution_count":0,"outputs":[{"output_type":"stream","text":["/bin/bash: /dev: Is a directory\n","anscombe.json\t\t Data-1441 mnist_train_small.csv\n","california_housing_test.csv Lab-1-Solutions README.md\n","california_housing_train.csv mnist_test.csv\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"colab_type":"text","id":"7P9EG0KOqvz2"},"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":{"colab_type":"code","id":"UlsANMuf2qMs","outputId":"7bc8fca4-cd53-4cba-fcef-d54ac23ce8cd","executionInfo":{"status":"ok","timestamp":1581106284921,"user_tz":360,"elapsed":1532,"user":{"displayName":"justin keas","photoUrl":"","userId":"03309259762371215646"}},"colab":{"base_uri":"https://localhost:8080/","height":34}},"source":["!\"cat/etc/passwd\""],"execution_count":0,"outputs":[{"output_type":"stream","text":["/bin/bash: cat/etc/passwd: No such file or directory\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"colab_type":"text","id":"SZuhLbD8qvz5"},"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":{"colab_type":"text","id":"H5vxtcXnqvz6"},"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":{"colab_type":"code","id":"twRKNaGy3XGw","outputId":"0b75cbb3-81dc-44ae-b6af-adeab82a0cf6","executionInfo":{"status":"error","timestamp":1581106631592,"user_tz":360,"elapsed":5325,"user":{"displayName":"justin keas","photoUrl":"","userId":"03309259762371215646"}},"colab":{"base_uri":"https://localhost:8080/","height":262}},"source":["!cat\n","!\"cat > favorite-colors-list.txt\"\n","purple\n","blue\n","black\n","\"^D\""],"execution_count":0,"outputs":[{"output_type":"stream","text":["^C\n","/bin/bash: cat > favorite-colors-list.txt: command not found\n"],"name":"stdout"},{"output_type":"error","ename":"NameError","evalue":"ignored","traceback":["\u001b[0;31m---------------------------------------------------------------------------\u001b[0m","\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)","\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mget_ipython\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msystem\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'cat'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mget_ipython\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msystem\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'\"cat > favorite-colors-list.txt\"'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mpurple\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0mblack\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;34m\"^D\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;31mNameError\u001b[0m: name 'purple' is not defined"]}]},{"cell_type":"markdown","metadata":{"colab_type":"text","id":"DZODNKiAqvz8"},"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":{"colab_type":"code","id":"oP9XlZl_3iZD","colab":{}},"source":["!/bin/bash --noediting"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"colab_type":"text","id":"v5IaZXNyqvz_"},"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":{"colab_type":"text","id":"J_R64sQDqv0A"},"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":{"colab_type":"text","id":"edTvE6rOqv0C"},"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":{"colab_type":"code","id":"e5tXg0f8qv0D","colab":{}},"source":["%cd /content/drive/My\\ Drive\n","!ls"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"colab_type":"text","id":"WYsyYcg1qv0J"},"source":["Make a new directory:"]},{"cell_type":"code","metadata":{"colab_type":"code","id":"Z7noY1hMqv0L","colab":{}},"source":["!mkdir Data-1401-Repo\n","%cd Data-1401-Repo"],"execution_count":0,"outputs":[]},{"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","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":{"colab_type":"text","id":"cOAuqTVUqv0V"},"source":["Go into the directory:"]},{"cell_type":"code","metadata":{"colab_type":"code","id":"b1Ew4tEZqv0X","colab":{}},"source":["%cd DATA1401-Spring-2020\n","!ls"],"execution_count":0,"outputs":[]},{"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","colab":{}},"source":["!git remote -v"],"execution_count":0,"outputs":[]},{"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","colab":{}},"source":["!git remote -v"],"execution_count":0,"outputs":[]},{"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":["We will submit your Lab 1 using git at the next Lab."]},{"cell_type":"code","metadata":{"colab_type":"code","id":"PPfGmFQI40HR","colab":{}},"source":[""],"execution_count":0,"outputs":[]}]} \ No newline at end of file From e727bea861f8c1f749a5debab9a913d55194d14e Mon Sep 17 00:00:00 2001 From: justinkeas <60792432+justinkeas@users.noreply.github.com> Date: Fri, 14 Feb 2020 13:39:27 -0600 Subject: [PATCH 02/10] Add files via upload --- Lab-2-solutions.ipynb.ipynb | 1 + 1 file changed, 1 insertion(+) create mode 100644 Lab-2-solutions.ipynb.ipynb diff --git a/Lab-2-solutions.ipynb.ipynb b/Lab-2-solutions.ipynb.ipynb new file mode 100644 index 0000000..2ca4804 --- /dev/null +++ b/Lab-2-solutions.ipynb.ipynb @@ -0,0 +1 @@ +{"nbformat":4,"nbformat_minor":0,"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3"},"colab":{"name":"Copy of Lab-2.ipynb","provenance":[{"file_id":"https://github.com/afarbin/DATA1401-Spring-2020/blob/master/Labs/Lab-2/Lab-2.ipynb","timestamp":1581108555939}],"collapsed_sections":[]}},"cells":[{"cell_type":"markdown","metadata":{"id":"uk7yc0nadBGa","colab_type":"text"},"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":"1dEHgy94Waf7","colab_type":"code","outputId":"ee5f1c54-134e-4e30-d139-73776ea2329a","executionInfo":{"status":"ok","timestamp":1581107532690,"user_tz":360,"elapsed":29216,"user":{"displayName":"","photoUrl":"","userId":""}},"colab":{"base_uri":"https://localhost:8080/","height":121}},"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":{"id":"OMNaOnRksNK3","colab_type":"text"},"source":["You may also choose to delete the fork from your GitHub account. "]},{"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":"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":"2u6B-rfNr1wN","colab_type":"code","colab":{}},"source":["!rm -rf drive/My\\ Drive/Data-1401-Repo"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"BDVI5nu8-2RH","colab_type":"text"},"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":"3620d8d5-5a45-40af-d329-ce62596edd14","executionInfo":{"status":"ok","timestamp":1581107594927,"user_tz":360,"elapsed":1258,"user":{"displayName":"","photoUrl":"","userId":""}},"colab":{"base_uri":"https://localhost:8080/","height":50}},"source":["%cd /content/drive/My\\ Drive\n","!ls"],"execution_count":0,"outputs":[{"output_type":"stream","text":["/content/drive/My Drive\n","'Colab Notebooks' 'Getting started.pdf'\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":"59cd33b8-79d2-4ae8-aba2-ed21081259f3","executionInfo":{"status":"ok","timestamp":1581107604272,"user_tz":360,"elapsed":1531,"user":{"displayName":"","photoUrl":"","userId":""}},"colab":{"base_uri":"https://localhost:8080/","height":34}},"source":["!mkdir Data-1401-Repo\n","%cd Data-1401-Repo"],"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":"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","colab":{}},"source":["# What you past here should look like:\n","#!git clone https://github.com/ Date: Fri, 14 Feb 2020 13:57:02 -0600 Subject: [PATCH 03/10] Add files via upload --- Labs/Lab-1/_Lab-1.ipynb solutions.ipynb | 1 + 1 file changed, 1 insertion(+) create mode 100644 Labs/Lab-1/_Lab-1.ipynb solutions.ipynb diff --git a/Labs/Lab-1/_Lab-1.ipynb solutions.ipynb b/Labs/Lab-1/_Lab-1.ipynb solutions.ipynb new file mode 100644 index 0000000..1519f7f --- /dev/null +++ b/Labs/Lab-1/_Lab-1.ipynb solutions.ipynb @@ -0,0 +1 @@ +{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":" Lab-1.ipynb solutions","provenance":[{"file_id":"1pFCXymfKHFjAO4PAAtXhGOltVMSTrXOr","timestamp":1581106736672},{"file_id":"https://github.com/afarbin/DATA1401-Spring-2020/blob/master/Labs/Lab-1/Lab-1.ipynb","timestamp":1581103948023}],"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":"O5vg8KKRq0sy"},"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":"code","metadata":{"id":"qpVKcvQYJHZM","colab_type":"code","outputId":"3b2a1056-f6b1-4587-d1e6-2c94961f9c8f","executionInfo":{"status":"ok","timestamp":1581104116480,"user_tz":360,"elapsed":75741,"user":{"displayName":"justin keas","photoUrl":"","userId":"03309259762371215646"}},"colab":{"base_uri":"https://localhost:8080/","height":121}},"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":"FVt_1hPt1dAK"},"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":{"colab_type":"text","id":"x0JBL_RFrDDj"},"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":"markdown","metadata":{"colab_type":"text","id":"hwJ6wJk3tiLv"},"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":{"colab_type":"text","id":"TjfIzdQZqvzk"},"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":{"colab_type":"text","id":"C_LmOgzFqvzp"},"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":{"colab_type":"code","id":"KJ5f-WO0wcAv","outputId":"9573de0b-8acb-41e5-f570-80fc7852bee8","executionInfo":{"status":"ok","timestamp":1581104567516,"user_tz":360,"elapsed":3396,"user":{"displayName":"justin keas","photoUrl":"","userId":"03309259762371215646"}},"colab":{"base_uri":"https://localhost:8080/","height":101}},"source":["!ls\n","!echo \"----------\"\n","!ls sample_data"],"execution_count":0,"outputs":[{"output_type":"stream","text":["drive 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":{"colab_type":"text","id":"8f-n4AXFw-dD"},"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":{"colab_type":"code","id":"99nrBYTWxZJr","outputId":"6da17a0d-4560-43e3-d599-16340885494f","executionInfo":{"status":"ok","timestamp":1581104625079,"user_tz":360,"elapsed":3493,"user":{"displayName":"justin keas","photoUrl":"","userId":"03309259762371215646"}},"colab":{"base_uri":"https://localhost:8080/","height":67}},"source":["!echo \"Technique 1:\"\n","!ls\n","!cd sample_data\n","!ls"],"execution_count":0,"outputs":[{"output_type":"stream","text":["Technique 1:\n","drive sample_data\n","drive sample_data\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"colab_type":"code","id":"2-Znf97Lxl-Z","outputId":"cad32cc5-8ca8-4191-f6b4-88764e8710cc","executionInfo":{"status":"ok","timestamp":1581104631920,"user_tz":360,"elapsed":2520,"user":{"displayName":"justin keas","photoUrl":"","userId":"03309259762371215646"}},"colab":{"base_uri":"https://localhost:8080/","height":101}},"source":["!echo \"Technique 2:\"\n","!ls ; cd sample_data ;ls"],"execution_count":0,"outputs":[{"output_type":"stream","text":["Technique 2:\n","drive 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":{"colab_type":"text","id":"4x9n1rAkxyYl"},"source":["Notebooks allow a bit of \"magic\" (using \"%\") to avoid some of these limitations:\n"]},{"cell_type":"code","metadata":{"colab_type":"code","id":"vLBPTX4rx3gd","outputId":"c0853e69-b8e3-423f-b7d7-0a7d8a2f5dfd","executionInfo":{"status":"ok","timestamp":1581104652689,"user_tz":360,"elapsed":2797,"user":{"displayName":"justin keas","photoUrl":"","userId":"03309259762371215646"}},"colab":{"base_uri":"https://localhost:8080/","height":118}},"source":["!echo \"Technique 3:\"\n","!ls \n","%cd sample_data \n","!ls"],"execution_count":0,"outputs":[{"output_type":"stream","text":["Technique 3:\n","drive 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":{"colab_type":"text","id":"U8XpvPjcyH0w"},"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":{"colab_type":"code","id":"MIDFitLZyuZy","outputId":"101876d5-aeb9-4230-b586-459858f696de","executionInfo":{"status":"ok","timestamp":1581104707901,"user_tz":360,"elapsed":32739,"user":{"displayName":"justin keas","photoUrl":"","userId":"03309259762371215646"}},"colab":{"base_uri":"https://localhost:8080/","height":84}},"source":["!/bin/bash --noediting"],"execution_count":0,"outputs":[{"output_type":"stream","text":["bash: cannot set terminal process group (127): Inappropriate ioctl for device\n","bash: no job control in this shell\n","\u001b]0;root@3af3fd4c4da6: /content/sample_data\u0007\u001b[01;32mroot@3af3fd4c4da6\u001b[00m:\u001b[01;34m/content/sample_data\u001b[00m# exit\n","exit\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"colab_type":"text","id":"q-4hfZBywW25"},"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":{"colab_type":"text","id":"51Eya4LBqvzs"},"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":{"colab_type":"text","id":"YS7YFiPwqvzu"},"source":["!/bin/bash --noediting"]},{"cell_type":"markdown","metadata":{"colab_type":"text","id":"YoEgruUhqvzw"},"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":{"colab_type":"code","id":"A16VzZ3G0J8x","outputId":"6dfe0a05-6410-43d6-e804-7e50943ee478","executionInfo":{"status":"ok","timestamp":1581105575299,"user_tz":360,"elapsed":4808,"user":{"displayName":"justin keas","photoUrl":"","userId":"03309259762371215646"}},"colab":{"base_uri":"https://localhost:8080/","height":101}},"source":["!cd \"drive/My\\Drive\"\n","!ls\n","!mkdir \"Data-1441\"\n","!cd \"Data-1441\"\n","!mkdir \"Lab-1-Solutions\"\n","\n"],"execution_count":0,"outputs":[{"output_type":"stream","text":["/bin/bash: line 0: cd: drive/My\\Drive: No such file or directory\n","anscombe.json\t\t Data-1441\t\t README.md\n","california_housing_test.csv mnist_test.csv\n","california_housing_train.csv mnist_train_small.csv\n","mkdir: cannot create directory ‘Data-1441’: File exists\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"colab_type":"text","id":"o38c4lbsqvzy"},"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":{"colab_type":"code","id":"yNj2LXzP2ksl","outputId":"a4024ec6-4c66-4c54-a7b9-5051db45c48f","executionInfo":{"status":"ok","timestamp":1581106187495,"user_tz":360,"elapsed":2402,"user":{"displayName":"justin keas","photoUrl":"","userId":"03309259762371215646"}},"colab":{"base_uri":"https://localhost:8080/","height":84}},"source":["!/dev\n","!ls\n","\n","\n","\n","\n"],"execution_count":0,"outputs":[{"output_type":"stream","text":["/bin/bash: /dev: Is a directory\n","anscombe.json\t\t Data-1441 mnist_train_small.csv\n","california_housing_test.csv Lab-1-Solutions README.md\n","california_housing_train.csv mnist_test.csv\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"colab_type":"text","id":"7P9EG0KOqvz2"},"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":{"colab_type":"code","id":"UlsANMuf2qMs","outputId":"7bc8fca4-cd53-4cba-fcef-d54ac23ce8cd","executionInfo":{"status":"ok","timestamp":1581106284921,"user_tz":360,"elapsed":1532,"user":{"displayName":"justin keas","photoUrl":"","userId":"03309259762371215646"}},"colab":{"base_uri":"https://localhost:8080/","height":34}},"source":["!\"cat/etc/passwd\""],"execution_count":0,"outputs":[{"output_type":"stream","text":["/bin/bash: cat/etc/passwd: No such file or directory\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"colab_type":"text","id":"SZuhLbD8qvz5"},"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":{"colab_type":"text","id":"H5vxtcXnqvz6"},"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":{"colab_type":"code","id":"twRKNaGy3XGw","outputId":"0b75cbb3-81dc-44ae-b6af-adeab82a0cf6","executionInfo":{"status":"error","timestamp":1581106631592,"user_tz":360,"elapsed":5325,"user":{"displayName":"justin keas","photoUrl":"","userId":"03309259762371215646"}},"colab":{"base_uri":"https://localhost:8080/","height":262}},"source":["!cat\n","!\"cat > favorite-colors-list.txt\"\n","purple\n","blue\n","black\n","\"^D\""],"execution_count":0,"outputs":[{"output_type":"stream","text":["^C\n","/bin/bash: cat > favorite-colors-list.txt: command not found\n"],"name":"stdout"},{"output_type":"error","ename":"NameError","evalue":"ignored","traceback":["\u001b[0;31m---------------------------------------------------------------------------\u001b[0m","\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)","\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mget_ipython\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msystem\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'cat'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mget_ipython\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msystem\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'\"cat > favorite-colors-list.txt\"'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mpurple\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0mblack\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;34m\"^D\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;31mNameError\u001b[0m: name 'purple' is not defined"]}]},{"cell_type":"markdown","metadata":{"colab_type":"text","id":"DZODNKiAqvz8"},"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":{"colab_type":"code","id":"oP9XlZl_3iZD","colab":{}},"source":["!/bin/bash --noediting"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"colab_type":"text","id":"v5IaZXNyqvz_"},"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":{"colab_type":"text","id":"J_R64sQDqv0A"},"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":{"colab_type":"text","id":"edTvE6rOqv0C"},"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":{"colab_type":"code","id":"e5tXg0f8qv0D","colab":{}},"source":["%cd /content/drive/My\\ Drive\n","!ls"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"colab_type":"text","id":"WYsyYcg1qv0J"},"source":["Make a new directory:"]},{"cell_type":"code","metadata":{"colab_type":"code","id":"Z7noY1hMqv0L","colab":{}},"source":["!mkdir Data-1401-Repo\n","%cd Data-1401-Repo"],"execution_count":0,"outputs":[]},{"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","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":{"colab_type":"text","id":"cOAuqTVUqv0V"},"source":["Go into the directory:"]},{"cell_type":"code","metadata":{"colab_type":"code","id":"b1Ew4tEZqv0X","colab":{}},"source":["%cd DATA1401-Spring-2020\n","!ls"],"execution_count":0,"outputs":[]},{"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","colab":{}},"source":["!git remote -v"],"execution_count":0,"outputs":[]},{"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","colab":{}},"source":["!git remote -v"],"execution_count":0,"outputs":[]},{"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":["We will submit your Lab 1 using git at the next Lab."]},{"cell_type":"code","metadata":{"colab_type":"code","id":"PPfGmFQI40HR","colab":{}},"source":[""],"execution_count":0,"outputs":[]}]} \ No newline at end of file From 08050760537dcbbd0d31d5f21efdf23c30978aba Mon Sep 17 00:00:00 2001 From: justinkeas <60792432+justinkeas@users.noreply.github.com> Date: Fri, 14 Feb 2020 13:58:10 -0600 Subject: [PATCH 04/10] Add files via upload --- Labs/Lab-2/Lab-2-solutions.ipynb.ipynb | 1 + 1 file changed, 1 insertion(+) create mode 100644 Labs/Lab-2/Lab-2-solutions.ipynb.ipynb diff --git a/Labs/Lab-2/Lab-2-solutions.ipynb.ipynb b/Labs/Lab-2/Lab-2-solutions.ipynb.ipynb new file mode 100644 index 0000000..2ca4804 --- /dev/null +++ b/Labs/Lab-2/Lab-2-solutions.ipynb.ipynb @@ -0,0 +1 @@ +{"nbformat":4,"nbformat_minor":0,"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3"},"colab":{"name":"Copy of Lab-2.ipynb","provenance":[{"file_id":"https://github.com/afarbin/DATA1401-Spring-2020/blob/master/Labs/Lab-2/Lab-2.ipynb","timestamp":1581108555939}],"collapsed_sections":[]}},"cells":[{"cell_type":"markdown","metadata":{"id":"uk7yc0nadBGa","colab_type":"text"},"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":"1dEHgy94Waf7","colab_type":"code","outputId":"ee5f1c54-134e-4e30-d139-73776ea2329a","executionInfo":{"status":"ok","timestamp":1581107532690,"user_tz":360,"elapsed":29216,"user":{"displayName":"","photoUrl":"","userId":""}},"colab":{"base_uri":"https://localhost:8080/","height":121}},"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":{"id":"OMNaOnRksNK3","colab_type":"text"},"source":["You may also choose to delete the fork from your GitHub account. "]},{"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":"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":"2u6B-rfNr1wN","colab_type":"code","colab":{}},"source":["!rm -rf drive/My\\ Drive/Data-1401-Repo"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"BDVI5nu8-2RH","colab_type":"text"},"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":"3620d8d5-5a45-40af-d329-ce62596edd14","executionInfo":{"status":"ok","timestamp":1581107594927,"user_tz":360,"elapsed":1258,"user":{"displayName":"","photoUrl":"","userId":""}},"colab":{"base_uri":"https://localhost:8080/","height":50}},"source":["%cd /content/drive/My\\ Drive\n","!ls"],"execution_count":0,"outputs":[{"output_type":"stream","text":["/content/drive/My Drive\n","'Colab Notebooks' 'Getting started.pdf'\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":"59cd33b8-79d2-4ae8-aba2-ed21081259f3","executionInfo":{"status":"ok","timestamp":1581107604272,"user_tz":360,"elapsed":1531,"user":{"displayName":"","photoUrl":"","userId":""}},"colab":{"base_uri":"https://localhost:8080/","height":34}},"source":["!mkdir Data-1401-Repo\n","%cd Data-1401-Repo"],"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":"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","colab":{}},"source":["# What you past here should look like:\n","#!git clone https://github.com/ Date: Fri, 28 Feb 2020 14:55:19 -0600 Subject: [PATCH 05/10] Add files via upload --- Labs/Lab-3/Lab-3-solutions.ipynb | 1 + 1 file changed, 1 insertion(+) 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..3a45778 --- /dev/null +++ b/Labs/Lab-3/Lab-3-solutions.ipynb @@ -0,0 +1 @@ +{"nbformat":4,"nbformat_minor":0,"metadata":{"kernelspec":{"display_name":"Python 2","language":"python","name":"python2"},"language_info":{"codemirror_mode":{"name":"ipython","version":2},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython2","version":"2.7.10"},"colab":{"name":"Copy of Lab-3.ipynb","provenance":[{"file_id":"https://github.com/afarbin/DATA1401-Spring-2020/blob/master/Labs/Lab-3/Lab-3.ipynb","timestamp":1582923039626}]}},"cells":[{"cell_type":"markdown","metadata":{"id":"kVhlgphts8yj","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":"code","metadata":{"id":"1Bv3X2-fta5l","colab_type":"code","outputId":"213f8978-0910-46dc-c5ae-90b4e74f294c","executionInfo":{"status":"ok","timestamp":1582170516405,"user_tz":360,"elapsed":25089,"user":{"displayName":"","photoUrl":"","userId":""}},"colab":{"base_uri":"https://localhost:8080/","height":121}},"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":{"id":"MxM9lNYZs8yn","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":"otD8SwKos8yo","colab_type":"code","colab":{}},"source":["# Write you solution \n","empty = 0\n","X = \"1\"\n","O = \"2\"\n","size = 3\n","\n","board=list()\n","for i in range(size):\n"," row=list()\n"," for j in range(size):\n"," row.append(empty)\n"," \n"," board.append(row)\n"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"deletable":true,"editable":true,"id":"vwCpfSiTs8yt","colab_type":"code","outputId":"396c1ead-864c-40a7-f304-047dbffca0a3","executionInfo":{"status":"ok","timestamp":1582920436594,"user_tz":360,"elapsed":727,"user":{"displayName":"","photoUrl":"","userId":""}},"colab":{"base_uri":"https://localhost:8080/","height":34}},"source":["# Test your solution here\n","board\n"," \n"," \n"," \n"],"execution_count":0,"outputs":[{"output_type":"execute_result","data":{"text/plain":["[[0, 0, 0], [0, 0, 0], [0, 0, 0]]"]},"metadata":{"tags":[]},"execution_count":110}]},{"cell_type":"markdown","metadata":{"deletable":true,"editable":true,"id":"2XtZzv1us8yy","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":"UEkA5pIFs8yz","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":{"deletable":true,"editable":true,"id":"QlMdc2Rms8y4","colab_type":"code","colab":{}},"source":["# Write you solution here\n","turn = '1' or '2'\n","count = 0\n","if count >= 5:\n"," if board[0][0] == board[0][1] == board[0][2] != 0:\n"," print('Game Over')\n"," print('player', turn, 'wins')\n"," elif board[1][0] == board[1][1] == board[1][2] !=0:\n"," print('Game Over')\n"," print ('player', turn, 'wins')\n"," elif board[2][0] == board[2][1] == board[2][2] !=0:\n"," print('Game Over')\n"," print ('player', turn, 'wins')\n"," elif board[0][0] == board[1][0] == board[2][0] !=0:\n"," print('Game Over')\n"," print ('player', turn, 'wins')\n"," elif board[0][1] == board[1][1] == board[2][1] !=0:\n"," print('Game Over')\n"," print ('player', turn, 'wins')\n"," elif board[0][2] == board[1][2] == board[2][2] !=0:\n"," print('Game Over')\n"," print ('player', turn, 'wins')\n"," elif board[0][0] == board[1][1] == board[2][2] !=0:\n"," print('Game Over')\n"," print ('player', turn, 'wins')\n"," elif board[0][2] == board[1][1] == board[2][0] !=0:\n"," print('Game Over')\n"," print ('player', turn, 'wins')\n","if count == 9:\n"," print('Game Over')\n"," print(\"It's a tie!\")\n","\n","\n","\n","\n","\n"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"deletable":true,"editable":true,"id":"y6SGvMxms8y7","colab_type":"code","colab":{}},"source":["# Test your solution here"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"deletable":true,"editable":true,"id":"R5FbHRoys8y_","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":"KShlolxis8zB","colab_type":"code","colab":{}},"source":["# Write you solution here\n","def draw_board(n, m):\n"," for row in range(n):\n"," line1=\"\"\n"," line2=\"\"\n"," for column in range(m):\n"," line1+=\" ---\"\n"," line2+=\" | \"\n"," line2+=\"|\"\n"," print(line1)\n"," print(line2)\n"," bottom=\"\"\n"," for column in range(m):\n"," bottom+=\" ---\"\n"," print(bottom)\n","\n"," "],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"deletable":true,"editable":true,"id":"O2Efwhy_s8zE","colab_type":"code","outputId":"c11838ba-9aa7-40fc-d740-513316c9896a","executionInfo":{"status":"ok","timestamp":1582921395211,"user_tz":360,"elapsed":305,"user":{"displayName":"","photoUrl":"","userId":""}},"colab":{"base_uri":"https://localhost:8080/","height":168}},"source":["# Test your solution here\n","draw_board(3,3)"],"execution_count":0,"outputs":[{"output_type":"stream","text":[" --- --- ---\n"," | | | |\n"," --- --- ---\n"," --- --- ---\n"," | | | |\n"," --- --- ---\n"," --- --- ---\n"," | | | |\n"," --- --- ---\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"deletable":true,"editable":true,"id":"aTW5iypys8zI","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":"x7yf4H27s8zJ","colab_type":"code","colab":{}},"source":["# Write you solution here\n","def draw_board(size):\n"," for row in range(size):\n"," line1=\"\"\n"," line2=\"\"\n"," for column in range(size):\n"," line1+=\" ---\"\n"," line2+=\" | \"\n"," line2+=\"| \"\n"," print(line1)\n"," print(line2)\n"," bottom=\"\"\n"," for column in range(size):\n"," bottom+=\" ---\"\n"," print(bottom)\n"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"deletable":true,"editable":true,"id":"YK9xNWsTs8zN","colab_type":"code","outputId":"7480529c-8dd7-4855-9c97-aef84fb9b63c","executionInfo":{"status":"ok","timestamp":1582922057117,"user_tz":360,"elapsed":635,"user":{"displayName":"","photoUrl":"","userId":""}},"colab":{"base_uri":"https://localhost:8080/","height":168}},"source":["# Test your solution here\n","game_board = draw_board(size)"],"execution_count":0,"outputs":[{"output_type":"stream","text":[" --- --- ---\n"," | | | | \n"," --- --- ---\n"," --- --- ---\n"," | | | | \n"," --- --- ---\n"," --- --- ---\n"," | | | | \n"," --- --- ---\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"deletable":true,"editable":true,"id":"P5LmTPd9s8zR","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":"hLpla5X_s8zT","colab_type":"code","colab":{}},"source":["# Write you solution here\n","def game():\n"," turn = 'X'\n"," count= 0\n"," for board in range(size):\n"," print(game_board)\n"," print(\"It's your turn\", turn, \"Pick a spot\")\n"," move=input()\n"," if board[move] == 0:\n"," board[move] = turn\n"," print(True)\n"," count+= 1\n"," else: \n"," print(False)\n"," print(\"spot already taken, pick another spot\")\n"," continue\n"," if turn == 'X':\n"," turn = 'O'\n"," else:\n"," turn = 'X'\n"," \n"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"deletable":true,"editable":true,"id":"st5a9CP7s8zZ","colab_type":"code","outputId":"35346571-9956-437c-ed94-150b31bf9da6","executionInfo":{"status":"error","timestamp":1582922410601,"user_tz":360,"elapsed":15985,"user":{"displayName":"","photoUrl":"","userId":""}},"colab":{"base_uri":"https://localhost:8080/","height":337}},"source":["# Test your solution here\n","game()\n"],"execution_count":0,"outputs":[{"output_type":"stream","text":["None\n","(\"It's your turn\", 'X', 'Pick a spot')\n"],"name":"stdout"},{"output_type":"error","ename":"KeyboardInterrupt","evalue":"ignored","traceback":["\u001b[0;31m\u001b[0m","\u001b[0;31mKeyboardInterrupt\u001b[0mTraceback (most recent call last)","\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mgame\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m","\u001b[0;32m\u001b[0m in \u001b[0;36mgame\u001b[0;34m()\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mgame_board\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0;32mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"It's your turn\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mturn\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"Pick a spot\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 7\u001b[0;31m \u001b[0mmove\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 8\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mboard\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mmove\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0mboard\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mmove\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mturn\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;32m/usr/local/lib/python2.7/dist-packages/ipykernel/ipkernel.pyc\u001b[0m in \u001b[0;36m\u001b[0;34m(prompt)\u001b[0m\n\u001b[1;32m 162\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_sys_eval_input\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mbuiltin_mod\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 163\u001b[0m \u001b[0mbuiltin_mod\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mraw_input\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mraw_input\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 164\u001b[0;31m \u001b[0mbuiltin_mod\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0minput\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mlambda\u001b[0m \u001b[0mprompt\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m''\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0meval\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mraw_input\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mprompt\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 165\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_save_getpass\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mgetpass\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgetpass\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 166\u001b[0m \u001b[0mgetpass\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgetpass\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgetpass\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;32m/usr/local/lib/python2.7/dist-packages/ipykernel/kernelbase.pyc\u001b[0m in \u001b[0;36mraw_input\u001b[0;34m(self, prompt)\u001b[0m\n\u001b[1;32m 703\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_parent_ident\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 704\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_parent_header\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 705\u001b[0;31m \u001b[0mpassword\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mFalse\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 706\u001b[0m )\n\u001b[1;32m 707\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;32m/usr/local/lib/python2.7/dist-packages/ipykernel/kernelbase.pyc\u001b[0m in \u001b[0;36m_input_request\u001b[0;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[1;32m 733\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 734\u001b[0m \u001b[0;31m# re-raise KeyboardInterrupt, to truncate traceback\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 735\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 736\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 737\u001b[0m \u001b[0;32mbreak\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;31mKeyboardInterrupt\u001b[0m: "]}]},{"cell_type":"markdown","metadata":{"deletable":true,"editable":true,"id":"d-RM1qh6s8zd","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":"Ia63Kcy9s8ze","colab_type":"code","colab":{}},"source":["# Write you solution here"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"deletable":true,"editable":true,"id":"mPXShxoIs8zi","colab_type":"code","colab":{}},"source":["# Test your solution here"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"deletable":true,"editable":true,"id":"mFQz1Q7Fs8zm","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":"W4aSS5HQs8zo","colab_type":"code","colab":{}},"source":["# Write you solution here"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"deletable":true,"editable":true,"id":"nuHSG8mjs8zr","colab_type":"code","colab":{}},"source":["# Test your solution here"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"deletable":true,"editable":true,"id":"zpbV17vPs8zv","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":"jI74Sp7Cs8zv","colab_type":"code","colab":{}},"source":["# Write you solution here"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"deletable":true,"editable":true,"id":"L8GJFhS_s8z1","colab_type":"code","colab":{}},"source":["# Test your solution here"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"deletable":true,"editable":true,"id":"n5dtKadus8z6","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":"TpatbeTas8z6","colab_type":"code","colab":{}},"source":["# Write you solution here"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"deletable":true,"editable":true,"id":"2zsF6leRs8z9","colab_type":"code","colab":{}},"source":["# Test your solution here"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"deletable":true,"editable":true,"id":"S41svkg8s80A","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":"OeksV7XGs80B","colab_type":"code","colab":{}},"source":["# Test your solution here"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"deletable":true,"editable":true,"id":"xWiX4TLys80E","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":"zacy-Q5Vs80F","colab_type":"code","colab":{}},"source":["# Write you solution here"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"deletable":true,"editable":true,"id":"HD5A38pVs80J","colab_type":"code","colab":{}},"source":["# Test your solution here"],"execution_count":0,"outputs":[]}]} \ No newline at end of file From 899b43ba62818058018e40c73055600c4b023483 Mon Sep 17 00:00:00 2001 From: justinkeas <60792432+justinkeas@users.noreply.github.com> Date: Fri, 3 Apr 2020 12:31:43 -0500 Subject: [PATCH 06/10] Add files via upload --- Exams/Mid-term/solutions to Exam (1).ipynb | 1 + 1 file changed, 1 insertion(+) create mode 100644 Exams/Mid-term/solutions to Exam (1).ipynb diff --git a/Exams/Mid-term/solutions to Exam (1).ipynb b/Exams/Mid-term/solutions to Exam (1).ipynb new file mode 100644 index 0000000..1939ac8 --- /dev/null +++ b/Exams/Mid-term/solutions to Exam (1).ipynb @@ -0,0 +1 @@ +{"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":"solutions to Exam.ipynb","provenance":[{"file_id":"https://github.com/afarbin/DATA1401-Spring-2020/blob/master/Exams/Mid-term/Exam.ipynb","timestamp":1585881117029}],"collapsed_sections":[]}},"cells":[{"cell_type":"markdown","metadata":{"id":"irrh64c2V-Sj","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":"J9OpPnQyV-Sl","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":"RT-YdX8PV-Sm","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":"TJgWpfsfV-Sr","colab_type":"text"},"source":["should return the string `\"Alabama\"`. Note that you can compare strings:"]},{"cell_type":"code","metadata":{"id":"Dd7TbMGvV-Ss","colab_type":"code","outputId":"fb24123f-9805-4ff2-be59-f987b0785f85","executionInfo":{"status":"ok","timestamp":1585929531099,"user_tz":300,"elapsed":482,"user":{"displayName":"justin keas","photoUrl":"","userId":"03309259762371215646"}},"colab":{"base_uri":"https://localhost:8080/","height":84}},"source":["print(\"A\">\"B\")\n","print(\"B\">\"A\")\n","print(\"A\">\"a\")\n","print(\"bca\">\"bbc\")"],"execution_count":83,"outputs":[{"output_type":"stream","text":["False\n","True\n","False\n","True\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"6NmvXJGfV-Sx","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":"q3kWbQlpWBIh","colab_type":"code","colab":{}},"source":["def first_alphabetically(lst):\n"," x=lst[0]\n"," for y in lst[1:]:\n"," if x>y:\n"," x=y\n"," return x"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"Bg2pG0ZuWC41","colab_type":"code","outputId":"de27eda8-3464-475b-a66d-7e1aae1c3930","executionInfo":{"status":"ok","timestamp":1585929533614,"user_tz":300,"elapsed":362,"user":{"displayName":"justin keas","photoUrl":"","userId":"03309259762371215646"}},"colab":{"base_uri":"https://localhost:8080/","height":34}},"source":["first_alphabetically(states)"],"execution_count":85,"outputs":[{"output_type":"execute_result","data":{"text/plain":["'Alabama'"]},"metadata":{"tags":[]},"execution_count":85}]},{"cell_type":"markdown","metadata":{"id":"NNgEjSjPV-Sy","colab_type":"text"},"source":["2. Write a function `arg_first_alphabetically(lst)`, which does the same thing as in exercise 1 but returns the index of the first string alphabetically."]},{"cell_type":"code","metadata":{"id":"nHlatmOBgF9Y","colab_type":"code","colab":{}},"source":["def arg_first_alphabetically(lst):\n"," x=lst[0]\n"," for y in lst[1:]:\n"," if x>y:\n"," x=y\n"," return lst.index(x)\n","\n"," "],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"NnwBTZYvgGa7","colab_type":"code","outputId":"6cd791d6-11b5-46a0-b2c3-3b030cb1b012","executionInfo":{"status":"ok","timestamp":1585929536440,"user_tz":300,"elapsed":348,"user":{"displayName":"justin keas","photoUrl":"","userId":"03309259762371215646"}},"colab":{"base_uri":"https://localhost:8080/","height":34}},"source":["arg_first_alphabetically(states)"],"execution_count":87,"outputs":[{"output_type":"execute_result","data":{"text/plain":["23"]},"metadata":{"tags":[]},"execution_count":87}]},{"cell_type":"markdown","metadata":{"id":"-3dyidHxV-Sz","colab_type":"text"},"source":["3. Use your result in question 2 to implement a function `arg_sort_alphabetically(lst)` that returns a list that is alphabetically sorted. Sorting can be accomplished by successively applying the function in question 1 and removing the first element alphabetically. You can remove an element from a list using `pop()`. 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":"jDzH_FTUgYja","colab_type":"code","colab":{}},"source":["def arg_sort_alphabetically(lst):\n"," for i in range(len(lst)):\n"," for j in range(len(lst) - 1): \n"," if lst[j] > lst[j+1]:\n"," lst[j+1], lst[j] = lst[j], lst[j +1]\n"," print(lst)"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"SIl8mIxngYui","colab_type":"code","outputId":"a9f2f3a3-6647-4106-d9a4-c8da152f1c15","executionInfo":{"status":"ok","timestamp":1585929539202,"user_tz":300,"elapsed":511,"user":{"displayName":"justin keas","photoUrl":"","userId":"03309259762371215646"}},"colab":{"base_uri":"https://localhost:8080/","height":54}},"source":["arg_sort_alphabetically(states)"],"execution_count":89,"outputs":[{"output_type":"stream","text":["['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming']\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"mo1vBo1xV-Sz","colab_type":"text"},"source":["4. Implement a function `outer_product` that takes two one-dimensional lists of numbers and returns the two-dimensional outer product matrix defined as:\n","\n","\\begin{equation*}\n","\\begin{pmatrix} x_1\\\\x_2\\\\ \\vdots \\\\x_m \\end{pmatrix} \\begin{pmatrix} y_1&y_2& \\dots &y_n\\end{pmatrix} =\n","\\begin{pmatrix}\n","x_1y_1 & x_1y_2 & \\dots & x_1y_n\\\\\n","x_2y_1 & x_2y_2 & \\dots & x_2y_n\\\\\n","\\vdots & \\vdots & \\ddots & \\vdots \\\\\n","x_my_1 & x_my_2 & \\dots & x_my_n\n","\\end{pmatrix}\n","\\end{equation*}\n","\n","In other words the elements of matrix C which is the outer product of A and B are $c_{ij} = a_i b_j$."]},{"cell_type":"code","metadata":{"id":"Tzd7nZiA4LQa","colab_type":"code","outputId":"cf1c1de4-f1a7-4733-fb59-9e7d84e5b510","executionInfo":{"status":"ok","timestamp":1585881100569,"user_tz":300,"elapsed":29172,"user":{"displayName":"","photoUrl":"","userId":""}},"colab":{"base_uri":"https://localhost:8080/","height":121}},"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":"code","metadata":{"id":"3BypDrm9gZ-1","colab_type":"code","colab":{}},"source":["def outer_product(A, B):\n"," result = []\n"," for i in range(len(A)): #this loops through columns of the matrix\n"," result = 0\n"," for j in range(len(B[0])): #this loops through vector coordinates & rows of matrix\n"," for k in range(len(B)): \n"," result[i][j] += A[i][k] * B[k][j] \n"," result.append(total)\n"," return result"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"gHOvrZvpgaLK","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":313},"outputId":"1ea7bc4f-2c05-4f97-ce72-278dd5c52c47","executionInfo":{"status":"error","timestamp":1585934715703,"user_tz":300,"elapsed":597,"user":{"displayName":"justin keas","photoUrl":"","userId":"03309259762371215646"}}},"source":["B=[2,2,2]\n","A=[[2],[2],[2]]\n","outer_product(A, B)"],"execution_count":138,"outputs":[{"output_type":"error","ename":"TypeError","evalue":"ignored","traceback":["\u001b[0;31m---------------------------------------------------------------------------\u001b[0m","\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)","\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mB\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mA\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mouter_product\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mA\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mB\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m","\u001b[0;32m\u001b[0m in \u001b[0;36mouter_product\u001b[0;34m(A, B)\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mA\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;31m#this loops through columns of the matrix\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0;32mfor\u001b[0m \u001b[0mj\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mB\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;31m#this loops through vector coordinates & rows of matrix\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 6\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mk\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mB\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0mresult\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mj\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m+=\u001b[0m \u001b[0mA\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mk\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0mB\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mk\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mj\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;31mTypeError\u001b[0m: object of type 'int' has no len()"]}]},{"cell_type":"markdown","metadata":{"id":"oNkRIYCOV-S0","colab_type":"text"},"source":["5. Implement a function `cumulative_sum(lst)` that takes a list of numbers and returns a list of same size where the element `i` is the sum of the elements `0` to `i` of the input list. For example given `[1,2,3]`, you should return [1,3,6]."]},{"cell_type":"code","metadata":{"id":"plMBNH0XgbC7","colab_type":"code","colab":{}},"source":["def cumulative_sum(lst):\n"," i = []\n"," cumsum = 0\n"," for element in lst:\n"," cumsum+=element\n"," i.append(cumsum)\n"," return i \n","\n","numbers = [1,2,3]\n"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"C2Kt5AH0gbPH","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":34},"outputId":"ada19fc3-ed45-4d76-cb1b-bafb2d196dfa","executionInfo":{"status":"ok","timestamp":1585928693960,"user_tz":300,"elapsed":545,"user":{"displayName":"justin keas","photoUrl":"","userId":"03309259762371215646"}}},"source":["cumulative_sum(numbers)"],"execution_count":79,"outputs":[{"output_type":"execute_result","data":{"text/plain":["[1, 3, 6]"]},"metadata":{"tags":[]},"execution_count":79}]},{"cell_type":"markdown","metadata":{"id":"sa42puKhV-S1","colab_type":"text"},"source":["6. Imagine you have a normal distributed random variable `x`. For example `x` can be grades on this exam. Using the normal distribution generator and histogram functions from lecture (provided below) and `cumulative_sum` from previous question to compute what is the value of $x_{90}$ in $\\sigma$ such that 90% of the values $x$ are below $x_{90}$. In other words:"]},{"cell_type":"markdown","metadata":{"id":"VJr_CgzKV-S3","colab_type":"text"},"source":["$$\n","\\int_{-\\infty}^{x_{90}} N(x;\\mu=0,\\sigma=1) dx = 0.9\n","$$"]},{"cell_type":"code","metadata":{"id":"9cGhLL7KV-S3","colab_type":"code","colab":{}},"source":["import math,random\n","\n","def arange(x_min,x_max,steps=10):\n"," step_size=(x_max-x_min)/steps\n"," x=x_min\n"," out = list()\n"," for i in range(steps):\n"," out.append(x)\n"," x+=step_size\n"," return out\n","\n","def generate_normal(N,m=0,s=1):\n"," out = list() \n"," \n"," while len(out)=bin_edges[i] and d Date: Fri, 17 Apr 2020 14:18:36 -0500 Subject: [PATCH 07/10] Add files via upload --- Labs/Lab-5/Lab-5solutions.ipynb | 1 + 1 file changed, 1 insertion(+) create mode 100644 Labs/Lab-5/Lab-5solutions.ipynb diff --git a/Labs/Lab-5/Lab-5solutions.ipynb b/Labs/Lab-5/Lab-5solutions.ipynb new file mode 100644 index 0000000..4cdc14a --- /dev/null +++ b/Labs/Lab-5/Lab-5solutions.ipynb @@ -0,0 +1 @@ +{"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":"Lab-5solutions.ipynb","provenance":[{"file_id":"https://github.com/afarbin/DATA1401-Spring-2020/blob/master/Labs/Lab-5/Lab-5.ipynb","timestamp":1587150783632}],"collapsed_sections":[],"toc_visible":true}},"cells":[{"cell_type":"markdown","metadata":{"id":"PFr7jcfRWMkk","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":"code","metadata":{"id":"Q9NOkVg0WR9I","colab_type":"code","outputId":"9b3bc55e-918d-42af-833a-9630df97ae6a","executionInfo":{"status":"ok","timestamp":1587147281544,"user_tz":300,"elapsed":24579,"user":{"displayName":"","photoUrl":"","userId":""}},"colab":{"base_uri":"https://localhost:8080/","height":121}},"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":{"id":"s7XtWCuXWMkl","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":"84qYCWGzWMkl","colab_type":"code","colab":{}},"source":["class counter:\n"," def __init__(self,max_val):\n"," self.max_val=max_val\n"," self.cur_val=1\n"," \n"," def increment(self):\n"," if self.cur_val>self.max_val:\n"," print(\"Max value reached.\")\n"," else:\n"," self.cur_val+=1\n"," \n"," def reset(self):\n"," self.cur_val=1\n"," "],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"Feb_mmqAWMks","colab_type":"code","colab":{}},"source":["my_counter=counter(3)"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"xZzR5KWIWMkv","colab_type":"code","outputId":"bf6bb316-3aa4-42aa-9d71-cacb24474dc8","executionInfo":{"status":"ok","timestamp":1587147294585,"user_tz":300,"elapsed":431,"user":{"displayName":"","photoUrl":"","userId":""}},"colab":{"base_uri":"https://localhost:8080/","height":50}},"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":["Max value reached.\n","Max value reached.\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"tLvX2QPVWMk0","colab_type":"code","colab":{}},"source":["my_counter.cur_val=100"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"Aeal-kdAWMk3","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":{"id":"-o83luo_WMk5","colab_type":"code","colab":{}},"source":["class counter:\n"," def __init__(self,max_val):\n"," self.__max_val=max_val\n"," self.__cur_val=1\n"," \n"," def increment(self):\n"," if self.__cur_val>self.__max_val:\n"," print(\"Max value reached.\")\n"," else:\n"," self.__cur_val+=1\n"," \n"," def reset(self):\n"," self.__cur_val=1\n"," \n"," def cur_val(self):\n"," return self.__cur_val\n","\n"," def max_val(self):\n"," return self.__max_val\n","\n"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"GaN9qXXmWMk9","colab_type":"code","colab":{}},"source":["my_counter=counter(3)"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"Z6pWHSQBWMlB","colab_type":"code","outputId":"151f3e69-2ecb-4d65-a679-8940d233bcc6","executionInfo":{"status":"ok","timestamp":1587147299463,"user_tz":300,"elapsed":323,"user":{"displayName":"","photoUrl":"","userId":""}},"colab":{"base_uri":"https://localhost:8080/","height":34}},"source":["my_counter.cur_val()"],"execution_count":0,"outputs":[{"output_type":"execute_result","data":{"text/plain":["1"]},"metadata":{"tags":[]},"execution_count":8}]},{"cell_type":"markdown","metadata":{"id":"o5y5_kOeWMlF","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":"code","metadata":{"id":"ijz4vhEDWMlF","colab_type":"code","colab":{}},"source":["class rectangle:\n"," def __init__(self,width,length,x,y):\n"," self.__width=width\n"," self.__length=length\n"," self.__x=x\n"," self.__y=y\n"," \n"," def area(self):\n"," return self.__width*self.__length\n"," \n"," def perimeter(self):\n"," return 2*(self.__width+self.__length)\n"," \n"," def x(self):\n"," return self.__x\n"," \n"," def y(self):\n"," return self.__y\n","\n"," "],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"qaRgm1wsWlj5","colab_type":"code","outputId":"dc04971c-cb9a-43e0-eaba-2497a4571591","executionInfo":{"status":"ok","timestamp":1587147393643,"user_tz":300,"elapsed":350,"user":{"displayName":"","photoUrl":"","userId":""}},"colab":{"base_uri":"https://localhost:8080/","height":34}},"source":["my_rectangle=rectangle(5,6,5,6)\n","my_rectangle.area()"],"execution_count":0,"outputs":[{"output_type":"execute_result","data":{"text/plain":["30"]},"metadata":{"tags":[]},"execution_count":13}]},{"cell_type":"markdown","metadata":{"id":"x9wXmbt3WMlJ","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":"code","metadata":{"id":"1wQDrrOsW2L1","colab_type":"code","colab":{}},"source":["class circle:\n"," def __init__(self, x, y, radius):\n"," self.__x=x\n"," self.__y=y\n"," self.__radius=radius\n","\n"," def area(self):\n"," return (self.__radius**2)*(22/7)\n"," \n"," def circumference(self):\n"," return self.__radius*2*(22/7)\n","\n"," def radius(self):\n"," return self.__radius\n","\n"," def x(self):\n"," return self.__x\n","\n"," def y(self):\n"," return self.__y"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"FbsrIw43W3m4","colab_type":"code","outputId":"38aeea90-9710-411d-c012-99aa38f3a8f2","executionInfo":{"status":"ok","timestamp":1587147952990,"user_tz":300,"elapsed":279,"user":{"displayName":"","photoUrl":"","userId":""}},"colab":{"base_uri":"https://localhost:8080/","height":34}},"source":["my_circle=circle(5,5,4)\n","my_circle.area()"],"execution_count":0,"outputs":[{"output_type":"execute_result","data":{"text/plain":["50.285714285714285"]},"metadata":{"tags":[]},"execution_count":15}]},{"cell_type":"markdown","metadata":{"id":"rgCYrfnBWMlK","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":"code","metadata":{"id":"Bd7UlJp1W4Iy","colab_type":"code","colab":{}},"source":["class shapes:\n"," def __init__(self, x, y):\n"," self.x=x\n"," self.y=y\n","\n","class rectangle(shapes):\n"," def __init__(self,width,length,x,y):\n"," super(rectangle, self).__init__(x, y)\n"," self.__width=width\n"," self.__length=length\n"," self.__x=x\n"," self.__y=y\n"," \n"," def area(self):\n"," return self.__width*self.__length\n"," \n"," def perimeter(self):\n"," return 2*(self.__width+self.__length)\n"," \n"," def x(self):\n"," return self.__x\n"," \n"," def y(self):\n"," return self.__y\n","\n","class circle(shapes):\n"," def __init__(self, x, y, radius):\n"," super(circle, slef).__init__(x, y)\n"," self.__x=x\n"," self.__y=y\n"," self.__radius=radius\n","\n"," def area(self):\n"," return (self.__radius**2)*(22/7)\n"," \n"," def circumference(self):\n"," return self.__radius*2*(22/7)\n","\n"," def radius(self):\n"," return self.__radius\n","\n"," def x(self):\n"," return self.__x\n","\n"," def y(self):\n"," return self.__y"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"6Aqq4Y5JW4QX","colab_type":"code","outputId":"41ff8497-7deb-469e-81b6-e9334c08f314","executionInfo":{"status":"ok","timestamp":1587148318479,"user_tz":300,"elapsed":326,"user":{"displayName":"","photoUrl":"","userId":""}},"colab":{"base_uri":"https://localhost:8080/","height":34}},"source":["arectangle=rectangle(5,5,5,5)\n","arectangle.perimeter()"],"execution_count":0,"outputs":[{"output_type":"execute_result","data":{"text/plain":["20"]},"metadata":{"tags":[]},"execution_count":19}]},{"cell_type":"markdown","metadata":{"id":"9NleAzjLWMlL","colab_type":"text"},"source":["6. Implement an analogous triangle class."]},{"cell_type":"code","metadata":{"id":"VtmGKW10W4vu","colab_type":"code","colab":{}},"source":["class triangle(shapes):\n"," def __init__(self, x, y, a, base, c, height):\n"," super(triangle, self).__init__(x, y)\n"," self.__x=x\n"," self.__y=y\n"," self.__a=a\n"," self.__base=base\n"," self.__c=c\n"," self.__height=height\n"," \n"," def area(self):\n"," return (1/2)*self.__base*self.__height\n","\n"," def perimeter(self):\n"," return self.__a+self.__base+self.__c\n","\n"," def x(self):\n"," return self.__x\n","\n"," def y(self):\n"," return self.__y\n","\n"," def a(self):\n"," return self.__a\n","\n"," def base(self):\n"," return self.__base\n","\n"," def c(self):\n"," return self.__c\n","\n"," def height(self):\n"," return self.__height\n"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"iha9Hog_W41Y","colab_type":"code","outputId":"eee3bc4b-16a7-4c72-f4b4-2c69df97e329","executionInfo":{"status":"ok","timestamp":1587148753738,"user_tz":300,"elapsed":275,"user":{"displayName":"","photoUrl":"","userId":""}},"colab":{"base_uri":"https://localhost:8080/","height":34}},"source":["atriangle=triangle(5,6,6,6,6,6)\n","atriangle.area()"],"execution_count":0,"outputs":[{"output_type":"execute_result","data":{"text/plain":["18.0"]},"metadata":{"tags":[]},"execution_count":22}]},{"cell_type":"markdown","metadata":{"id":"b1NSsDtiWMlM","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":"code","metadata":{"id":"oKunQ7xUW5fp","colab_type":"code","colab":{}},"source":["class shapes:\n"," def __init__(self, x, y):\n"," self.x=x\n"," self.y=y\n","\n","class rectangle(shapes):\n"," def __init__(self,width,length,x,y):\n"," super(rectangle, self).__init__(x, y)\n"," self.__width=width\n"," self.__length=length\n"," self.__x=x\n"," self.__y=y\n"," \n"," def area(self):\n"," return self.__width*self.__length\n"," \n"," def perimeter(self):\n"," return 2*(self.__width+self.__length)\n"," \n"," def x(self):\n"," return self.__x\n"," \n"," def y(self):\n"," return self.__y\n","\n"," def test(self):\n"," if self.__x<=self.__width:\n"," print(\"x within rectangle\")\n"," else:\n"," print(\"x not within rectangle\")\n"," if self.__y<=self.__length:\n"," print(\"y within rectangle\")\n"," else:\n"," print(\"y not within rectangle\")\n","\n","class circle(shapes):\n"," def __init__(self, x, y, radius):\n"," super(circle, slef).__init__(x, y)\n"," self.__x=x\n"," self.__y=y\n"," self.__radius=radius\n","\n"," def area(self):\n"," return (self.__radius**2)*(22/7)\n"," \n"," def circumference(self):\n"," return self.__radius*2*(22/7)\n","\n"," def radius(self):\n"," return self.__radius\n","\n"," def x(self):\n"," return self.__x\n","\n"," def y(self):\n"," return self.__y\n","\n"," def test(self):\n"," if self.__x<= self.__radius*2:\n"," print(\"x within circle\")\n"," else:\n"," print(\"x not within circle\")\n"," if self.__y<=self.__radius*2:\n"," print(\"y within circle\")\n"," else:\n"," print(\"y not within circle\")\n","\n","class triangle(shapes):\n"," def __init__(self, x, y, a, base, c, height):\n"," super(triangle, self).__init__(x, y)\n"," self.__x=x\n"," self.__y=y\n"," self.__a=a\n"," self.__base=base\n"," self.__c=c\n"," self.__height=height\n"," \n"," def area(self):\n"," return (1/2)*self.__base*self.__height\n","\n"," def perimeter(self):\n"," return self.__a+self.__base+self.__c\n","\n"," def x(self):\n"," return self.__x\n","\n"," def y(self):\n"," return self.__y\n","\n"," def a(self):\n"," return self.__a\n","\n"," def base(self):\n"," return self.__base\n","\n"," def c(self):\n"," return self.__c\n","\n"," def height(self):\n"," return self.__height"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"s-CPJgu6W5td","colab_type":"code","outputId":"1a31f031-cadc-48fc-bfda-4f3545d2f0b4","executionInfo":{"status":"ok","timestamp":1587149434065,"user_tz":300,"elapsed":332,"user":{"displayName":"","photoUrl":"","userId":""}},"colab":{"base_uri":"https://localhost:8080/","height":50}},"source":["my_rectangle=rectangle(5,6,4,3)\n","my_rectangle.test()"],"execution_count":0,"outputs":[{"output_type":"stream","text":["x within rectangle\n","y within rectangle\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"5NJndRrkWMlM","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":"code","metadata":{"id":"KlhhIpi6W6Ly","colab_type":"code","colab":{}},"source":["class shapes:\n"," def __init__(self, x, y):\n"," self.x=x\n"," self.y=y\n","\n","class rectangle(shapes):\n"," def __init__(self,width,length,x,y):\n"," super(rectangle, self).__init__(x, y)\n"," self.__width=width\n"," self.__length=length\n"," self.__x=x\n"," self.__y=y\n"," \n"," def area(self):\n"," return self.__width*self.__length\n"," \n"," def perimeter(self):\n"," return 2*(self.__width+self.__length)\n"," \n"," def x(self):\n"," return self.__x\n"," \n"," def y(self):\n"," return self.__y\n","\n"," def test(self):\n"," if self.__x<=self.__width:\n"," print(\"x within rectangle\")\n"," else:\n"," print(\"x not within rectangle\")\n"," if self.__y<=self.__length:\n"," print(\"y within rectangle\")\n"," else:\n"," print(\"y not within rectangle\")\n","\n"," def coord(self):\n"," cx=self.__width\n"," cy=self.__length\n"," while cx >=0:\n"," print(\"(\",cx,\",0)\")\n"," cx=cx-1\n"," cx=self.__width\n"," while cx >=0:\n"," print(\"(\",cx,\",\",self.__length,\")\")\n"," cx=cx-1\n"," cx=self.__width\n"," while cy >=0:\n"," print(\"(0,\", cy,\")\")\n"," cy=cy-1\n"," cy=self.__length\n"," while cy >=0:\n"," print(\"(\",self.__width,\",\", cy, \")\")\n"," cy=cy-1\n"," cy=self.__length"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"w7ENDGR5W6P2","colab_type":"code","outputId":"a7227e39-77ba-48b5-cf91-807ba8316981","executionInfo":{"status":"ok","timestamp":1587150705482,"user_tz":300,"elapsed":376,"user":{"displayName":"","photoUrl":"","userId":""}},"colab":{"base_uri":"https://localhost:8080/","height":554}},"source":["abc=rectangle(7,7,7,7)\n","abc.coord()"],"execution_count":0,"outputs":[{"output_type":"stream","text":["( 7 ,0)\n","( 6 ,0)\n","( 5 ,0)\n","( 4 ,0)\n","( 3 ,0)\n","( 2 ,0)\n","( 1 ,0)\n","( 0 ,0)\n","( 7 , 7 )\n","( 6 , 7 )\n","( 5 , 7 )\n","( 4 , 7 )\n","( 3 , 7 )\n","( 2 , 7 )\n","( 1 , 7 )\n","( 0 , 7 )\n","(0, 7 )\n","(0, 6 )\n","(0, 5 )\n","(0, 4 )\n","(0, 3 )\n","(0, 2 )\n","(0, 1 )\n","(0, 0 )\n","( 7 , 7 )\n","( 7 , 6 )\n","( 7 , 5 )\n","( 7 , 4 )\n","( 7 , 3 )\n","( 7 , 2 )\n","( 7 , 1 )\n","( 7 , 0 )\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"ZqKWKU-pWMlN","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."]},{"cell_type":"code","metadata":{"id":"jqThJCj7W65c","colab_type":"code","colab":{}},"source":[""],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"Hy45s-6WW69v","colab_type":"code","colab":{}},"source":[""],"execution_count":0,"outputs":[]}]} \ No newline at end of file From 165f3dc13639d04e5356bd44419bb8f70dbdfa3b Mon Sep 17 00:00:00 2001 From: justinkeas <60792432+justinkeas@users.noreply.github.com> Date: Fri, 24 Apr 2020 14:28:49 -0500 Subject: [PATCH 08/10] Add files via upload --- Labs/Lab-6/Lab-6solutions.ipynb | 1 + 1 file changed, 1 insertion(+) create mode 100644 Labs/Lab-6/Lab-6solutions.ipynb diff --git a/Labs/Lab-6/Lab-6solutions.ipynb b/Labs/Lab-6/Lab-6solutions.ipynb new file mode 100644 index 0000000..326d240 --- /dev/null +++ b/Labs/Lab-6/Lab-6solutions.ipynb @@ -0,0 +1 @@ +{"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":"Lab-6solutions.ipynb","provenance":[{"file_id":"https://github.com/justinkeas/DATA1401-Spring-2020/blob/master/Labs/Lab-6/Lab-6.ipynb","timestamp":1587756361221}],"collapsed_sections":[]}},"cells":[{"cell_type":"markdown","metadata":{"id":"wxey2xn3VgMN","colab_type":"text"},"source":["# Lab 6\n"]},{"cell_type":"markdown","metadata":{"id":"2kFyeYy-VgMQ","colab_type":"text"},"source":["Matrix Representation: In this lab you will be creating a simple linear algebra system. In memory, we will represent matrices as nested python lists as we have done in lecture. \n","\n","1. Create a `matrix` class with the following properties:\n"," * It can be initialized in 2 ways:\n"," 1. with arguments `n` and `m`, the size of the matrix. A newly instanciated matrix will contain all zeros.\n"," 2. with a list of lists of values. Note that since we are using lists of lists to implement matrices, it is possible that not all rows have the same number of columns. Test explicitly that the matrix is properly specified.\n"," * Matrix instances `M` can be indexed with `M[i][j]` and `M[i,j]`.\n"," * Matrix assignment works in 2 ways:\n"," 1. If `M_1` and `M_2` are `matrix` instances `M_1=M_2` sets the values of `M_1` to those of `M_2`, if they are the same size. Error otherwise.\n"," 2. In example above `M_2` can be a list of lists of correct size.\n"]},{"cell_type":"code","metadata":{"id":"Fi7UNfO_ZyQr","colab_type":"code","colab":{}},"source":["class matrix:\n"," def __init__(self, m, n):\n"," self.__m=m\n"," self.__n=n\n","\n"," def zero_matrix(self):\n"," out=list()\n"," for i in range(self.__m):\n"," row=list()\n"," for j in range(self.__n):\n"," row.append(0.)\n"," out.append(row)\n"," return out\n"," \n"," def eye_matrix(self):\n"," M=zero_matrix(self.__n,self.__n)\n"," for i in range(self.__n):\n"," M[i][i]=1.\n"," return M\n"," \n"," def is_matrix(M):\n"," if isinstance(M,list):\n"," row_length=len(M[0])\n"," for row in M:\n"," if not row_length==len(row):\n"," return False\n"," else:\n"," False\n"," return True"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"0UuG9YZh8HnR","colab_type":"code","outputId":"af4c7054-4b57-4767-88f4-0eec3850f080","executionInfo":{"status":"ok","timestamp":1587744561588,"user_tz":300,"elapsed":635,"user":{"displayName":"","photoUrl":"","userId":""}},"colab":{"base_uri":"https://localhost:8080/","height":34}},"source":["matrix(5,5)"],"execution_count":0,"outputs":[{"output_type":"execute_result","data":{"text/plain":["<__main__.matrix at 0x7f574ccb4128>"]},"metadata":{"tags":[]},"execution_count":4}]},{"cell_type":"markdown","metadata":{"id":"NMtW996iVgMR","colab_type":"text"},"source":["2. Add the following methods:\n"," * `shape()`: returns a tuple `(n,m)` of the shape of the matrix.\n"," * `transpose()`: returns a new matrix instance which is the transpose of the matrix.\n"," * `row(n)` and `column(n)`: that return the nth row or column of the matrix M as a new appropriately shaped matrix object.\n"," * `to_list()`: which returns the matrix as a list of lists.\n"," * `block(n_0,n_1,m_0,m_1)` that returns a smaller matrix located at the n_0 to n_1 columns and m_0 to m_1 rows. \n"," * (Extra credit) Modify `__getitem__` implemented above to support slicing.\n"," "]},{"cell_type":"code","metadata":{"id":"sW1cg6h0854X","colab_type":"code","colab":{}},"source":["class matrix:\n"," def __init__(self, m, n):\n"," self.__m=m\n"," self.__n=n\n","\n"," def zero_matrix(self):\n"," out=list()\n"," for i in range(self.__m):\n"," row=list()\n"," for j in range(self.__n):\n"," row.append(0.)\n"," out.append(row)\n"," return out\n"," \n"," def eye_matrix(self):\n"," M=zero_matrix(self.__n,self.__n)\n"," for i in range(self.__n):\n"," M[i][i]=1.\n"," return M\n"," \n"," def is_matrix(M):\n"," if isinstance(M,list):\n"," row_length=len(M[0])\n"," for row in M:\n"," if not row_length==len(row):\n"," return False\n"," else:\n"," False\n"," return True\n"," \n"," def matrix_shape(M):\n"," if is_matrix(M):\n"," self.__m=len(M)\n"," self.__n=len(M[0])\n"," return self.__m,self.__n\n"," else:\n"," 0,0\n"," \n"," def matrix_transpose(M):\n"," self.__m,self.__n=matrix_shape(M)\n"," M_out=zero_matrix(self.__n,self.__m)\n"," for i in range(self.__m):\n"," for j in range(self.__n):\n"," M_out[j][i]=M[i][j]\n"," return M_out\n","\n"," def matrix_row(self):\n"," return M[i]\n","\n"," def matrix_column(self):\n"," return M[j]\n","\n"," "],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"HBSwpZIQ864F","colab_type":"code","outputId":"42fdef86-e4d0-45ac-8b11-7f58f9734472","executionInfo":{"status":"error","timestamp":1587756210077,"user_tz":300,"elapsed":693,"user":{"displayName":"","photoUrl":"","userId":""}},"colab":{"base_uri":"https://localhost:8080/","height":337}},"source":["my_matrix=matrix(4,5)\n","my_matrix.is_matrix()\n","my_matrix.matrix_transpose()"],"execution_count":0,"outputs":[{"output_type":"error","ename":"NameError","evalue":"ignored","traceback":["\u001b[0;31m---------------------------------------------------------------------------\u001b[0m","\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)","\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mmy_matrix\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mmatrix\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m4\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mmy_matrix\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mis_matrix\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mmy_matrix\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmatrix_transpose\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m","\u001b[0;32m\u001b[0m in \u001b[0;36mmatrix_transpose\u001b[0;34m(M)\u001b[0m\n\u001b[1;32m 38\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 39\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mmatrix_transpose\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mM\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 40\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__m\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__n\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mmatrix_shape\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mM\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 41\u001b[0m \u001b[0mM_out\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mzero_matrix\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__n\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__m\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 42\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__m\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;32m\u001b[0m in \u001b[0;36mmatrix_shape\u001b[0;34m(M)\u001b[0m\n\u001b[1;32m 31\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 32\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mmatrix_shape\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mM\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 33\u001b[0;31m \u001b[0;32mif\u001b[0m \u001b[0mis_matrix\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mM\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 34\u001b[0m \u001b[0mm\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mM\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 35\u001b[0m \u001b[0mn\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mM\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;31mNameError\u001b[0m: name 'is_matrix' is not defined"]}]},{"cell_type":"markdown","metadata":{"id":"dOHIVUnkVgMS","colab_type":"text"},"source":["3. Write functions that create special matrices (note these are standalone functions, not member functions of your `matrix` class):\n"," * `constant(n,m,c)`: returns a `n` by `m` matrix filled with floats of value `c`.\n"," * `zeros(n,m)` and `ones(n,m)`: return `n` by `m` matrices filled with floats of value `0` and `1`, respectively.\n"," * `eye(n)`: returns the n by n identity matrix."]},{"cell_type":"code","metadata":{"id":"HYJP_9oQE3pt","colab_type":"code","colab":{}},"source":["def constant_matrix(n,m,c):\n"," out=list()\n"," for i in range(n):\n"," row=list()\n"," for j in range(m):\n"," row.append(c)\n"," out.append(row)\n"," return out\n","\n","def zero_matrix(n,m):\n"," out=list()\n"," for i in range(n):\n"," row=list()\n"," for j in range(m):\n"," row.append(0.)\n"," out.append(row)\n"," return out\n","\n","def ones_matrix(n,m):\n"," out=list()\n"," for i in range(n):\n"," row=list()\n"," for j in range(m):\n"," row.append(1)\n"," out.append(row)\n"," return out\n","\n","def eye_matrix(n):\n"," M=zero_matrix(n,n)\n"," for i in range(n):\n"," M[i][i]=1.\n"," return M"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"lNs_vJpLE2xi","colab_type":"code","outputId":"491c8ccf-37d2-4f9a-f177-ab1631061249","executionInfo":{"status":"ok","timestamp":1587747132273,"user_tz":300,"elapsed":518,"user":{"displayName":"","photoUrl":"","userId":""}},"colab":{"base_uri":"https://localhost:8080/","height":101}},"source":["constant_matrix(5,5,5)"],"execution_count":0,"outputs":[{"output_type":"execute_result","data":{"text/plain":["[[5, 5, 5, 5, 5],\n"," [5, 5, 5, 5, 5],\n"," [5, 5, 5, 5, 5],\n"," [5, 5, 5, 5, 5],\n"," [5, 5, 5, 5, 5]]"]},"metadata":{"tags":[]},"execution_count":30}]},{"cell_type":"markdown","metadata":{"id":"jPW6VKbaVgMT","colab_type":"text"},"source":["4. Add the following member functions to your class. Make sure to appropriately test the dimensions of the matrices to make sure the operations are correct.\n"," * `M.scalarmul(c)`: a matrix that is scalar product $cM$, where every element of $M$ is multiplied by $c$.\n"," * `M.add(N)`: adds two matrices $M$ and $N$. Don’t forget to test that the sizes of the matrices are compatible for this and all other operations.\n"," * `M.sub(N)`: subtracts two matrices $M$ and $N$.\n"," * `M.mat_mult(N)`: returns a matrix that is the matrix product of two matrices $M$ and $N$.\n"," * `M.element_mult(N)`: returns a matrix that is the element-wise product of two matrices $M$ and $N$.\n"," * `M.equals(N)`: returns true/false if $M==N$."]},{"cell_type":"markdown","metadata":{"id":"RWoVn-J2VgMU","colab_type":"text"},"source":["5. Overload python operators to appropriately use your functions in 4 and allow expressions like:\n"," * 2*M\n"," * M*2\n"," * M+N\n"," * M-N\n"," * M*N\n"," * M==N\n"," * M=N\n"]},{"cell_type":"markdown","metadata":{"id":"fmQpLjeGVgMV","colab_type":"text"},"source":["6. Demonstrate the basic properties of matrices with your matrix class by creating two 2 by 2 example matrices using your Matrix class and illustrating the following:\n","\n","$$\n","(AB)C=A(BC)\n","$$\n","$$\n","A(B+C)=AB+AC\n","$$\n","$$\n","AB\\neq BA\n","$$\n","$$\n","AI=A\n","$$"]},{"cell_type":"code","metadata":{"id":"IpZpvU8bVgMW","colab_type":"code","colab":{}},"source":[""],"execution_count":0,"outputs":[]}]} \ No newline at end of file From 216ba4fab12d8e3ad4b57eab9d8a6d53c965bfc3 Mon Sep 17 00:00:00 2001 From: justinkeas <60792432+justinkeas@users.noreply.github.com> Date: Fri, 8 May 2020 14:30:46 -0500 Subject: [PATCH 09/10] Add files via upload --- Labs/Lab-7/Lab-7solutions.ipynb | 1 + 1 file changed, 1 insertion(+) create mode 100644 Labs/Lab-7/Lab-7solutions.ipynb diff --git a/Labs/Lab-7/Lab-7solutions.ipynb b/Labs/Lab-7/Lab-7solutions.ipynb new file mode 100644 index 0000000..0960613 --- /dev/null +++ b/Labs/Lab-7/Lab-7solutions.ipynb @@ -0,0 +1 @@ +{"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.0"},"colab":{"name":"Copy of Lab-7.ipynb","provenance":[{"file_id":"https://github.com/afarbin/DATA1401-Spring-2020/blob/master/Labs/Lab-7/Lab-7.ipynb","timestamp":1588966153764}],"toc_visible":true}},"cells":[{"cell_type":"markdown","metadata":{"id":"RsZWacz_HjTV","colab_type":"text"},"source":["# Lab 7\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-7/Lab-7.ipynb)\n","\n","Here are the \"Gradebook\" classes from lecture. For this lab, you will use these classes and are encouraged to modify them as you need."]},{"cell_type":"code","metadata":{"id":"HybDpU2KSHDJ","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":121},"outputId":"a5618a94-be4c-48bd-bca9-527e0b0465c5","executionInfo":{"status":"ok","timestamp":1588958124971,"user_tz":300,"elapsed":25107,"user":{"displayName":"","photoUrl":"","userId":""}}},"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":"code","metadata":{"id":"rljJafIxHjTW","colab_type":"code","colab":{}},"source":["import numpy as np\n","import math\n","\n","# Create some virtual classes\n","class base:\n"," __name=\"\"\n"," \n"," def __init__(self,name):\n"," self.__name=name\n","\n"," def name(self):\n"," return self.__name\n","\n","class data(base):\n"," def __init__(self,name):\n"," base.__init__(self,name)\n"," \n","class alg(base):\n"," def __init__(self,name):\n"," base.__init__(self,name)"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"tpwJ3H9iHjTb","colab_type":"code","colab":{}},"source":["class grade(data):\n"," __value=0\n"," __numerical=True\n"," __gradebook_name=str()\n"," __letter_grades=[\"F-\",\"F\",\"F+\",\"D-\",\"D\",\"D+\",\"C-\",\"C\",\"C+\",\"B-\",\"B\",\"B+\",\"A-\",\"A\",\"A+\"]\n"," \n"," def __init__(self,name,numerical=True,value=None):\n"," if value:\n"," if isinstance(value,(int,float)):\n"," self.__numerical=True\n"," elif isinstance(value,str):\n"," self.__numerical=False\n"," self.set(value)\n"," else: \n"," self.__numerical=numerical\n"," self.__gradebook_name=name\n"," data.__init__(self,name+\" Grade Algorithm\") \n","\n"," def set(self,value):\n"," if isinstance(value,(int,float)) and self.__numerical:\n"," self.__value=value\n"," elif isinstance(value,str) and not self.__numerical:\n"," if value in self.__letter_grades:\n"," self.__value=value\n"," else:\n"," print (self.name()+\" Error: Bad Grade.\")\n"," raise Exception\n"," \n"," def value(self):\n"," return self.__value\n"," \n"," def numerical(self):\n"," return self.__numerical\n"," \n"," def gradebook_name(self):\n"," return self.__gradebook_name\n"," \n"," def __str__(self):\n"," return self.__gradebook_name+\": \"+str(self.__value)\n","\n","class student(data):\n"," __id_number=0\n"," __grades=dict()\n"," \n"," def __init__(self,first_name, last_name,id_number):\n"," self.__id_number=id_number\n"," self.__grades=dict()\n"," data.__init__(self,first_name+\" \"+last_name+\" Student Data\")\n","\n"," def add_grade(self,a_grade,overwrite=False):\n"," if overwrite or not a_grade.gradebook_name() in self.__grades:\n"," self.__grades[a_grade.gradebook_name()]=a_grade\n"," else:\n"," print (self.name()+\" Error Adding Grade \"+a_grade.name()+\". Grade already exists.\")\n"," raise Exception\n","\n"," def id_number(self):\n"," return self.__id_number\n"," \n"," def __getitem__(self,key):\n"," return self.__grades[key]\n"," \n"," def print_grades(self):\n"," for grade in self.__grades:\n"," print (self.__grades[grade])\n"," \n"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"_sK1kgF8HjTe","colab_type":"code","colab":{}},"source":["class calculator(alg): \n"," def __init__(self,name):\n"," alg.__init__(self,name)\n","\n"," def apply(self,a_grade_book):\n"," raise NotImplementedError\n"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"vSzfiSPwHjTh","colab_type":"code","colab":{}},"source":["class grade_book(data):\n"," # New member class to hold arbitrary data associated with the class\n","\n"," __data=dict()\n"," __students=dict()\n"," \n"," def __init__(self,name):\n"," data.__init__(self,name+\" Course Grade Book\")\n"," self.__students=dict()\n"," self.__data=dict()\n"," \n"," # New method to access data\n"," def __getitem__(self,key):\n"," return self.__data[key]\n"," \n"," # New method to add data\n"," def __setitem__(self, key, value):\n"," self.__data[key] = value\n"," \n"," def add_student(self,a_student):\n"," self.__students[a_student.id_number()]=a_student\n","\n"," # New method to allow iterating over students\n"," def get_students(self):\n"," return self.__students\n"," \n"," def assign_grade(self,key,a_grade):\n"," the_student=None\n"," try:\n"," the_student=self.__students[key]\n"," except:\n"," for id in self.__students:\n"," if key == self.__students[id].name():\n"," the_student=self.__students[id]\n"," break\n"," if the_student:\n"," the_student.add_grade(a_grade)\n"," else:\n"," print (self.name()+\" Error: Did not find student.\")\n"," \n"," def apply_calculator(self,a_calculator,**kwargs):\n"," a_calculator.apply(self,**kwargs)\n"," \n"," "],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"nTs835MXHjTk","colab_type":"code","colab":{}},"source":["class uncurved_letter_grade_percent(calculator):\n"," __grades_definition=[ (.97,\"A+\"),\n"," (.93,\"A\"),\n"," (.9,\"A-\"),\n"," (.87,\"B+\"),\n"," (.83,\"B\"),\n"," (.8,\"B-\"),\n"," (.77,\"C+\"),\n"," (.73,\"C\"),\n"," (.7,\"C-\"),\n"," (.67,\"C+\"),\n"," (.63,\"C\"),\n"," (.6,\"C-\"),\n"," (.57,\"F+\"),\n"," (.53,\"F\"),\n"," (0.,\"F-\")]\n"," __max_grade=100.\n"," __grade_name=str()\n"," \n"," def __init__(self,grade_name,max_grade=100.):\n"," self.__max_grade=max_grade\n"," self.__grade_name=grade_name\n"," calculator.__init__(self,\n"," \"Uncurved Percent Based Grade Calculator \"+self.__grade_name+\" Max=\"+str(self.__max_grade))\n"," \n"," def apply(self,a_grade_book,grade_name=None,**kwargs):\n"," if grade_name:\n"," pass\n"," else:\n"," grade_name=self.__grade_name\n"," \n"," \n"," for k,a_student in a_grade_book.get_students().iteritems():\n"," a_grade=a_student[grade_name]\n","\n"," if not a_grade.numerical():\n"," print (self.name()+ \" Error: Did not get a numerical grade as input.\")\n"," raise Exception\n"," \n"," percent=a_grade.value()/self.__max_grade\n"," \n"," for i,v in enumerate(self.__grades_definition):\n"," if percent>=v[0]:\n"," break\n"," \n"," a_student.add_grade(grade(grade_name+\" Letter\",value=self.__grades_definition[i][1]))\n"," "],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"KLvj60Q-HjTn","colab_type":"code","colab":{}},"source":["class mean_std_calculator(calculator):\n"," def __init__(self):\n"," calculator.__init__(self,\"Mean and Standard Deviation Calculator\")\n"," \n"," def apply(self,a_grade_book,grade_name,**kwargs):\n"," grades=list()\n"," for k,a_student in a_grade_book.get_students().iteritems():\n"," grades.append(a_student[grade_name].value())\n"," \n"," a_grade_book[grade_name+\" Mean\"] = np.mean(grades)\n"," a_grade_book[grade_name+\" STD\"] = math.sqrt(np.var(grades))\n"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"collapsed":true,"id":"5i9Bw9F4HjTr","colab_type":"text"},"source":["## CSV Reader\n","\n","*Exercise 1*: The data for a class are stored in a \"camma separated values\" (CSV) file name `Data1401-Grades.csv` in the directory of this lab. You can see the contents using the `cat` shell command:"]},{"cell_type":"code","metadata":{"id":"JkwzL8TgHjTs","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":34},"outputId":"a3320e33-2d50-4d63-ea42-c75cb2a03514","executionInfo":{"status":"ok","timestamp":1588965118606,"user_tz":300,"elapsed":1367,"user":{"displayName":"","photoUrl":"","userId":""}}},"source":["!cat Data1401-Grades.csv "],"execution_count":31,"outputs":[{"output_type":"stream","text":["cat: Data1401-Grades.csv: No such file or directory\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"CAUyw38bHjTx","colab_type":"text"},"source":["You will note that the first line has the names of the \"columns\" of data, and that subsequent lines (or \"rows\") have the data for each student, separated by cammas.\n","\n","Recalling that in lecture we created a file reader, create a CSV reader function that takes a filename as input and returns data structure(s) that store the data in the file. Note that you are not allowed to use a library. The point here is for *you* to write the CSV reader. Some options for your data structures (pick one):\n","\n","* A list of dictionaries, where each element of the list is corresponds to a row of data and the dictionaries are keyed by the column name. For example `data[5][\"l3_5\"]` corresponds to the 6th student's grade on lab 3 question 5.\n","\n","* A list of lists (i.e. a 2-D array or matrix) and a dictionary, where each element of the \"matrix\" corresponds to a a specific grade for a specific student and the dictionary maps the name of the column to the column index. For example `data[5][column_names[\"l1_5\"]]` corresponds to the 6th student's grade on lab 3 question 5.\n","\n","* A dictionary of lists, where each element of the dictionary corresponds to a column of data and the lists contain the data in that column. For example `data[\"l3_5\"][5]` corresponds to the 6th student's grade on lab 3 question 5.\n","\n","* (Extra Credit) A class that simultaneously supports all of the above methods."]},{"cell_type":"code","metadata":{"id":"Su58L-h0HjTx","colab_type":"code","colab":{}},"source":["# Your solution here.\n","\n","def csv_reader(filename):\n"," data=list() # if you choose first option\n"," for row in range(filename):\n"," print (row[0])\n"," for column in range(filename):\n"," print (data[row][column])\n"," return data"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"tA2W_YCnHjT0","colab_type":"text"},"source":["## Creating a Gradebook\n","\n","*Exercise 2:* In lecture we used pandas to read the CSV file and create the grade book. The example below works for the CSV file for this lab. Modify the code below to use your CSV reader instead."]},{"cell_type":"code","metadata":{"id":"_JIfzIdxHjT0","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":346},"outputId":"2722e6f5-2a8a-4df8-95ef-986dd30b0e2b","executionInfo":{"status":"error","timestamp":1588965123307,"user_tz":300,"elapsed":340,"user":{"displayName":"","photoUrl":"","userId":""}}},"source":["import pandas as pd\n","class_data=csv_reader(\"Data1401-Grades.csv\")\n","\n","a_grade_book=grade_book(\"Data 1401\")\n","\n","for student_i in range(class_data.shape[0]):\n"," a_student_0=student(\"Student\",str(student_i),student_i)\n","\n"," for k in class_data.keys():\n"," a_student_0.add_grade(grade(k,value=class_data[k][student_i]))\n","\n"," a_grade_book.add_student(a_student_0)\n"," "],"execution_count":33,"outputs":[{"output_type":"error","ename":"TypeError","evalue":"ignored","traceback":["\u001b[0;31m---------------------------------------------------------------------------\u001b[0m","\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)","\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mpandas\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mpd\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mclass_data\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcsv_reader\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Data1401-Grades.csv\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0ma_grade_book\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mgrade_book\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Data 1401\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;32m\u001b[0m in \u001b[0;36mcsv_reader\u001b[0;34m(filename)\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mcsv_reader\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfilename\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mlist\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# if you choose first option\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0;32mfor\u001b[0m \u001b[0mrow\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfilename\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 5\u001b[0m \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mrow\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mcolumn\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfilename\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;31mTypeError\u001b[0m: 'str' object cannot be interpreted as an integer"]}]},{"cell_type":"markdown","metadata":{"id":"n0ldCCs-HjT3","colab_type":"text"},"source":["## Grade Summing\n","\n","*Exercise 3:* In lectre we will change the design of our algorithm classes and then update the `uncurved_letter_grade_percent` calculator. In lecture we also created a `grade_summer` calcuator that takes a prefix (for example `e1_` and a number `n`) and sums all grades starting with that prefix up to `n` and creates a new sum grade. Update this calculator (below) to the new design of our algorithm classes. Test your updated calculator by using it to sum the grades for all labs, quizzes, and exams of each student."]},{"cell_type":"code","metadata":{"id":"hlk1CBk0HjT4","colab_type":"code","colab":{}},"source":["# Note this is the OLD design... you will need to modify it.\n","\n","class summary_calculator(alg): \n"," def __init__(self,name):\n"," alg.__init__(self,name)\n","\n"," def apply(self,a_student):\n"," raise NotImplementedError\n","\n","class grade_summer(summary_calculator):\n"," def __init__(self,prefix,n):\n"," self.__prefix=prefix\n"," self.__n=n\n"," summary_calculator.__init__(self,\"Sum Grades\")\n"," \n"," def apply(self,a_student):\n"," labels=[self.__prefix+str(x) for x in range(1,self.__n)]\n"," \n"," grade_sum=0.\n"," for label in labels:\n"," grade_sum+=a_student[label].value()\n","\n"," a_student.add_grade(grade(self.__prefix+\"sum\",value=grade_sum))"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"Kcfhwj7EHjT8","colab_type":"text"},"source":["## Curving Grades\n","\n","*Exercise 4:* Use the `mean_std_calculator` above to calculate the mean and standard deviation for every lab, quiz, and exam in the class. Add a new print function to the `grade_book` class to print out such information in a nice way, and use this function to show your results.\n"]},{"cell_type":"code","metadata":{"id":"nq54M13HHjT9","colab_type":"code","colab":{}},"source":["# Your solution here\n","class mean_std_calculator(calculator):\n"," def __init__(self):\n"," calculator.__init__(self,\"Mean and Standard Deviation Calculator\")\n"," \n"," def apply(self,a_grade_book,grade_name,**kwargs):\n"," grades=list()\n"," for k,a_student in a_grade_book.get_students().iteritems():\n"," grades.append(a_student[grade_name].value())\n"," \n"," a_grade_book[grade_name+\" Mean\"] = np.mean(grades)\n"," a_grade_book[grade_name+\" STD\"] = math.sqrt(np.var(grades))\n","class grade_book(data):\n"," def print(self):\n"," return mean_std_calculator\n"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"AM6ZJEsNHjUB","colab_type":"text"},"source":["*Exercise 5:* In lecture we will change the design of our algorithms classes and then update the `uncurved_letter_grade_percent` calculator. Do the same for the `curved_letter_grade` calculator below and by curving all the lab, quiz, and exam grades."]},{"cell_type":"code","metadata":{"id":"CdBgBUlgHjUF","colab_type":"code","colab":{}},"source":["class curved_letter_grade(grade_calculator):\n"," __grades_definition=[ (.97,\"A+\"),\n"," (.93,\"A\"),\n"," (.9,\"A-\"),\n"," (.87,\"B+\"),\n"," (.83,\"B\"),\n"," (.8,\"B-\"),\n"," (.77,\"C+\"),\n"," (.73,\"C\"),\n"," (.7,\"C-\"),\n"," (.67,\"C+\"),\n"," (.63,\"C\"),\n"," (.6,\"C-\"),\n"," (.57,\"F+\"),\n"," (.53,\"F\"),\n"," (0.,\"F-\")]\n"," __max_grade=100.\n"," __grade_name=str()\n"," \n"," def __init__(self,grade_name,mean,std,max_grade=100.):\n"," self.__max_grade=max_grade\n"," self.__mean=mean\n"," self.__std=std\n"," self.__grade_name=grade_name\n"," grade_calculator.__init__(self,\n"," \"Curved Percent Based Grade Calculator \"+self.__grade_name+ \\\n"," \" Mean=\"+str(self.__mean)+\\\n"," \" STD=\"+str(self.__std)+\\\n"," \" Max=\"+str(self.__max_grade))\n"," \n","\n"," def apply(self,a_grade):\n"," if not isinstance(a_grade,grade):\n"," print (self.name()+ \" Error: Did not get an proper grade as input.\")\n"," raise Exception\n"," if not a_grade.numerical():\n"," print (self.name()+ \" Error: Did not get a numerical grade as input.\")\n"," raise Exception\n"," \n"," # Rescale the grade\n"," percent=a_grade.value()/self.__max_grade\n"," shift_to_zero=percent-(self.__mean/self.__max_grade)\n"," scale_std=0.1*shift_to_zero/(self.__std/self.__max_grade)\n"," scaled_percent=scale_std+0.8\n"," \n"," for i,v in enumerate(self.__grades_definition):\n"," if scaled_percent>=v[0]:\n"," break\n"," \n"," return grade(self.__grade_name,value=self.__grades_definition[i][1])\n"," "],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"I6oDwfPZHjUO","colab_type":"text"},"source":["## Final Course Grade\n","\n","*Exercise 6:* Write a new calculator that sums grades with a prefix, as in the `grade_summer` calculator, but drops `n` lowest grades. Apply the algorithm to drop the lowest lab grade in the data.\n"]},{"cell_type":"code","metadata":{"id":"BNcbuE4CHjUP","colab_type":"code","colab":{}},"source":["# Your solution here"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"6fMN4mNbHjUX","colab_type":"text"},"source":["*Exercise 7*: Write a new calculator that creates a new letter grade based on a weighted average of letter grades, by assigning the following numerical values to letter grades:"]},{"cell_type":"code","metadata":{"id":"LPVrA6c3HjUZ","colab_type":"code","colab":{}},"source":["GradeMap={\"A+\":12,\n"," \"A\":11,\n"," \"A-\":10,\n"," \"B+\":9,\n"," \"B\":8,\n"," \"B-\":7,\n"," \"C+\":6,\n"," \"C\":5,\n"," \"C-\":4,\n"," \"D+\":3,\n"," \"D\":2,\n"," \"D-\":1,\n"," \"F\":0}"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"UhdQV2g2HjUf","colab_type":"text"},"source":["Test you calculator by applying the weights from the syllabus of this course and computing everyone's grade in the course."]},{"cell_type":"code","metadata":{"id":"zbYzkt2KHjUh","colab_type":"code","colab":{}},"source":["# Your solution here"],"execution_count":0,"outputs":[]}]} \ No newline at end of file From 810133c7814bd630d98be67dc61fc81dffe13ee1 Mon Sep 17 00:00:00 2001 From: justinkeas <60792432+justinkeas@users.noreply.github.com> Date: Mon, 11 May 2020 16:20:44 -0500 Subject: [PATCH 10/10] Add files via upload --- Exams/Final/Finalsolutions.ipynb | 1 + 1 file changed, 1 insertion(+) create mode 100644 Exams/Final/Finalsolutions.ipynb diff --git a/Exams/Final/Finalsolutions.ipynb b/Exams/Final/Finalsolutions.ipynb new file mode 100644 index 0000000..f646612 --- /dev/null +++ b/Exams/Final/Finalsolutions.ipynb @@ -0,0 +1 @@ +{"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.0"},"colab":{"name":"Finalsolutions.ipynb","provenance":[{"file_id":"https://github.com/afarbin/DATA1401-Spring-2020/blob/master/Exams/Final/Final.ipynb","timestamp":1589231906786}],"collapsed_sections":[]}},"cells":[{"cell_type":"markdown","metadata":{"id":"Tpc_6UuVcg1E","colab_type":"text"},"source":["# Final 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/Final/Final.ipynb)"]},{"cell_type":"code","metadata":{"id":"qqS42ccRckjS","colab_type":"code","outputId":"ea60afe9-ede7-4d53-f016-539c340e52a2","executionInfo":{"status":"ok","timestamp":1589212538824,"user_tz":300,"elapsed":37091,"user":{"displayName":"","photoUrl":"","userId":""}},"colab":{"base_uri":"https://localhost:8080/","height":121}},"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":{"id":"XML5XBSicg1F","colab_type":"text"},"source":["Recall the drawing system from lecture 18:"]},{"cell_type":"code","metadata":{"id":"pQENZ1WLcg1G","colab_type":"code","colab":{}},"source":["class Canvas:\n"," def __init__(self, width, height):\n"," self.width = width\n"," self.height = height\n"," self.data = [[' '] * width for i in range(height)]\n","\n"," def set_pixel(self, row, col, char='*'):\n"," self.data[row][col] = char\n","\n"," def get_pixel(self, row, col):\n"," return self.data[row][col]\n"," \n"," def v_line(self, x, y, w, **kargs):\n"," for i in range(x,x+w):\n"," self.set_pixel(i,y, **kargs)\n","\n"," def h_line(self, x, y, h, **kargs):\n"," for i in range(y,y+h):\n"," self.set_pixel(x,i, **kargs)\n"," \n"," def line(self, x1, y1, x2, y2, **kargs):\n"," slope = (y2-y1) / (x2-x1)\n"," for y in range(y1,y2):\n"," x= int(slope * y)\n"," self.set_pixel(x,y, **kargs)\n"," \n"," def display(self):\n"," print(\"\\n\".join([\"\".join(row) for row in self.data]))"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"SvkXQ-GMcg1L","colab_type":"code","colab":{}},"source":["class Shape:\n"," def __init__(self, name=\"\", **kwargs):\n"," self.name=name\n"," self.kwargs=kwargs\n"," \n"," def paint(self, canvas): pass\n","\n","class Rectangle(Shape):\n"," def __init__(self, x, y, w, h, **kwargs):\n"," Shape.__init__(self, **kwargs)\n"," self.x = x\n"," self.y = y\n"," self.w = w\n"," self.h = h\n","\n"," def paint(self, canvas):\n"," canvas.h_line(self.x, self.y, self.w, **self.kwargs)\n"," canvas.h_line(self.x, self.y + self.h, self.w, **self.kwargs)\n"," canvas.v_line(self.x, self.y, self.h, **self.kwargs)\n"," canvas.v_line(self.x + self.w, self.y, self.h, **self.kwargs)\n","\n","class Square(Rectangle):\n"," def __init__(self, x, y, size, **kwargs):\n"," Rectangle.__init__(self, x, y, size, size, **kwargs)\n","\n","class Line(Shape):\n"," def __init__(self, x1, y1, x2, y2, **kwargs):\n"," Shape.__init__(self, **kwargs)\n"," self.x1=x1\n"," self.y1=y1\n"," self.x2=x2\n"," self.y2=y2\n"," \n"," def paint(self, canvas):\n"," canvas.line(self.x1,self.y1,self.x2,self.y2)\n"," \n","class CompoundShape(Shape):\n"," def __init__(self, shapes):\n"," self.shapes = shapes\n","\n"," def paint(self, canvas):\n"," for s in self.shapes:\n"," s.paint(canvas)"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"zzvNuZbKcg1P","colab_type":"code","colab":{}},"source":["class RasterDrawing:\n"," def __init__(self):\n"," self.shapes=dict()\n"," self.shape_names=list()\n"," \n"," def add_shape(self,shape):\n"," if shape.name == \"\":\n"," shape.name = self.assign_name()\n"," \n"," self.shapes[shape.name]=shape\n"," self.shape_names.append(shape.name)\n"," \n"," def paint(self,canvas):\n"," for shape_name in self.shape_names:\n"," self.shapes[shape_name].paint(canvas)\n"," \n"," def assign_name(self):\n"," name_base=\"shape\"\n"," name = name_base+\"_0\"\n"," \n"," i=1\n"," while name in self.shapes:\n"," name = name_base+\"_\"+str(i)\n"," \n"," return name\n"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"wDlZks1kcg1T","colab_type":"text"},"source":["1. Add `Point` and `Triangle` classes and test them."]},{"cell_type":"code","metadata":{"id":"CfKP9npIcg1U","colab_type":"code","colab":{}},"source":["class Point(Shape):\n"," def __init__(self, x1, y1, **kwargs):\n"," Shape.__init__(self, **kwargs)\n"," self.x1 = x1\n"," self.y1 = y1\n"," \n"," def paint(self,canvas):\n"," canvas.set_pixel(self.x1,self.y1)\n","\n","class Triangle(Shape):\n"," def __init__(self, x1, y1, x2, y2, x3, y3, **kwargs):\n"," Shape.__init__(self, **kwargs)\n"," self.x1=x1\n"," self.y1=y1\n"," self.x2=x2\n"," self.y2=y2\n"," self.x3=x3\n"," self.y3=y3\n"," \n"," def paint(self, canvas):\n"," canvas.line(self.x1,self.y1,self.x2,self.y2) \n"," canvas.line(self.x2,self.y2,self.x3,self.y3) \n"," canvas.line(self.x1,self.y1,self.x3,self.y3)"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"C55ZmqvKg7Ng","colab_type":"code","outputId":"a9534074-8758-4e15-e8fb-1d32305550d3","executionInfo":{"status":"ok","timestamp":1589229616847,"user_tz":300,"elapsed":407,"user":{"displayName":"","photoUrl":"","userId":""}},"colab":{"base_uri":"https://localhost:8080/","height":185}},"source":["f1=Canvas(10,10)\n","mypoint=Point(5,5)\n","mypoint.paint(f1)\n","f1.display()"],"execution_count":0,"outputs":[{"output_type":"stream","text":[" \n"," \n"," \n"," \n"," \n"," * \n"," \n"," \n"," \n"," \n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"sBxQWFfsg97t","colab_type":"code","outputId":"95a66b35-c211-485c-b4fd-e65787818a30","executionInfo":{"status":"ok","timestamp":1589229618644,"user_tz":300,"elapsed":367,"user":{"displayName":"","photoUrl":"","userId":""}},"colab":{"base_uri":"https://localhost:8080/","height":269}},"source":["f2=Canvas(15,15)\n","mytriangle=Triangle(2,2,5,5,1,5)\n","mytriangle.paint(f2)\n","f2.display()"],"execution_count":0,"outputs":[{"output_type":"stream","text":[" \n"," \n"," * \n"," ** \n"," * \n"," \n"," * \n"," \n"," \n"," * \n"," \n"," \n"," \n"," \n"," \n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"ZBPMNugC0pV4","colab_type":"code","colab":{}},"source":["import math\n","pi=22/7"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"slWT9ajfcg1X","colab_type":"text"},"source":["2. Add an `Arc` class that is instantiated with a center location, two axis lengths, and starting and ending angles. If start and end are not specified or are the same angle, the `Arc` instance should draw an oval. If in addition the two axes are the same, the `Arc` instance should draw a circle. Create `Oval` and `Circle` classes that inherit from `Arc`. Test everything."]},{"cell_type":"code","metadata":{"id":"deqr9EEMcg1Y","colab_type":"code","colab":{}},"source":["class Arc(Shape):\n"," def __init__(self, r, x1, y1, x, y, SA, EA, **kwargs):\n"," Shape.__init__(self, **kwargs)\n"," self.r = r\n"," self.x1 = x1\n"," self.y1 = y1\n"," self.x = x\n"," self.y = y\n"," self.SA = SA\n"," self.EA = EA\n"," \n"," def paint(self, canvas):\n"," canvas.set_pixel(self.x1, self.y1)\n"," canvas[x][:]\n"," canvas[:][y]\n"," canvas.line((self.x1+(self.r*math.cos(self.SA)),self.y1+(self.r*math.sin(self.SA))),(self.x1+(self.r*math.cos(self.EA)),self.y1+(self.r*math.sin(self.EA))))\n","\n","class Circle(Arc):\n"," def __init__(self, r, x1, y1, x, y, A, **kwargs):\n"," Arc.__init__(self, **kwargs)\n"," self.A = A\n","\n"," def paint(self, canvas):\n"," canvas.set_pixel(self.x1, self.y1)\n"," canvas[x][:]\n"," canvas[:][y]\n","\n","class Oval(Arc):\n"," def __init__(self, r, w, h, x1, y1, x, y, SA, EA, **kwargs):\n"," Arc.__init__(self, **kwargs)\n"," self.w = w\n"," self.h = h\n","\n"," def paint(self, canvas):\n"," canvas.set_pixel(self.x1, self.y1)\n"," canvas[x][:]\n"," canvas[:][y]\n","\n","\n","\n"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"-U49F5NqCRcC","colab_type":"code","outputId":"beb4d53d-9c03-4d44-c1e3-e0a88e5d6344","executionInfo":{"status":"error","timestamp":1589229628323,"user_tz":300,"elapsed":394,"user":{"displayName":"","photoUrl":"","userId":""}},"colab":{"base_uri":"https://localhost:8080/","height":374}},"source":["c1=Canvas(10,10)\n","myarc=CompoundShape([Arc(4,4,4,4,4,0,pi/2)])\n","myarc.paint(c1)\n","c1.display()"],"execution_count":0,"outputs":[{"output_type":"error","ename":"TypeError","evalue":"ignored","traceback":["\u001b[0;31m---------------------------------------------------------------------------\u001b[0m","\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)","\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mc1\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mCanvas\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mmyarc\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mCompoundShape\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mArc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m4\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m4\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m4\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m4\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m4\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mpi\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mmyarc\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpaint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mc1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0mc1\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdisplay\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;32m\u001b[0m in \u001b[0;36mpaint\u001b[0;34m(self, canvas)\u001b[0m\n\u001b[1;32m 41\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mpaint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcanvas\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 42\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0ms\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshapes\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 43\u001b[0;31m \u001b[0ms\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpaint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcanvas\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m","\u001b[0;32m\u001b[0m in \u001b[0;36mpaint\u001b[0;34m(self, canvas)\u001b[0m\n\u001b[1;32m 12\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mpaint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcanvas\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[0mcanvas\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mset_pixel\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mx1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0my1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 14\u001b[0;31m \u001b[0mcanvas\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mline\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mx1\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mr\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0mmath\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcos\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mSA\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0my1\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mr\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0mmath\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msin\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mSA\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mx1\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mr\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0mmath\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcos\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mEA\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0my1\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mr\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0mmath\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msin\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mEA\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 15\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 16\u001b[0m \u001b[0;32mclass\u001b[0m \u001b[0mCircle\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mArc\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;31mTypeError\u001b[0m: line() missing 2 required positional arguments: 'x2' and 'y2'"]}]},{"cell_type":"markdown","metadata":{"id":"rXixovcKcg1c","colab_type":"text"},"source":["3. Use your classes to create a `RasterDrawing` that draws a happy face."]},{"cell_type":"code","metadata":{"id":"hx592wzbcg1d","colab_type":"code","colab":{}},"source":[""],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"AOWir646cg1i","colab_type":"text"},"source":["4. Add to the `Shape` base class a `__str__()` method. Overwrite the method in each shape to generate a string of the python code necessary to reinstantiate the object. For example, for a rectangle originally instantiated using `Square(5,5,20,char=\"^\")`, `__str__()` should return the string `'Square(5,5,20,char=\"^\")'`.\n"]},{"cell_type":"code","metadata":{"id":"56V_Uv_Vcg1j","colab_type":"code","colab":{}},"source":["class Shape:\n"," def __str__(self):\n"," return 'Shape(name=',self.name,',kwargs=',self.kwargs,')'"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"FKBrx9LJcX-R","colab_type":"code","outputId":"5ead7073-112e-4204-c15d-f4b3b5fbe041","executionInfo":{"status":"error","timestamp":1589230079098,"user_tz":300,"elapsed":360,"user":{"displayName":"","photoUrl":"","userId":""}},"colab":{"base_uri":"https://localhost:8080/","height":320}},"source":["mysquare=Square(5,5,20,char=\"^\")\n","mysquare.__str__()"],"execution_count":0,"outputs":[{"output_type":"error","ename":"TypeError","evalue":"ignored","traceback":["\u001b[0;31m---------------------------------------------------------------------------\u001b[0m","\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)","\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mmysquare\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mSquare\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m20\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mchar\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\"^\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mmysquare\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__str__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;32m\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, x, y, size, **kwargs)\u001b[0m\n\u001b[1;32m 22\u001b[0m \u001b[0;32mclass\u001b[0m \u001b[0mSquare\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mRectangle\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 23\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m__init__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0my\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msize\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 24\u001b[0;31m \u001b[0mRectangle\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__init__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0my\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msize\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msize\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 25\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 26\u001b[0m \u001b[0;32mclass\u001b[0m \u001b[0mLine\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mShape\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;32m\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, x, y, w, h, **kwargs)\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;32mclass\u001b[0m \u001b[0mRectangle\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mShape\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m__init__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0my\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mw\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mh\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 10\u001b[0;31m \u001b[0mShape\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__init__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 11\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 12\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0my\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0my\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;31mTypeError\u001b[0m: object.__init__() takes no parameters"]}]},{"cell_type":"markdown","metadata":{"id":"qZMxL_rWcg1o","colab_type":"text"},"source":["5. Add to `RasterDrawing` two functions, `save(filename)` and `load(filename)`. The save function writes the `__str__()` of all of the shapes in the drawing to a file (one shape per line). The load function, reads the file, and instantiates each object using the python `eval(expression)` function, and adds each shape to the drawing, thereby recreating a \"saved\" raster drawing. Use this functionality to save and load your happy face.\n","\n"," `eval` takes a string that contains a fragment of a python code and executes it. Consider the following examples: "]},{"cell_type":"code","metadata":{"id":"SeCK_szBgeWF","colab_type":"code","colab":{}},"source":["class RasterDrawing:\n"," def __init__(self):\n"," self.shapes=dict()\n"," self.shape_names=list()\n"," \n"," def add_shape(self,shape):\n"," if shape.name == \"\":\n"," shape.name = self.assign_name()\n"," \n"," self.shapes[shape.name]=shape\n"," self.shape_names.append(shape.name)\n"," \n"," def paint(self,canvas):\n"," for shape_name in self.shape_names:\n"," self.shapes[shape_name].paint(canvas)\n"," \n"," def assign_name(self):\n"," name_base=\"shape\"\n"," name = name_base+\"_0\"\n"," \n"," i=1\n"," while name in self.shapes:\n"," name = name_base+\"_\"+str(i)\n"," \n"," return name\n"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"uQpI9tgcgGgu","colab_type":"code","colab":{}},"source":["class RasterDrawing:\n"," def __str__():\n"," return 'Shape(name=',self.name,')'\n"," \n"," def save(filename):\n"," return __str__(self.shape_names)\n","\n"," def load(filename):\n"," return eval(__str__(self.shape_names))\n"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"uAV4rOyKhdX3","colab_type":"code","outputId":"e6271079-d8d4-436f-e4b4-9f7e2e58378e","executionInfo":{"status":"error","timestamp":1589231602880,"user_tz":300,"elapsed":369,"user":{"displayName":"","photoUrl":"","userId":""}},"colab":{"base_uri":"https://localhost:8080/","height":279}},"source":["RasterDrawing.save(Rectangle)"],"execution_count":0,"outputs":[{"output_type":"error","ename":"NameError","evalue":"ignored","traceback":["\u001b[0;31m---------------------------------------------------------------------------\u001b[0m","\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)","\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mRasterDrawing\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msave\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mRectangle\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m","\u001b[0;32m\u001b[0m in \u001b[0;36msave\u001b[0;34m(filename)\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0;34m'Shape(name='\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m')'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0msave\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfilename\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0m__str__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshape_names\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 6\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mload\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfilename\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;31mNameError\u001b[0m: name '__str__' is not defined"]}]},{"cell_type":"code","metadata":{"id":"EjvRFxP_cg1p","colab_type":"code","outputId":"fc5feac6-2d4d-47de-e363-fe5a293a9092","executionInfo":{"status":"ok","timestamp":1589230596973,"user_tz":300,"elapsed":312,"user":{"displayName":"","photoUrl":"","userId":""}},"colab":{"base_uri":"https://localhost:8080/","height":34}},"source":["eval(\"print('Hello')\")"],"execution_count":0,"outputs":[{"output_type":"stream","text":["Hello\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"njPlmMYKcg1s","colab_type":"code","outputId":"45a0a528-ee6f-4f1b-8b2b-0b3617e6fe0e","executionInfo":{"status":"ok","timestamp":1589230190152,"user_tz":300,"elapsed":316,"user":{"displayName":"","photoUrl":"","userId":""}},"colab":{"base_uri":"https://localhost:8080/","height":34}},"source":["x = eval('1+2')\n","print(x)"],"execution_count":0,"outputs":[{"output_type":"stream","text":["3\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"GbO_EA2Dcg1w","colab_type":"code","colab":{}},"source":[""],"execution_count":0,"outputs":[]}]} \ No newline at end of file