From 1f2c2f4ac9a2d495e69997c72e4b0b51d0724578 Mon Sep 17 00:00:00 2001 From: nicholassingleton352 <60523574+nicholassingleton352@users.noreply.github.com> Date: Fri, 7 Feb 2020 13:21:29 -0600 Subject: [PATCH 01/10] Add files via upload --- Labs/Lab-1/Copy_of_Lab_1.ipynb | 755 +++++++++++++++++++++++++++++++++ 1 file changed, 755 insertions(+) create mode 100644 Labs/Lab-1/Copy_of_Lab_1.ipynb diff --git a/Labs/Lab-1/Copy_of_Lab_1.ipynb b/Labs/Lab-1/Copy_of_Lab_1.ipynb new file mode 100644 index 0000000..0e8b2b5 --- /dev/null +++ b/Labs/Lab-1/Copy_of_Lab_1.ipynb @@ -0,0 +1,755 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "colab": { + "name": "Copy of Lab-1.ipynb", + "provenance": [], + "collapsed_sections": [] + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "O5vg8KKRq0sy", + "colab_type": "text" + }, + "source": [ + "# Lab 1\n", + "\n", + "## Python Notebooks on Google Colab\n", + "\n", + "Data 1401's Labs, Homework, and Exams will be all in form of iPython notebooks. You may already be familiar with python notebooks if you have used Jupyter before, for example in Data 1301. If so, you are welcome to use whatever means you have to run Jupyter notebooks for this course, though you may get limited support. Our primary means of running python notebooks will be through [Google Colab](https://colab.research.google.com) and we will be storing files on google drive.\n", + "\n", + "You will need a google account. If you do not have one or you wish to use a different account for this course, please follow [these instructions](https://edu.gcfglobal.org/en/googledriveanddocs/getting-started-with-google-drive/1/) to make an account.\n", + "\n", + "Once you are ready with your account, you can continue in Colab. Click on the following badge to open this notebook in Colab:\n", + "\n", + "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github//afarbin/DATA1401-Spring-2020/blob/master/Labs/Lab-1/Lab-1.ipynb)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "FVt_1hPt1dAK", + "colab_type": "text" + }, + "source": [ + "## Notebooks in Colab\n", + "\n", + "You now are presumably in Colab. Word of caution, by default, Google Colab does not save your notebooks, so if you close your session, you will loose your work.\n", + "\n", + "So first thing: from the file menu above select \"Save a copy in Drive\"." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "x0JBL_RFrDDj", + "colab_type": "text" + }, + "source": [ + "## Storing Notebooks in Google Drive\n", + "A better way to work is to save your notebooks directly into Google Drive and upload directly to Git (where you will be downloading and uploading your homework). In order properly setup Git, we'll need to work more directly in your Google Drive.\n", + "\n", + "On the left sidebar, press the file icon to see a listing of files accessibile to this Notebook. Then press \"Mount Drive\" and follow the instructions to mount your Google Drive in this notebook. A new cell will be inserted into this notebook, which after you run by pressing the play button will instruct you to follow a link to log into your Google Account and enable access to your Drive in another tab. Finally you will copy a link from the new tab back into the cell in this notebook. Once you are done, press refresh under files in the left sidebar and you should have \"drive/My Drive\" appear." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "hwJ6wJk3tiLv", + "colab_type": "text" + }, + "source": [ + "## Github\n", + "All the class material will be stored on github. You will also submit your homework using github. To do so, you will need a github account.\n", + "\n", + "If you do not already have a github account or wish to create a new one for this course, create one:\n", + "* Browse to [github.com](https://github.com).\n", + "* Click the green “Sign up for GitHub”\tbutton.\n", + "* Follow instructions for creating an account.\n", + "* Make sure you remember your github username and password.\n", + "\n", + "Write an email to the course TA titled \"Data 1401: Github account\" with your github username (not your password) as the contents.\n", + "\n", + "## Google Groups\n", + "\n", + "Class annoucements will be made via google groups. If you did not already receive an invite to the class google group, had trouble with the invite, or wish to use a different email address, write an email to the course TA titled \"Data 1401: Google Group\" with your preferred email.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "TjfIzdQZqvzk", + "colab_type": "text" + }, + "source": [ + "## Introduction: Unix, Git, and Jupyter\n", + "\n", + "This lab aims to introduce you to basic Unix, familiarize you with iPython notebooks and get you setup to submit your homework.\n", + "*italicized text*" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "C_LmOgzFqvzp", + "colab_type": "text" + }, + "source": [ + "\n", + "\n", + "### Terminal, Shell, and ssh\n", + "\n", + "\n", + "The terminal is a simple program that generally runs another program, taking mostly keyboard input from you, passing it to this other program, and taking the output of the program and displaying on the screen for you.\n", + "\n", + "The terminal usually runs a program called a shell. Shells present a command prompt where you can type in commands, which are then executed when you press enter. In most shells, there are some special commands which the shell will execute. Everything else you type in, the shell will assume is a name of a program you want to run and arguments you want to pass that program. So if the shell doesn't recognize something you type in, it'll try to find a program with a name that is the same as the first word you gave it. \n", + "\n", + "### Shell in Colab\n", + "\n", + "Unfortunately, google Colab does not allow you to open a terminal window. Jupyter does, so if you are running in Jupyter (which most of you will not be), you may choose to open a terminal window by returning to the jupyter file list tab and selecting new terminal from the top right.\n", + "\n", + "For Colab, we will have to do something non-ideal, but functional. There are several ways to execute shell commands from within a python notebook. For example, you can use any shell command by putting \"!\" in front of the command:\n", + "\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "KJ5f-WO0wcAv", + "colab_type": "code", + "colab": {} + }, + "source": [ + "!ls\n", + "!echo \"----------\"\n", + "!ls sample_data" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "8f-n4AXFw-dD", + "colab_type": "text" + }, + "source": [ + "Unfortunately, every time you use \"!\" a new environment is created and the state reverted to the original state. Try to understand the difference between the following two sets of commands:\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "99nrBYTWxZJr", + "colab_type": "code", + "colab": {} + }, + "source": [ + "!echo \"Technique 1:\"\n", + "!ls\n", + "!cd sample_data\n", + "!ls" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "2-Znf97Lxl-Z", + "colab_type": "code", + "colab": {} + }, + "source": [ + "!echo \"Technique 2:\"\n", + "!ls ; cd sample_data ;ls" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "4x9n1rAkxyYl", + "colab_type": "text" + }, + "source": [ + "Notebooks allow a bit of \"magic\" (using \"%\") to avoid some of these limitations:\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "vLBPTX4rx3gd", + "colab_type": "code", + "colab": {} + }, + "source": [ + "!echo \"Technique 3:\"\n", + "!ls \n", + "%cd sample_data \n", + "!ls" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "U8XpvPjcyH0w", + "colab_type": "text" + }, + "source": [ + "For our purposes, we are just going to explicitly start a new shell and interact with it in the output cell. Execute the following cell. You will be able to type and execute commands. Look around a bit using \"ls\" and \"cd. You can stop the cell from running by typing \"exit\"." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "MIDFitLZyuZy", + "colab_type": "code", + "outputId": "428e21f9-bf40-4c57-dc64-b6a15149e791", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 505 + } + }, + "source": [ + "!/bin/bash --noediting" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "bash: cannot set terminal process group (122): Inappropriate ioctl for device\n", + "bash: no job control in this shell\n", + "\u001b]0;root@2ef5d1904db0: /content\u0007\u001b[01;32mroot@2ef5d1904db0\u001b[00m:\u001b[01;34m/content\u001b[00m# \"cat>favorite-colors-list.txt\"\n", + "bash: cat>favorite-colors-list.txt: command not found\n", + "\u001b]0;root@2ef5d1904db0: /content\u0007\u001b[01;32mroot@2ef5d1904db0\u001b[00m:\u001b[01;34m/content\u001b[00m# \"cat\"\n", + "\"cat>favorite-colors-list.txt\"\n", + "\"cat>favorite-colors-list.txt\"\n", + "Blue\n", + "Blue\n", + "Green\n", + "Green\n", + "Red\n", + "Red\n", + "\"^D\"\n", + "\"^D\"\n", + "\">>\"\n", + "\">>\"\n", + "Yellow\n", + "Yellow\n", + "Orange\n", + "Orange\n", + "\"sortsorted-favorite-color-list.txt\"\n", + "\"sortsorted-favorite-color-list.txt\"\n", + "\"sortsorted-users-TACC.txt\"\n", + "\"sortsorted-users-TACC.txt\"\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "q-4hfZBywW25", + "colab_type": "text" + }, + "source": [ + "While in this instance your shell is running in a this notebook, you can also run terminals natively on your own computer. On Linux or MacOS, you just have to run a program called terminal. In Windows you can start a \"command prompt\". \n", + "\n", + "\n", + "Type in \"ls\" into the terminal and press enter. The shell will find a program called \"ls\", a standard tool in Unix, and run it. \"ls\" lists the contents (files and directories) of your current directory. If you are just starting in this course, you probably only see the git repository you cloned. \n", + "\n", + "A subtle point to realize here is that while the terminal is running in the browser that is running on the computer in front of you, the shell is actually running on a machine on google hardware. The shell prompt typically displays the name of the machine you are using. What you are not seeing is that there is an intermidate program between the terminal running on your computer and the shell running on google. This intermidary program is taking your input from the terminal sending it over the network to google and bringing back the responses for you terminal to display.\n", + "\n", + "A bit of extra information. If you start a terminal on your own computer, the shell runs locally. The \"ls\" command would then list contents of a directory on your computer. You can typically connect to Unix computers by evoking a shell running on that machine over the network. In this case, you would have to initiate this intermidiary program yourself. The program is called \"ssh\" (secure shell). You can \"ssh\" to another machine from your machine, by simply typing \"ssh\" followed by the machine name or IP address. Most likely you would be prompted for a password, after which you would dropped into the prompt of a shell running on the remote machine. \n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "EuN7SJliS5KE", + "colab_type": "text" + }, + "source": [ + "" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "51Eya4LBqvzs", + "colab_type": "text" + }, + "source": [ + "## Programs and Environment Variables\n", + "\n", + "You have a listing of your current directory, but you don't know where that directory resides. You can see what directory you are using the command \"pwd\" (print working directory). Issue the command and look at the response. You'll get a slash (\"/\") separated list, known as the path, of the directory hierarchy of your current working directory. On Colab, this will start with \"contents\"\n", + "\n", + "Now back to thinking about the command prompt. Since \"ls\" is a program, it most be stored somewhere. It is clearly not in your working directory, because you didn't see it when you executed \"ls\". We can ask the shell to tell us where it found \"ls\" using the \"which ls\" command. Note that \"which\" is also a program. \"which ls\" comes back with \"/bin/ls\", telling you the \"ls\" program is sitting in \"/bin\" directory of the system. \n", + "\n", + "Lets see what else is in there by issuing a \"ls /bin\" command. You will get a long list of programs. You can run any of these programs by just typing their names and pressing enter. You may be able to guess what some of these programs do, but if you want to know, most of them provide you help, using \"--help\" or \"-h\" flag. For example execute \"ls --help\". For more information about a program or command, you can use Unix's manual pages using the \"man\" command. Try typing \"man ls\". Note that you will need to press space to scroll through lengthy manual pages and \"q\" to exit back to the shell prompt. \n", + "\n", + "Another command interesting is \"echo\". \"echo\" simply prints whatever you put after it to the screen. Try executing \"echo Hello World.\"\n", + "\n", + "At this point, you may wonder how was it that the shell knew to look for programs in \"/bin\"? The shell keeps a list of places to look for programs an environment variable with the name \"PATH\". The shell keeps a table that map string variable names to string expressions. When the shell starts, its configuration files set some environment variables that it uses. You can see the full list of defined environment variables using the command \"printenv\".\n", + "\n", + "You can use a environment variable in a shell by prepending name of the variable with a dollar sign character (\"\\$\"). So you can print out the PATH environment variable using the command \"echo $PATH\". What you will see is a colon (\":\") separated list of directories that the shell will search (in order) whenever you type in anything.\n", + "\n", + "You can set you own environment variables. Different shells have different syntax. Lets first figure out what shell we are running. \n", + "\n", + "*Exercise 1:* Use the \"echo\" command to print out the value of the \"SHELL\" environment variable:" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "YS7YFiPwqvzu", + "colab_type": "text" + }, + "source": [ + "!/bin/bash --noediting" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "YoEgruUhqvzw", + "colab_type": "text" + }, + "source": [ + "## Navigating Directories\n", + "\n", + "You can change your current directory using the \"cd\" shell command. Note that \"cd\" is not a Unix program. Once in a directory, you can use the \"ls\" command to list the contents or \"pwd\" to remind yourself your current working directory. You can move back one level in your current directory hierarchy using \"cd ..\". In general \"..\" represents the path to a directory one level above your current directory, \"../..\" represents two levels up, and so on. \".\" represents the current directory. If you look at the PATH environment variable, you'll notice that the last item is \".\", telling the shell to look into your current directory for commands. Finally the \"~\" character always refers to your home directory.\n", + "\n", + "Some other file manipulation commands:\n", + "\n", + " - The \"mkdir\" command creates new directories. \n", + " - \"cp\" and \"mv\" allow you to copy and move (or rename) files, taking 2 arguments: the original path/filename and the target path/filename. \n", + " - The \"rm\" and \"rmdir\" commands remove (delete) files and directories.\n", + "\n", + "\n", + "*Exercise 2:* Using the \"cd\" command, navigate into \"drive/My\\ Drive\" directory. Create a new directory called \"Data-1441\", and another directory inside \"Data-1441\" called \"Lab-1-Solutions\". Perform the rest of the lab in this directory." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "A16VzZ3G0J8x", + "colab_type": "code", + "colab": {} + }, + "source": [ + "!cd \"drive/My\\Drive\"\n" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "o38c4lbsqvzy", + "colab_type": "text" + }, + "source": [ + "## Exploring Unix Filesystem\n", + "\n", + "You can look at the root directory of the system by issuing \"ls /\". As explained in lecture, Unix uses the file system to communicate with devices and between processes. \"/etc\" keeps the configuration files of the system. \"/bin\" and \"/sbin\" store most of the standard Unix programs. \"/usr\" stores installes programs and their associate files, with \"/usr/bin\" usually storing the commands you can run. \n", + "\n", + "*Exercise 3:* List the \"/dev\" directory. How many SSD storage devices do you see? How many partitions does each device have? (Answer in box below)" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "yNj2LXzP2ksl", + "colab_type": "code", + "colab": {} + }, + "source": [ + "1 SSD storage and 1 partitions" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "7P9EG0KOqvz2", + "colab_type": "text" + }, + "source": [ + "## Text File Manipulation\n", + "\n", + "As explained in lecture, Unix stores most information in text files. For example, the list of all users and their home directories are stored in \"/etc/passwd\". Let get some familiarity with the most commonly used commands to manipulate files.\n", + "\n", + " - You can see the contents contents a file using the \"cat\" (concatenate) command. Try executing \"cat /etc/passwd\". You'll get a huge list that will go by your screen quickly. \n", + " \n", + " - To go through the file page by page, you can use the \"less\" or \"more\" commands. \n", + " \n", + " - You can see the first or last N (N=10 by default) lines of a file using \"head\" or \"tail\" commands. For example \"tail -20 /etc/passwd\" will list the last 20 lines. \n", + " \n", + " - You can search a test file using the \"grep\" command, which takes a string keyword as the first argument and a filename as the second, and by default prints out every line in the file that contrains the string. So for example you can do \"grep \\$USER /etc/passwd\" to find the line corresponding to your account. Some useful flags: \n", + " \n", + " - \"-i\" ignores the case of the keyword\n", + " - \"-v\" display those lines that do NOT match \n", + " - \"-n\" precede each matching line with the line number \n", + " - \"-c\" print only the total count of matched lines \n", + " \n", + " For example \"grep -c \\$USER /etc/passwd\" should show that you are in the password file just once. \n", + " \n", + " - The \"wc\" (word count) command counts the number of lines, words, and characters in a file. By default \"wc\" gives you all three numbers, but \"-w\", \"-l\", or \"-c\" flags \n", + "\n", + "*Exercise 4:* Count how many lines in the password file contain the letter \"w\". " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "UlsANMuf2qMs", + "colab_type": "code", + "colab": {} + }, + "source": [ + "!/bin/bash --noediting" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "SZuhLbD8qvz5", + "colab_type": "text" + }, + "source": [ + "## Redirection\n", + "\n", + "Unix provides programs \"pipes\" for input and output. Most of what you see on the screen when you run a program was written to the \"stdout\" (standard output) pipe. Other pipes are \"stdin\" (standard input) and \"stderr\" (standard error), where error messages are written.\n", + "\n", + "As discussed in lecture, the basic commands of are simple, but you can chain them to do complicated things. Redirection is how you chain these commands, directing the output of one command to the input of the next.\n", + "\n", + "As an example, consider the \"cat\" command. Cat takes stdin and outputs it to stdout. Type \"cat\" and press enter and confirm. You can get back to the command prompt by pressing \"control-c\" (sends terminate singal) or \"control-d\" (end of file character). Note that from now on we will use the convention: \"control-d\" = \"^D\"\n", + "\n", + "*Exercise 5a:* Using \"cat\" and indirection you can write things into a file. The \">\" symbol directs stdout into a file. Try \"cat > favorite-colors-list.txt\" and then type in your 3 favorite colors, each on it's own line. Use \"^D\" to end your input." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "H5vxtcXnqvz6", + "colab_type": "text" + }, + "source": [ + "Use \"cat\", \"more\", or \"less\" to confirm that you file is as you expect it. \">>\" allows you to append to the file. \n", + "\n", + "*Exercise 5b:* Append 2 more colors to your file." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "twRKNaGy3XGw", + "colab_type": "code", + "colab": {} + }, + "source": [ + "!/bin/bash --noediting" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "DZODNKiAqvz8", + "colab_type": "text" + }, + "source": [ + "The \"sort\" command sorts what it sees on stdin. Instead of taking input from the terminal, you can direct the shell to take stdin from a file using \"<\". Try \"sort < favorite-color-list.txt\" and \"sort < favorite-color-list.txt > sorted-favorite-color-list.txt\".\n", + "\n", + "Finally, instead of piping input / output into files, you can directly chain one program into another using \"|\". So for example, you can do \"cat /etc/passwd | grep -i \\$USER | wc -l\". \n", + "\n", + "*Exercise 5c:* Use indirection to count the number of users on TACC with your first name. Copy the command you used into box below." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "oP9XlZl_3iZD", + "colab_type": "code", + "colab": {} + }, + "source": [ + "!/bin/bash --noediting" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "v5IaZXNyqvz_", + "colab_type": "text" + }, + "source": [ + "## Git\n", + "\n", + "`git` is a Version Control System (VCS), typically used to organize the source code of software project but also good source of documents or web-pages. An instance of `git` server stores repositories, each typically containing the code relevant to a specific project. Users create local `clones` of repositories, change and develop the local copies of the code, `commit` the changes to their local repository, `push` to the server as a contribution, \n", + "`pull` updates from the server, and `merge` changes between local and remote versions. \n", + "\n", + "Besides cloning, repositories can be branched or forked. A repository generally starts with a `master` branch that evolves as push requests are merged in. Creating a new branch from an existing branch creates a snapshot of the which can evolve independently or be merged in later. Branches are easy to make and delete, and can serve various purposes. They can represent a stable version of software package. Or a parallel development for different operating system. A fork of a repository is a standalone instance of the repository which can be stored and managed independently from the original, where you can work independently without constraints or interference. \n", + "\n", + "[GitHub](github.com) provides a massive publically accessible instance of a `git` system besides sharing code, projects can be developed by the open source community. It provides tools for managing your repository and a wiki for documentation. Contributions to public software on GitHub generally require making a merge request which would be judged by the managers of the repository. That's why most software packages enourage you to create a new fork, so you can work independently.\n", + "\n", + "Lets take a look at some repositories:\n", + "\n", + "* [This class](https://github.com/afarbin/DATA1401-Spring-2020)\n", + "\n", + "\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "J_R64sQDqv0A", + "colab_type": "text" + }, + "source": [ + "## Plan\n", + "\n", + "You made a clone of the class repository at start of this lab. We will create a new fork where you can keep track and submit your work, following [these instructions](https://help.github.com/articles/fork-a-repo/).\n", + "\n", + "Goto to github.com and log in.\n", + "\n", + "Next, lets create a fork of the [class repository](https://github.com/afarbin/DATA1401-Spring-2019). Click the link and press the \"Fork\" button on the top right. Select your repository as where you want to place the fork.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "edTvE6rOqv0C", + "colab_type": "text" + }, + "source": [ + "Now we will check out your fork in your Google Drive / Colab.\n", + "\n", + "Note: Jupyter allows you to run shell directly in a notebook. We will use `!` and `%` to call shell commands directly in this notebook. Follow along yourself. Either create a new notebook or open a terminal. \n", + "\n", + "Start by listing the contents of your current directory." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "e5tXg0f8qv0D", + "colab_type": "code", + "colab": {} + }, + "source": [ + "%cd /content/drive/My\\ Drive\n", + "!ls" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "WYsyYcg1qv0J", + "colab_type": "text" + }, + "source": [ + "Make a new directory:" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Z7noY1hMqv0L", + "colab_type": "code", + "colab": {} + }, + "source": [ + "!mkdir Data-1401-Repo\n", + "%cd Data-1401-Repo" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "fwsBdTnYqv0Q", + "colab_type": "text" + }, + "source": [ + "From the github page for your fork, press the green \"Clone or download\" button and copy the URL.\n", + "\n", + "Goto to your notebook and use the following command to clone the repository, pasting the URL you just copied:\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "8w42MH6Jqv0S", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# What you past here should look like:\n", + "#!git clone https://github.com//DATA1401-Spring-2020.git" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "cOAuqTVUqv0V", + "colab_type": "text" + }, + "source": [ + "Go into the directory:" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "b1Ew4tEZqv0X", + "colab_type": "code", + "colab": {} + }, + "source": [ + "%cd DATA1401-Spring-2020\n", + "!ls" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "IrhWToc-qv0a", + "colab_type": "text" + }, + "source": [ + "We will now connect your fork to the original so you can pull changes from there. \n", + "\n", + "Check remote status:" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "JxtMYR-9qv0c", + "colab_type": "code", + "colab": {} + }, + "source": [ + "!git remote -v" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "9ud3X0fBqv0f", + "colab_type": "text" + }, + "source": [ + "Now use the original class URL to set your upstream:" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "pgJlKxBqqv0h", + "colab_type": "code", + "colab": {} + }, + "source": [ + "!git remote add upstream https://github.com/afarbin/DATA1401-Spring-2020.git" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "id2yUEt9qv0k", + "colab_type": "code", + "colab": {} + }, + "source": [ + "!git remote -v" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "sAkgeJ6Iqv0n", + "colab_type": "text" + }, + "source": [ + "From now on, you can get the newest version of class material by using:" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "AGDsfTFLqv0o", + "colab_type": "code", + "colab": {} + }, + "source": [ + "!git pull" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "u9RAhs5b4vXY", + "colab_type": "text" + }, + "source": [ + "We will submit your Lab 1 using git at the next Lab." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "PPfGmFQI40HR", + "colab_type": "code", + "colab": {} + }, + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + } + ] +} \ No newline at end of file From f92696b49159286f61f8d5a7fb036eb946a9471f Mon Sep 17 00:00:00 2001 From: nicholassingleton352 <60523574+nicholassingleton352@users.noreply.github.com> Date: Fri, 14 Feb 2020 14:58:02 -0600 Subject: [PATCH 02/10] Add files via upload --- Labs/Lab-2/Copy_of_Lab_2.ipynb | 1144 ++++++++++++++++++++++++++++++++ 1 file changed, 1144 insertions(+) create mode 100644 Labs/Lab-2/Copy_of_Lab_2.ipynb diff --git a/Labs/Lab-2/Copy_of_Lab_2.ipynb b/Labs/Lab-2/Copy_of_Lab_2.ipynb new file mode 100644 index 0000000..ae7e939 --- /dev/null +++ b/Labs/Lab-2/Copy_of_Lab_2.ipynb @@ -0,0 +1,1144 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "colab": { + "name": "Copy of Lab-2.ipynb", + "provenance": [], + "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": "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", + "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/=0:\n", + " if x[b] == ' ':\n", + " start = b+1\n", + " while start != end:\n", + " result += x[start]\n", + " start+=1\n", + " result+=' '\n", + " end = b\n", + " b-=1\n", + " start = 0\n", + " while start!=end:\n", + " result+=x[start]\n", + " start+=1\n", + " return result\n" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "nQyhnLZ_dBHn", + "colab_type": "code", + "outputId": "6f6e4aa2-cbee-48f3-e057-9234026b453e", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "Reversewords(\"Nicholas is my name\")" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "'name my is Nicholas'" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 13 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "NFSmRaSydBHq", + "colab_type": "text" + }, + "source": [ + "*Exercise 10:* Write a guessing game program that will repeatedly guess a number that the users picks, with the user indicating higher or lower, until it correctly guesses the number." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Ie2E1JzCdBHr", + "colab_type": "code", + "outputId": "09f9752a-57b6-4702-d597-ce7a6138ea22", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 437 + } + }, + "source": [ + "from random import randint\n", + "\n", + "print (\"In this program you will enter a number between 1 - 100.\"\n", + " \"\\nAfter the computer will try to guess your number!\")\n", + "\n", + "number = 0\n", + "\n", + "while number < 1 or number >100:\n", + " number = int(input(\"\\n\\nEnter a number for the computer to guess: \"))\n", + " \n", + "\n", + "guess = randint(1, 100)\n", + "\n", + "print (\"The computer takes a guess...\", guess)\n", + "\n", + "while guess != number:\n", + " if guess > number:\n", + " guess -=1\n", + " guess = randint(1, guess)\n", + " print (\"guess is too high\")\n", + " else:\n", + " print (\"guess is too low\")\n", + " guess += 1\n", + " guess = randint(guess, 100)\n", + " print (\"The computer takes a guess...\", guess)\n", + " \n", + "\n", + "print (\"The computer guessed\", guess, \"and it was correct!\")" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "In this program you will enter a number between 1 - 100.\n", + "After the computer will try to guess your number!\n", + "\n", + "\n", + "Enter a number for the computer to guess: 45\n", + "The computer takes a guess... 75\n", + "guess is too high\n", + "The computer takes a guess... 39\n", + "guess is too low\n", + "The computer takes a guess... 53\n", + "guess is too high\n", + "The computer takes a guess... 18\n", + "guess is too low\n", + "The computer takes a guess... 54\n", + "guess is too high\n", + "The computer takes a guess... 15\n", + "guess is too low\n", + "The computer takes a guess... 73\n", + "guess is too high\n", + "The computer takes a guess... 12\n", + "guess is too low\n", + "The computer takes a guess... 36\n", + "guess is too low\n", + "The computer takes a guess... 45\n", + "The computer guessed 45 and it was correct!\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "6T8YdWSMdBHs", + "colab_type": "code", + "colab": {} + }, + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + } + ] +} \ No newline at end of file From a69c3aa000a89261b390bd473afb04eb728aa2ba Mon Sep 17 00:00:00 2001 From: nicholassingleton352 <60523574+nicholassingleton352@users.noreply.github.com> Date: Fri, 28 Feb 2020 14:52:06 -0600 Subject: [PATCH 03/10] Add files via upload --- Labs/Lab-3/Copy_of_Lab_3.ipynb | 728 +++++++++++++++++++++++++++++++++ 1 file changed, 728 insertions(+) create mode 100644 Labs/Lab-3/Copy_of_Lab_3.ipynb diff --git a/Labs/Lab-3/Copy_of_Lab_3.ipynb b/Labs/Lab-3/Copy_of_Lab_3.ipynb new file mode 100644 index 0000000..6b3d5c1 --- /dev/null +++ b/Labs/Lab-3/Copy_of_Lab_3.ipynb @@ -0,0 +1,728 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "colab": { + "name": "Copy of Lab-3.ipynb", + "provenance": [], + "collapsed_sections": [] + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "7ShMYtF_Ri2m", + "colab_type": "text" + }, + "source": [ + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "# Lab 3- Tic Tac Toe\n", + "\n", + "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github//afarbin/DATA1401-Spring-2020/blob/master/Labs/Lab-3/Lab-3.ipynb)\n", + "\n", + "In this lab your will build a n x n Tic Tac Toe game. As you do the exercises, make sure your solutions work for any size Tic Tac Toe game. " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "RC2wuDbqRi2w", + "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": "ZdPhdgNgRi20", + "colab_type": "code", + "colab": {} + }, + "source": [ + "empty = 0\n", + "X=1\n", + "O=2\n", + "#Game board size\n", + "size = 3\n" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "deletable": true, + "editable": true, + "id": "T-HRsl25Ri2_", + "colab_type": "code", + "outputId": "90a0d757-f235-41a5-ecc7-c7781be27f86", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "[3]*8" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "[3, 3, 3, 3, 3, 3, 3, 3]" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 23 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "deletable": true, + "editable": true, + "id": "Fg6L74F6Ri3J", + "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": "YHt5kt9JRi3L", + "colab_type": "code", + "outputId": "31cd5196-b35f-4e97-9852-b63f996ca639", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 119 + } + }, + "source": [ + "\n", + "winner_is_2 = [[2, 2, 0],\n", + "\t[2, 1, 0],\n", + "\t[2, 1, 1]]\n", + "print (\"Player 2 wins\")\n", + "\n", + "winner_is_1 = [[1, 2, 0],\n", + "\t[2, 1, 0],\n", + "\t[2, 1, 1]]\n", + "print (\"Player 1 wins the game\")\n", + "\n", + "winner_is_also_1 = [[0, 1, 0],\n", + "\t[2, 1, 0],\n", + "\t[2, 1, 1]]\n", + "print (\"Player 1 wins the game\")\n", + "\n", + "no_winner = [[1, 2, 0],\n", + "\t[2, 1, 0],\n", + "\t[2, 1, 2]]\n", + "print (\"Draw Game !\")\n", + "\n", + "also_no_winner = [[1, 2, 0],\n", + "\t[2, 1, 0],\n", + "\t[2, 1, 0]]\n", + "print (\"Draw Game !\")\n", + "\n", + "again_no_winner = [[0,0,0],\n", + " [0,0,0],\n", + " [0,0,0]]\n", + "print (\"The Game is incomplete\") \n" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Player 2 wins\n", + "Player 1 wins the game\n", + "Player 1 wins the game\n", + "Draw Game !\n", + "Draw Game !\n", + "The Game is incomplete\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "deletable": true, + "editable": true, + "id": "2qxHwtCPRi3S", + "colab_type": "code", + "colab": {} + }, + "source": [ + "size = 3\n", + "board=list()\n", + "for i in range(3):\n", + " row=list()\n", + " for j in range(3):\n", + " row.append(empty)\n", + " \n", + " board.append(row)\n", + " \n", + "\n", + "board\n", + "\n", + "if board[0][0] == board[0][1] == board[0][2] != 2:\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')" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "deletable": true, + "editable": true, + "id": "KKMIbxJzRi3c", + "colab_type": "code", + "outputId": "fa350456-29d8-4de5-ab88-da5ff5333888", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + " board[1][0]\n" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "0" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 38 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "deletable": true, + "editable": true, + "id": "mKO9ktbBRi3j", + "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": "wDf7U5CXRi3l", + "colab_type": "code", + "colab": {} + }, + "source": [ + "def print_horiz_line(board_size):\n", + " print(\" --- \" * board_size)\n", + "\n", + "def print_vert_line(board_size):\n", + " print(\"| \" * (board_size + 1))\n", + "\n", + "#if __name__ == \"__main__\":\n", + "# board_size = int(input(\"What size of game board? \"))\n", + "\n", + "def draw_board(board_size):\n", + " for index in range(board_size):\n", + " print_horiz_line(board_size)\n", + " print_vert_line(board_size)\n", + " print_horiz_line(board_size)\n", + "\n", + "\n", + "\n", + " " + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "deletable": true, + "editable": true, + "id": "tiQ3fEscRi3r", + "colab_type": "code", + "outputId": "fb1e047a-b384-41b1-e0cf-4825f26e229c", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 136 + } + }, + "source": [ + "draw_board(3)" + ], + "execution_count": 62, + "outputs": [ + { + "output_type": "stream", + "text": [ + " --- --- --- \n", + "| | | | \n", + " --- --- --- \n", + "| | | | \n", + " --- --- --- \n", + "| | | | \n", + " --- --- --- \n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "deletable": true, + "editable": true, + "id": "aP86Q1ebRi3y", + "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": "9V9tlnclRi3z", + "colab_type": "code", + "outputId": "f2b50249-459a-4c75-9782-4f335a5152fe", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "\n", + "player_1 = 1\n", + "player_2 = 2\n", + "empty = 0\n", + "player_1_piece=\"X\"\n", + "player_2_piece=\"O\"\n", + "empty_space=\" \"\n", + "space_character= { player_1: player_1_piece,\n", + " player_2: player_2_piece,\n", + " empty: empty_space }\n", + "\n", + "space_character\n" + ], + "execution_count": 78, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "{0: ' ', 1: 'X', 2: 'O'}" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 78 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "deletable": true, + "editable": true, + "id": "1JFvK4WSRi35", + "colab_type": "code", + "colab": {} + }, + "source": [ + " draw_board(3)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "deletable": true, + "editable": true, + "id": "ZVUFnsELRi3-", + "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": "mpOUvc8TRi3_", + "colab_type": "code", + "colab": {} + }, + "source": [ + "def move_piece(board,player,location,move,verbose=True):\n", + " x,y=location\n", + " if not board[x][y] == player:\n", + " print_message(\"Player does not have piece at location.\",verbose)\n", + " return False\n", + " # Fetch the offset for the move\n", + " x_offset,y_offset = moves[player][move]\n", + " \n", + " # Make sure the move is on the board:\n", + " move_possible= x+x_offset < size and \\\n", + " x+x_offset >= 0 and \\\n", + " y+y_offset < size and \\\n", + " y+y_offset >= 0\n", + " \n", + " \n", + " jump_possible= x+2*x_offset < size and \\\n", + " x+2*x_offset >= 0 and \\\n", + " y+2*y_offset < size and \\\n", + " y+2*y_offset >= 0\n", + " \n", + "\n", + " \n", + " " + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "deletable": true, + "editable": true, + "id": "FUVqoMRXRi4E", + "colab_type": "code", + "colab": {} + }, + "source": [ + "player_choice(3)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "deletable": true, + "editable": true, + "id": "xUI-eERlRi4J", + "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": "3GEVozq8Ri4K", + "colab_type": "code", + "colab": {} + }, + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "deletable": true, + "editable": true, + "id": "lhzpTw5hRi4Q", + "colab_type": "code", + "colab": {} + }, + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "deletable": true, + "editable": true, + "id": "bclOar-xRi4U", + "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": "bZS4A2KrRi4W", + "colab_type": "code", + "colab": {} + }, + "source": [ + "\n", + "\n" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "deletable": true, + "editable": true, + "id": "5fflBRjLRi4c", + "colab_type": "code", + "colab": {} + }, + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "deletable": true, + "editable": true, + "id": "zhss69S1Ri4i", + "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": "l4O3SGrRRi4k", + "colab_type": "code", + "colab": {} + }, + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "deletable": true, + "editable": true, + "id": "lP_7WvhARi4q", + "colab_type": "code", + "colab": {} + }, + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "deletable": true, + "editable": true, + "id": "-HYOebKkRi4v", + "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": "E1p7ebsTRi4w", + "colab_type": "code", + "colab": {} + }, + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "deletable": true, + "editable": true, + "id": "Am2xV_LoRi40", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Test your solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "deletable": true, + "editable": true, + "id": "F8wjkoz9Ri44", + "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": "OfXmTjeRRi45", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Test your solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "deletable": true, + "editable": true, + "id": "u9A0Y6uURi49", + "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": "Zp89M9B_Ri4_", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Write you solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "deletable": true, + "editable": true, + "id": "N8GCCq8KRi5F", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Test your solution here" + ], + "execution_count": 0, + "outputs": [] + } + ] +} \ No newline at end of file From 48e26ba94d4fe52a682b7fe42aea0276e1456b0f Mon Sep 17 00:00:00 2001 From: nicholassingleton352 <60523574+nicholassingleton352@users.noreply.github.com> Date: Fri, 27 Mar 2020 15:51:21 -0500 Subject: [PATCH 04/10] Add files via upload --- Labs/Lab-4/Copy_of_Lab_4.ipynb | 737 +++++++++++++++++++++++++++++++++ 1 file changed, 737 insertions(+) create mode 100644 Labs/Lab-4/Copy_of_Lab_4.ipynb diff --git a/Labs/Lab-4/Copy_of_Lab_4.ipynb b/Labs/Lab-4/Copy_of_Lab_4.ipynb new file mode 100644 index 0000000..e786cc6 --- /dev/null +++ b/Labs/Lab-4/Copy_of_Lab_4.ipynb @@ -0,0 +1,737 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.1" + }, + "colab": { + "name": "Copy of Lab-4.ipynb", + "provenance": [], + "collapsed_sections": [] + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "qUOGKd2xqsjD", + "colab_type": "text" + }, + "source": [ + "## Lab 4\n", + "\n", + "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github//afarbin/DATA1401-Spring-2020/blob/master/Labs/Lab-4/Lab-4.ipynb)\n", + "\n", + "In this lab we will become familiar with distributions, histograms, and functional programming. \n", + "\n", + "\n", + "### Uniform Distribution\n", + "Lets start with generating some fake random data. You can get a random number between 0 and 1 using the python random module as follow:" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "iKrs7HNSqsjL", + "colab_type": "code", + "colab": {} + }, + "source": [ + "def histogram(x):\n", + " x_min=x[0]\n", + " for elements in x:\n", + " if element < x_min:\n", + " x_min = element\n", + " return x_min\n", + " histogram([2,23,4,6,9])" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "jFsNdthQqsjf", + "colab_type": "text" + }, + "source": [ + "Everytime you call random, you will get a new number.\n", + "\n", + "*Exercise 1:* Using random, write a function `generate_uniform(N, mymin, mymax)`, that returns a python list containing N random numbers between specified minimum and maximum value. Note that you may want to quickly work out on paper how to turn numbers between 0 and 1 to between other values. " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "qKkFwbglqsjj", + "colab_type": "code", + "colab": {} + }, + "source": [ + "def generate_uniform(N,x_min,x_max):\n", + " outlist = []\n", + " import random\n", + " for j in range(N): \n", + " outlist.append(random.randint(x_min, x_max)) \n", + " return outlist" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "5enk-k2Rqsjs", + "colab_type": "code", + "outputId": "89a47c77-a8bb-4edb-aa12-c7dfff257c97", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 112 + } + }, + "source": [ + "data=generate_uniform(1000,-10,10)\n", + "print (\"Data Type:\", type(data))\n", + "print (\"Data Length:\", len(data))\n", + "if len(data)>0: \n", + " print (\"Type of Data Contents:\", type(data[0]))\n", + " print (\"Data Minimum:\", min(data))\n", + " print (\"Data Maximum:\", max(data))" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Data Type: \n", + "Data Length: 1000\n", + "Type of Data Contents: \n", + "Data Minimum: -10\n", + "Data Maximum: 10\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "QGMbz2Tcqsj4", + "colab_type": "text" + }, + "source": [ + "*Exercise 2a:* \n", + "Write a function that computes the mean of values in a list." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "9y4Tmba1qsj6", + "colab_type": "code", + "colab": {} + }, + "source": [ + "def def_mean (num):\n", + " m = 0\n", + " for t in num:\n", + " m = m + t \n", + "\n", + " avg = m / len(num)\n", + " return avg\n", + " " + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "UpP9oPFuqskG", + "colab_type": "code", + "outputId": "ac66a702-0adf-4090-a989-8fdbdabde766", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 36 + } + }, + "source": [ + "print(\"The average is\", def_mean([18,25,3,41,5])) \n", + " " + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "The average is 18.4\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "gLxPrA-YqskN", + "colab_type": "text" + }, + "source": [ + "*Exercise 2b:* \n", + "Write a function that computes the variance of values in a list." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "awZwZQ4ZqskP", + "colab_type": "code", + "outputId": "7cfe60ce-b899-403e-c9ed-31e6cdff3827", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "import statistics\n", + "grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]\n", + "statistics.pvariance(grades)\n", + "\n" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "334.07100591715977" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 1 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "_UrovYhHqski", + "colab_type": "code", + "colab": {} + }, + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "SGSKJQ3vqsks", + "colab_type": "text" + }, + "source": [ + "## Histogramming" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "J-vWqovfqsku", + "colab_type": "text" + }, + "source": [ + "*Exercise 3:* Write a function that bins the data so that you can create a histogram. An example of how to implement histogramming is the following logic:\n", + "\n", + "* User inputs a list of values `x` and optionally `n_bins` which defaults to 10.\n", + "* If not supplied, find the minimum and maximum (`x_min`,`x_max`) of the values in x.\n", + "* Determine the bin size (`bin_size`) by dividing the range of the function by the number of bins.\n", + "* Create an empty list of zeros of size `n_bins`, call it `hist`.\n", + "* Loop over the values in `x`\n", + " * Loop over the values in `hist` with index `i`:\n", + " * If x is between `x_min+i*bin_size` and `x_min+(i+1)*bin_size`, increment `hist[i].` \n", + " * For efficiency, try to use continue to goto the next bin and data point.\n", + "* Return `hist` and the list corresponding of the bin edges (i.e. of `x_min+i*bin_size`). " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "AJkVMqHZqskx", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Solution\n", + "def histogram(data, n_bins=10,x_min=None, x_max=None):\n", + " if x_min==None:\n", + " x_min=min(x)\n", + " if x_max==None:\n", + " x_max=max(x)\n", + "\n", + " bin_edges = arange(x_min,x_max,n_bins)\n", + " bin_edges.append(x_max)\n", + "\n", + " hist=[0]*n_bins\n", + "\n", + " for d in data:\n", + " for i in range(n_bins):\n", + " if d>=bin_edges[i] and d=mymin\n", + " return testrange\n", + "\n", + "# Examples:\n", + "F1=inrange(0,10)\n", + "F2=inrange(10,20)\n", + "\n", + "# Test of in_range\n", + "print (F1(0), F1(1), F1(10), F1(15), F1(20))\n", + "print (F2(0), F2(1), F2(10), F2(15), F2(20))\n", + "\n", + "print (\"Number of Entries passing F1:\", len(where(data,F1)))\n", + "print (\"Number of Entries passing F2:\", len(where(data,F2)))" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "1cTxCPjQqslz", + "colab_type": "code", + "colab": {} + }, + "source": [ + "def isOdd(x):\n", + "\n", + " if x/2 == int(x/2):\n", + "\n", + " return True\n", + "\n", + " else:\n", + "\n", + " return False\n", + "def c(a,b):\n", + " if a==b:\n", + " return 'They are the same!'\n", + " else:\n", + " return 'Integer A is greater than B.' if (abs(a-b)==a-b) else 'Integer B is greater than A.'\n", + "\n", + "def multiple(m, n):\n", + "\treturn True if m % n == 0 else False\n", + "\n" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "KSYgdAIAqsl8", + "colab_type": "code", + "outputId": "36374325-9f0f-4a8b-e9ee-bd4a435d93f2", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "multiple(5,20)" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "False" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 6 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "GhybH4TRqsmB", + "colab_type": "text" + }, + "source": [ + "*Exercise 7:* Repeat the previous exercise using `lambda` and the built-in python functions sum and map instead of your solution above. " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Vxfakv8bqsmD", + "colab_type": "code", + "colab": {} + }, + "source": [ + "N = 20\n", + "arr = []\n", + "for i in range(1, 20):\n", + " arr.append(i)\n", + "\n", + "arr2 = []\n", + "z = lambda x: x%2!=0\n", + "arr2 = filter(lambda x: x % 2 != 0, arr)\n", + "map(lambda x: x%2!=0,arr2)\n", + "print(list(arr2))\n", + "\n", + "N = 20\n", + "arr = []\n", + "for i in range(1, 20):\n", + " arr.append(i)\n", + "\n", + "arr2 = []\n", + "z = lambda x: x%2==0\n", + "arr2 = filter(lambda x: x % 2 == 0, arr)\n", + "map(lambda x: x%2==0,arr2)\n", + "print(list(arr2))\n", + "\n", + "f=lambda x,y: x>y\n", + "f(5,2)\n", + "\n", + "f=lambda x,y: x 0. \n", + "\n", + "Use the test function below and your histogramming functions above to demonstrate that your generator is working properly.\n", + "\n", + "Hint: A simple, but slow, solution is to a draw random number test_x within the specified range and another number p between the min and max of the function (which you will have to determine). If p<=function(test_x), then place test_x on the output. If not, repeat the process, drawing two new numbers. Repeat until you have the specified number of generated numbers, N. For this problem, it's OK to determine the min and max by numerically sampling the function. " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "HbY2P1rUqsmN", + "colab_type": "code", + "colab": {} + }, + "source": [ + "def generate_function(func,x_min,x_max,N=1000):\n", + " out = list()\n", + " ### BEGIN SOLUTION\n", + "\n", + " # Fill in your solution here \n", + " \n", + " ### END SOLUTION\n", + " \n", + " return out" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "XEVYLwtCqsmj", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# A test function\n", + "def test_func(x,a=1,b=1):\n", + " return abs(a*x+b)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "tJ90Xq8mqsmp", + "colab_type": "text" + }, + "source": [ + "*Exercise 8:* Use your function to generate 1000 numbers that are normal distributed, using the `gaussian` function below. Confirm the mean and variance of the data is close to the mean and variance you specify when building the Gaussian. Histogram the data. " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "d5qlrAguqsmr", + "colab_type": "code", + "colab": {} + }, + "source": [ + "import math\n", + "\n", + "def gaussian(mean, sigma):\n", + " def f(x):\n", + " return math.exp(-((x-mean)**2)/(2*sigma**2))/math.sqrt(math.pi*sigma)\n", + " return f\n", + "\n", + "# Example Instantiation\n", + "g1=gaussian(0,1)\n", + "g2=gaussian(10,3)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "MGDxt2_Hqsmv", + "colab_type": "text" + }, + "source": [ + "*Exercise 9:* Combine your `generate_function`, `where`, and `in_range` functions above to create an integrate function. Use your integrate function to show that approximately 68% of Normal distribution is within one variance." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "x1yY8tkIqsmx", + "colab_type": "code", + "colab": {} + }, + "source": [ + "def integrate(func, x_min, x_max, n_points=1000):\n", + " \n", + " return integral" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "D-UQS1KHqsm2", + "colab_type": "code", + "colab": {} + }, + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + } + ] +} \ No newline at end of file From aca30ea360e0f3446c0afaf0083bd423ac57ecca Mon Sep 17 00:00:00 2001 From: nicholassingleton352 <60523574+nicholassingleton352@users.noreply.github.com> Date: Fri, 3 Apr 2020 14:49:24 -0500 Subject: [PATCH 05/10] Add files via upload I performed exercise 1 on the lab, but I did not see a space where I can perform Exercise 2, 3, 4, 5, and 6 . So, I copy and pasted the code and solutions for 2, 3, 4, 5, and 6 on a separate document --- Labs/Lab-4/Copy_of_Exam.ipynb | 269 +++++++++++++++++++++++++++ Labs/Lab-4/Data Science Midterm.docx | Bin 0 -> 15110 bytes 2 files changed, 269 insertions(+) create mode 100644 Labs/Lab-4/Copy_of_Exam.ipynb create mode 100644 Labs/Lab-4/Data Science Midterm.docx diff --git a/Labs/Lab-4/Copy_of_Exam.ipynb b/Labs/Lab-4/Copy_of_Exam.ipynb new file mode 100644 index 0000000..e52b8c7 --- /dev/null +++ b/Labs/Lab-4/Copy_of_Exam.ipynb @@ -0,0 +1,269 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.1" + }, + "colab": { + "name": "Copy of Exam.ipynb", + "provenance": [] + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "zShtCZTtkgHN", + "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": "aw8J18IwkgHQ", + "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": "kEBRdsuFkgHU", + "colab_type": "code", + "colab": {} + }, + "source": [ + "def first_alphabetically(lst):\n", + " 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']\n", + " states.sort()\n", + " return states[0]\n", + " " + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "xylKXe6VkgHu", + "colab_type": "text" + }, + "source": [ + "should return the string `\"Alabama\"`. Note that you can compare strings:" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "sBHzr4gWkgHw", + "colab_type": "code", + "outputId": "80dbfb54-d7eb-480a-a965-82d918cdbce4", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "source": [ + "first_alphabetically(\"lst\")" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "'Alabama'" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 31 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "n4dETTaUkgH6", + "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": "markdown", + "metadata": { + "id": "VA91-PWEkgH8", + "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": "markdown", + "metadata": { + "id": "MF_MwcczkgH-", + "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": "markdown", + "metadata": { + "id": "IX3Y4-fukgIA", + "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": "markdown", + "metadata": { + "id": "cQ3IU4HwkgIC", + "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": "markdown", + "metadata": { + "id": "7590pjz1kgIE", + "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": "FBFYjSt3kgIF", + "colab_type": "text" + }, + "source": [ + "$$\n", + "\\int_{-\\infty}^{x_{90}} N(x;\\mu=0,\\sigma=1) dx = 0.9\n", + "$$" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "aI1Lc3BHkgIG", + "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 dVB(NuU&6fSFKfYk|3Zc0B`^#006)TtmjWzX#fELcwhhkG5``-Q_$AN$=Jq8SIN!J z*ioD2v$YjoE+{ZrHURkj{QqtLhex0;e%Pjm4ng=X;SoNjQQ6=ix0C`nj3<#=;Sd7d z16buHcA)LG0~uIe87KzYih!8$ZjDi;-*0j$)d~u+-j(LGfwT}@7 z$!0v=8vhTLFIG%#?HE!lKy-xly|JT;(6Z@y>3*nc_kc)>geVm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tC0y%5zllTKWhR0KyqkUo06!j^~oxz72CY9Y??0`y2 z#naWGwr`okY1N!_xXJ?ZxKG1g9ox%$3FnocIv@0-PUg#^SRk&GQAw#S7aVY}rEXMT zH#^@Wkbfj?fq<#s^K1XTVDMK=_}A^fln=^D{$0V}%bWfRj(ZP_{!;ApJMiyyAAbeb zzAtU~|5btfuIcw$fIoE|zE?&5M`gh8@ZVQc{0ZMf`zQSOwH3ds_z#ziaq=3h_@o0B}PL0Q{a%{2l%G ze9K?a;N*Wn|110QJN)-Z`A-F5l>ZvNf5**ol3?%V@vD>$2GISU-=Ssu_3Qru0@hc2 literal 0 HcmV?d00001 From a0d32f033af18701c2ded59ecacb60053818951c Mon Sep 17 00:00:00 2001 From: nicholassingleton352 <60523574+nicholassingleton352@users.noreply.github.com> Date: Wed, 15 Apr 2020 15:21:55 -0500 Subject: [PATCH 06/10] Add files via upload --- Exams/Mid-term/Copy_of_Exam(1).ipynb | 460 +++++++++++++++++++++++++++ 1 file changed, 460 insertions(+) create mode 100644 Exams/Mid-term/Copy_of_Exam(1).ipynb diff --git a/Exams/Mid-term/Copy_of_Exam(1).ipynb b/Exams/Mid-term/Copy_of_Exam(1).ipynb new file mode 100644 index 0000000..f87728f --- /dev/null +++ b/Exams/Mid-term/Copy_of_Exam(1).ipynb @@ -0,0 +1,460 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.1" + }, + "colab": { + "name": "Copy of Exam.ipynb", + "provenance": [] + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "zShtCZTtkgHN", + "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": "aw8J18IwkgHQ", + "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": "kEBRdsuFkgHU", + "colab_type": "code", + "colab": {} + }, + "source": [ + "def first_alphabetically(lst):\n", + " 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']\n", + " states.sort()\n", + " return states[0]\n", + " " + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "xylKXe6VkgHu", + "colab_type": "text" + }, + "source": [ + "should return the string `\"Alabama\"`. Note that you can compare strings:" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "sBHzr4gWkgHw", + "colab_type": "code", + "outputId": "80dbfb54-d7eb-480a-a965-82d918cdbce4", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "source": [ + "first_alphabetically(\"lst\")" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "'Alabama'" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 31 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "n4dETTaUkgH6", + "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": "markdown", + "metadata": { + "id": "VA91-PWEkgH8", + "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": "ZfqV_gtKdVwK", + "colab_type": "code", + "colab": {} + }, + "source": [ + "def arg_first_alphabetically(lst):\n", + " 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']\n", + " \n", + " index = states.index('Alabama')\n", + " print('The index of Alabama:', index)\n" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "2XG3PCOKdWw9", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "388346e1-fca6-4ed0-83da-14e14a2cf134" + }, + "source": [ + "Input: arg_first_alphabetically('lst')\n" + ], + "execution_count": 2, + "outputs": [ + { + "output_type": "stream", + "text": [ + "The index of Alabama: 23\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "MF_MwcczkgH-", + "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": "WyjtK7RMdynM", + "colab_type": "code", + "colab": {} + }, + "source": [ + "def arg_sort_alphabetically(lst):\n", + " 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']\n", + " states.sort()\n", + " print(states)\n" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "YWTOOEGvdy_T", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 55 + }, + "outputId": "a99ec18b-8f1f-45a9-ae12-43119ff8ea20" + }, + "source": [ + "arg_sort_alphabetically(\"lst\")\n" + ], + "execution_count": 4, + "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": "IX3Y4-fukgIA", + "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": "PbszfQPueQuZ", + "colab_type": "code", + "colab": {} + }, + "source": [ + "def outer_product(a,b):\n", + " zip_b = zip(*b)\n", + " zip_b = list(zip_b)\n", + " return [[sum(ele_a*ele_b for ele_a, ele_b in zip(row_a, col_b)) \n", + " for col_b in zip_b] for row_a in a]\n", + " \n", + "x = [[1,2,3],[4,5,6],[7,8,9],[10,11,12]]\n", + "y = [[1,2],[1,2],[3,4]]\n" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "EHYS-9sleV1y", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "03ee6c3f-d974-43c4-b908-46936e914a92" + }, + "source": [ + "outer_product(x,y)" + ], + "execution_count": 7, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "[[12, 18], [27, 42], [42, 66], [57, 90]]" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 7 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "cQ3IU4HwkgIC", + "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": "ckRXE6JbfE8b", + "colab_type": "code", + "colab": {} + }, + "source": [ + "def cumulative_sum(nums_list):\n", + " return [sum(nums_list[:i+1]) for i in range(len(nums_list))]\n" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "bmHftjKKfFLR", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "7c1fb025-2580-446c-e65a-c59cb1cf8b96" + }, + "source": [ + "cumulative_sum([1,2,3])" + ], + "execution_count": 9, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "[1, 3, 6]" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 9 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "7590pjz1kgIE", + "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": "FBFYjSt3kgIF", + "colab_type": "text" + }, + "source": [ + "$$\n", + "\\int_{-\\infty}^{x_{90}} N(x;\\mu=0,\\sigma=1) dx = 0.9\n", + "$$" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "aI1Lc3BHkgIG", + "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 15:21:13 -0500 Subject: [PATCH 07/10] Add files via upload --- Labs/Lab-5/Copy_of_Lab_5.ipynb | 564 +++++++++++++++++++++++++++++++++ 1 file changed, 564 insertions(+) create mode 100644 Labs/Lab-5/Copy_of_Lab_5.ipynb diff --git a/Labs/Lab-5/Copy_of_Lab_5.ipynb b/Labs/Lab-5/Copy_of_Lab_5.ipynb new file mode 100644 index 0000000..4b06f46 --- /dev/null +++ b/Labs/Lab-5/Copy_of_Lab_5.ipynb @@ -0,0 +1,564 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.1" + }, + "colab": { + "name": "Copy of Lab-5.ipynb", + "provenance": [] + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "h3B9k_Hsa5ZA", + "colab_type": "text" + }, + "source": [ + "# Lab 5- Object Oriented Programming\n", + "\n", + "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github//afarbin/DATA1401-Spring-2020/blob/master/Labs/Lab-5/Lab-5.ipynb)\n", + "\n", + "For all of the exercises below, make sure you provide tests of your solutions.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "IcezS6XLa5ZC", + "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": "C4vQdAara9-d", + "colab_type": "code", + "colab": {} + }, + "source": [ + "class counter:\n", + " def __init__(self,max_val):\n", + " self.max_val=max_val\n", + " self.cur_val=0\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=0\n", + " \n", + " " + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "s_8LD2UMcTaO", + "colab_type": "code", + "colab": {} + }, + "source": [ + "my_counter=counter(3)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "86_FKezgcZbS", + "colab_type": "code", + "outputId": "66f83fab-e18c-4399-9259-54f4f57dce8f", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 91 + } + }, + "source": [ + "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", + "Max value reached.\n", + "Max value reached.\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "5DfML5Cfa5ZH", + "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": "myABh7PZcRsP", + "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" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "W1GllOYmgSJb", + "colab_type": "code", + "colab": {} + }, + "source": [ + "my_counter=counter(3)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "3dSBVbYbgYOk", + "colab_type": "code", + "outputId": "7e0e801d-b1c3-449f-c876-b156c00e3132", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "source": [ + "my_counter.cur_val()" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "1" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 35 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "mlOeecmZa5ZK", + "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": "BYtc498AhMS-", + "colab_type": "code", + "colab": {} + }, + "source": [ + "class rectangle:\n", + " def __init__(self,width,length,x,y):\n", + " \n", + " x=__width=5\n", + " y=__length=3\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": "lvNO0gxJsAqX", + "colab_type": "code", + "colab": {} + }, + "source": [ + "r1=rectangle(5,3,5,3)\n" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "HdPkjZNxsBep", + "colab_type": "code", + "outputId": "88d1413c-400c-4198-e7ff-8d601c274497", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "source": [ + "r1.area()\n" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "15" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 39 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "-dpu7lanc_-X", + "colab_type": "code", + "outputId": "4bb51ecb-e4bb-40a6-cfbe-c1771238c085", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "source": [ + "r1.perimeter()" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "16" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 41 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "gQWirf2ua5ZM", + "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": "67tcD1NGoiv3", + "colab_type": "code", + "colab": {} + }, + "source": [ + "class circle:\n", + " def __init__(self,radius,x,y):\n", + " self.__radius=radius\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" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "rkJuEdjia5ZN", + "colab_type": "text" + }, + "source": [ + "5. Implement a common base class for the classes implemented in 3 and 4 above which implements all common methods as dummy functions. Re-implement those classes to inherit from the base class and overload the functions accordingly. " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "qsvkfJWia5ZO", + "colab_type": "text" + }, + "source": [ + "6. Implement an analogous triangle class." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "j-cnVtHIiWTe", + "colab_type": "code", + "colab": {} + }, + "source": [ + "class triangle:\n", + " def __init__(self,base,height,x,y):\n", + " \n", + " x=__base=8\n", + " y=__height=4\n", + " self.__base=base\n", + " self.__height=height\n", + " self.__x=x\n", + " self.__y=y\n", + "\n", + " def area(self):\n", + " return 1/2*(self.__base*self.__height)\n", + "\n", + " def x(self):\n", + " return self.__x\n", + "\n", + " def y(self):\n", + " return self.__y\n", + "\n", + " \n" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "vfCGilVAqx_8", + "colab_type": "code", + "colab": {} + }, + "source": [ + "t1=triangle(8,4,8,4)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "t0oRbxa6qySv", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "aa4e9d83-6c98-4e03-faab-b60381dd5fe9" + }, + "source": [ + "t1.area()" + ], + "execution_count": 6, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "16.0" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 6 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "AaOTV9Pna5ZT", + "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": "Mp8M2YdArTtC", + "colab_type": "code", + "colab": {} + }, + "source": [ + "class triangle:\n", + " def __init__(self,x1,y1,x2,y2,x,y):\n", + " # top and bottom-right \n", + " # corners of triangle. \n", + " self.__x1=x1\n", + " self.__y1=y1\n", + " self.__x2=x2\n", + " self.__y2=y2\n", + " self.__x=x\n", + " self.__y=y\n", + "\n", + " def locatepoint(self):\n", + " if (self.__x > self.__x1 and self.__x < self.__x2 and \n", + " self.__y > self.__y1 and self.__y < self.__y2) : \n", + " return True\n", + " else : \n", + " return False\n", + " \n" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "qNrA3N3b5yKE", + "colab_type": "code", + "colab": {} + }, + "source": [ + "t1=triangle(0,0,10,8,1,5)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "trJ4pc7M50JL", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "a0129b6c-f6e0-4df8-d3c3-7f8f38f29369" + }, + "source": [ + "t1.locatepoint()" + ], + "execution_count": 15, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "True" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 15 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Fpe0Xz27a5ZV", + "colab_type": "text" + }, + "source": [ + "8. Add a function to the object classes that return a list of up to 16 pairs of $x$ and $y$ points on the parameter of the object.\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "YFQg9YrTa5ZW", + "colab_type": "text" + }, + "source": [ + "9. Add a function in the base class of the object classes that returns true/false testing that the object overlaps with another object." + ] + } + ] +} \ No newline at end of file From c4a59cbe1e61cd510993f78d59d5b43bfe557649 Mon Sep 17 00:00:00 2001 From: nicholassingleton352 <60523574+nicholassingleton352@users.noreply.github.com> Date: Fri, 24 Apr 2020 15:09:17 -0500 Subject: [PATCH 08/10] Add files via upload --- Labs/Lab-6/Copy_of_Lab_6.ipynb | 482 +++++++++++++++++++++++++++++++++ 1 file changed, 482 insertions(+) create mode 100644 Labs/Lab-6/Copy_of_Lab_6.ipynb diff --git a/Labs/Lab-6/Copy_of_Lab_6.ipynb b/Labs/Lab-6/Copy_of_Lab_6.ipynb new file mode 100644 index 0000000..0bcd6fe --- /dev/null +++ b/Labs/Lab-6/Copy_of_Lab_6.ipynb @@ -0,0 +1,482 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.1" + }, + "colab": { + "name": "Copy of Lab-6.ipynb", + "provenance": [] + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "zMYAcyi-oz82", + "colab_type": "text" + }, + "source": [ + "# Lab 6\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "PkGvGYWDo6Pq", + "colab_type": "code", + "colab": {} + }, + "source": [ + "class Matrix():\n", + "\n", + " def __init__(self, height, width, m, n):\n", + " m=height=3\n", + " n=width=5\n", + " self.rows = [[0]*width for i in range(height)]\n", + " self.height = height\n", + " self.width = width\n", + " \n", + "\n", + " def __str__(self):\n", + " s =\"\\n\" + \"\\n\".join([str(i) for i in [rows for rows in self.rows] ])\n", + " return s\n", + " \n", + " \n", + " def dimensions(self):\n", + " tup1=(3,5)\n", + " return tup1\n", + " \n", + " def transpose(self):\n", + " import numpy as np\n", + " arr1 = np.array([[1, 2, 3], [4, 5, 6]])\n", + "\n", + " (f'Original Array:\\n{arr1}')\n", + "\n", + " arr1_transpose = arr1.transpose()\n", + "\n", + " return(f'Transposed Array:\\n{arr1_transpose}')\n", + " \n", + " def row(self):\n", + " import numpy as np\n", + " nArr2D = np.array(([21, 22, 23], [11, 22, 33], [43, 77, 89]))\n", + " row = nArr2D[1]\n", + " \n", + " return('Contents of Row at Index 1 : ' , row)\n", + "\n", + " def column(self):\n", + " import numpy as np\n", + " nArr2D = np.array(([21, 22, 23], [11, 22, 33], [43, 77, 89]))\n", + " column = nArr2D[:, 1]\n", + " \n", + " return('Contents of Column at Index 1 : ', column)\n", + "\n", + " def constants(self):\n", + " import numpy as np\n", + " return np.full((2, 2), 10)\n", + "\n", + " def zeroes(self):\n", + " import numpy as np\n", + " return np.full((3,3), 0)\n", + "\n", + " def ones(self):\n", + " import numpy as np\n", + " return np.full((4,4),1)\n" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "kViSjOyTpTCd", + "colab_type": "code", + "colab": {} + }, + "source": [ + "m1=Matrix(3,5,3,5)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "mOOy0Kzgqb2C", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "8ba2e116-9788-4c83-8a66-0e4d21d1db01" + }, + "source": [ + "m1.__str__()" + ], + "execution_count": 126, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "'\\n[0, 0, 0, 0, 0]\\n[0, 0, 0, 0, 0]\\n[0, 0, 0, 0, 0]'" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 126 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "hq15D26Gqhfw", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "5bfd0595-6ba3-4079-d501-a1208bebfbc0" + }, + "source": [ + "m1.dimensions()" + ], + "execution_count": 127, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "(3, 5)" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 127 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "bXqNVKhfqhr0", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "933206bb-941b-480b-e20b-9b27177d28b4" + }, + "source": [ + "m1.transpose()" + ], + "execution_count": 128, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "'Transposed Array:\\n[[1 4]\\n [2 5]\\n [3 6]]'" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 128 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "PPHS7Byfqh1Z", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "39e48717-d65b-4333-d3c9-a58bb717113e" + }, + "source": [ + "m1.row()" + ], + "execution_count": 129, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "('Contents of Row at Index 1 : ', array([11, 22, 33]))" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 129 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "JvUDzMwcqh9b", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "924f8da1-b0ac-499f-cc69-31656a0a1c43" + }, + "source": [ + "m1.column()" + ], + "execution_count": 130, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "('Contents of Column at Index 1 : ', array([22, 22, 77]))" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 130 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "TQ8MZl02spj2", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 54 + }, + "outputId": "5fceb5f5-0872-48b5-eb79-0db9fae68803" + }, + "source": [ + "m1.constants()" + ], + "execution_count": 102, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([[10, 10],\n", + " [10, 10]])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 102 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "MYu02RfTspu1", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 72 + }, + "outputId": "cc3946e8-ddf7-49c4-b66a-277150e9eb82" + }, + "source": [ + "m1.zeroes()" + ], + "execution_count": 121, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([[0, 0, 0],\n", + " [0, 0, 0],\n", + " [0, 0, 0]])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 121 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "qRGYUKR5xwEX", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 91 + }, + "outputId": "c8e8eb40-971b-4d32-890c-bfee60356c29" + }, + "source": [ + "m1.ones()" + ], + "execution_count": 131, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([[1, 1, 1, 1],\n", + " [1, 1, 1, 1],\n", + " [1, 1, 1, 1],\n", + " [1, 1, 1, 1]])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 131 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "O_smZYVQoz85", + "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": "markdown", + "metadata": { + "id": "C7k01DgZoz88", + "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": "markdown", + "metadata": { + "id": "WT-VfimZoz9C", + "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": "markdown", + "metadata": { + "id": "4CD7bSrPoz9G", + "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": "ddNtEH66oz9I", + "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": "RjdauGJ2oz9M", + "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": "QwnvsnIYoz9c", + "colab_type": "code", + "colab": {} + }, + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + } + ] +} \ No newline at end of file From 089abddae681608cf939a0dfca5b0caf0a10cac9 Mon Sep 17 00:00:00 2001 From: nicholassingleton352 <60523574+nicholassingleton352@users.noreply.github.com> Date: Fri, 8 May 2020 15:08:50 -0500 Subject: [PATCH 09/10] Add files via upload --- Labs/Lab-7/Copy_of_Lab_7.ipynb | 696 +++++++++++++++++++++++++++++++++ 1 file changed, 696 insertions(+) create mode 100644 Labs/Lab-7/Copy_of_Lab_7.ipynb diff --git a/Labs/Lab-7/Copy_of_Lab_7.ipynb b/Labs/Lab-7/Copy_of_Lab_7.ipynb new file mode 100644 index 0000000..30f2e4b --- /dev/null +++ b/Labs/Lab-7/Copy_of_Lab_7.ipynb @@ -0,0 +1,696 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.1" + }, + "colab": { + "name": "Copy of Lab-7.ipynb", + "provenance": [] + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "oVhPeotGx1lk", + "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": "2UuWuwnFx1lp", + "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": "5iF9OA0xx1l5", + "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": "H2_Mkpiox1mR", + "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": "7zvt1BZxx1mZ", + "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": "aX7dsS47x1mf", + "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": "RznGbm_Zx1mn", + "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": "wJDJ5-f7x1mt", + "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": "8PmiPFTux1mu", + "colab_type": "code", + "outputId": "78fd33f7-472f-4c79-f245-13caabf3e0ac", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "source": [ + "!cat Data1401-Grades.csv " + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "cat: Data1401-Grades.csv: No such file or directory\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "v3r2fUwPx1m0", + "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": "RDsXiDTPx1m1", + "colab_type": "code", + "outputId": "a3d13d50-ee14-4d67-b595-960a8a9503a5", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 55 + } + }, + "source": [ + "# Your solution here.\n", + "\n", + "def cvs_reader(filename):\n", + " line = 'student,Bill,grade,94,lab,1,question,1'\n", + " 'student,Fred,grade,94,lab,1,question,2'\n", + " 'student,Karen,grade,78,lab,1,question,4'\n", + " 'student,Kylie,grade,96,lab,3,question,7'\n", + " 'student,George,grade,35,lab,5,question,10'\n", + " 'student,Jorge,grade,87,lab,2,question,9'\n", + " 'student,Nate,grade,94,lab,4,question,5'\n", + " 'student,Matthew,grade,99,lab,6,question,6'\n", + " line.split(',')\n", + "[ 'student' , 'Bill', 'grade', '94', 'lab', '1', 'question', '1'\n", + " 'student', 'Fred', 'grade', '94', 'lab', '1', 'question', '2'\n", + " 'student', 'Karen', 'grade', '78', 'lab', '1', 'question', '4'\n", + " 'student', 'Kylie', 'grade', '96', 'lab' '3', 'question', '7'\n", + " 'student', 'George', 'grade', '35' , 'lab' '5', 'question', '10'\n", + " 'student', 'Jorge', 'grade', '87', 'lab', '2', 'question', '9'\n", + " 'student', 'Nate', 'grade', '94', 'lab', '4', 'question', '5'\n", + " 'student', 'Matthew', 'grade', '99', 'lab', '6', 'question', '6']\n", + " \n", + "\n", + "data = ['student' , 'Bill', 'grade', '94', 'lab', '1', 'question', '1'\n", + " 'student', 'Fred', 'grade', '94', 'lab', '1', 'question', '2'\n", + " 'student', 'Karen', 'grade', '78', 'lab', '1', 'question', '4'\n", + " 'student', 'Kylie', 'grade', '96', 'lab' '3', 'question', '7'\n", + " 'student', 'George', 'grade', '35' , 'lab' '5', 'question', '10'\n", + " 'student', 'Jorge', 'grade', '87', 'lab', '2', 'question', '9'\n", + " 'student', 'Nate', 'grade', '94', 'lab', '4', 'question', '5'\n", + " 'student', 'Matthew', 'grade', '99', 'lab', '6', 'question', '6']\n", + "\n", + "','.join(data)\n", + "\n", + " \n", + " " + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "'student,Bill,grade,94,lab,1,question,1student,Fred,grade,94,lab,1,question,2student,Karen,grade,78,lab,1,question,4student,Kylie,grade,96,lab3,question,7student,George,grade,35,lab5,question,10student,Jorge,grade,87,lab,2,question,9student,Nate,grade,94,lab,4,question,5student,Matthew,grade,99,lab,6,question,6'" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 49 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "UoFacPv0x1m9", + "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": "QyRUtQZBx1m-", + "colab_type": "code", + "colab": {} + }, + "source": [ + "import pandas as pd\n", + "class_data=pd.read_csv(\"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": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "j_jotawVx1nH", + "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": "WhRV3CTbx1nJ", + "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": "7kSOanpkx1nQ", + "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": "yftncEi6x1nS", + "colab_type": "code", + "colab": {} + }, + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "DIARt0hCx1nY", + "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": "d61x8sEmx1na", + "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": "wr7nz-fKx1nf", + "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": "MpTlRPLUx1nh", + "colab_type": "code", + "colab": {} + }, + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "swli9gQIx1nn", + "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": "e9RQRUKXx1np", + "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": "vVuBANPtx1nt", + "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": "Od6lLCaCx1nu", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Your solution here" + ], + "execution_count": 0, + "outputs": [] + } + ] +} \ No newline at end of file From 84f482826206aa3acfaf32967597ee7021d6a90e Mon Sep 17 00:00:00 2001 From: nicholassingleton352 <60523574+nicholassingleton352@users.noreply.github.com> Date: Mon, 11 May 2020 16:37:22 -0500 Subject: [PATCH 10/10] Add files via upload --- Exams/Final/Copy_of_Final(1).ipynb | 724 +++++++++++++++++++++++++++++ 1 file changed, 724 insertions(+) create mode 100644 Exams/Final/Copy_of_Final(1).ipynb diff --git a/Exams/Final/Copy_of_Final(1).ipynb b/Exams/Final/Copy_of_Final(1).ipynb new file mode 100644 index 0000000..c351a55 --- /dev/null +++ b/Exams/Final/Copy_of_Final(1).ipynb @@ -0,0 +1,724 @@ +{ + "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 Final.ipynb", + "provenance": [], + "collapsed_sections": [] + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "cqdi2ZhGqreU", + "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": "markdown", + "metadata": { + "id": "AawewtE4qreW", + "colab_type": "text" + }, + "source": [ + "Recall the drawing system from lecture 18:" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "wVyihGFsqreY", + "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 h_line(self, x, y, w, **kargs):\n", + " for i in range(x,x+w):\n", + " self.set_pixel(i,y, **kargs)\n", + "\n", + " def v_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": "V7yi8gyfqreu", + "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": "o9hC1lgCqre7", + "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": "u2-f-vGzqrfC", + "colab_type": "text" + }, + "source": [ + "1. Add `Point` and `Triangle` classes and test them." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "HNeUtsPcqrfE", + "colab_type": "code", + "outputId": "0e473821-6f13-4bab-ca55-5f4da3e7cf21", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "source": [ + "class Point:\n", + " \"\"\" Point class represents and manipulates x,y coords. \"\"\"\n", + "\n", + " def __init__(self):\n", + " \"\"\" Create a new point at the origin \"\"\"\n", + " self.x = 0\n", + " self.y = 0\n", + "\n", + "p = Point() # Instantiate an object of type Point\n", + "q = Point() # Make a second point\n", + "\n", + "print(p.x, p.y, q.x, q.y) # Each point object has its own x and y" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "0 0 0 0\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "vsUFtWjCstBp", + "colab_type": "code", + "colab": {} + }, + "source": [ + "class triangle:\n", + " def __init__(self,base,height,x,y):\n", + " \n", + " x=__base=8\n", + " y=__height=4\n", + " self.__base=base\n", + " self.__height=height\n", + " self.__x=x\n", + " self.__y=y" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "LdYlpCKLsrvR", + "colab_type": "text" + }, + "source": [ + "" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "jsn8lRksqrfL", + "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": "xcF0uXSBqrfN", + "colab_type": "code", + "colab": {} + }, + "source": [ + "import math \n", + " \n", + "# function to print circle pattern \n", + "def printPattern(radius): \n", + " \n", + " # dist represents distance to the center \n", + " # for horizontal movement \n", + " for i in range((2 * radius)+1): \n", + " \n", + " # for vertical movement \n", + " for j in range((2 * radius)+1): \n", + " \n", + " dist = math.sqrt((i - radius) * (i - radius) + \n", + " (j - radius) * (j - radius)) \n", + " \n", + " # dist should be in the \n", + " # range (radius - 0.5) \n", + " # and (radius + 0.5) to print stars(*) \n", + " if (dist > radius - 0.5 and dist < radius + 0.5): \n", + " print(\"*\",end=\"\") \n", + " else: \n", + " print(\" \",end=\"\") \n", + " \n", + " \n", + " print() \n", + " " + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "MXIrwVPCzp-l", + "colab_type": "code", + "outputId": "de5b5033-1333-432c-e25a-12d729d6588c", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 257 + } + }, + "source": [ + "radius = 6\n", + "printPattern(radius) " + ], + "execution_count": 33, + "outputs": [ + { + "output_type": "stream", + "text": [ + " ***** \n", + " ** ** \n", + " ** ** \n", + " * * \n", + "* *\n", + "* *\n", + "* *\n", + "* *\n", + "* *\n", + " * * \n", + " ** ** \n", + " ** ** \n", + " ***** \n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "dQy1VDPolAR2", + "colab_type": "code", + "colab": {} + }, + "source": [ + "import math \n", + " \n", + "# function to print oval pattern \n", + "def printPattern(radius): \n", + " \n", + " # dist represents distance to the center \n", + " # for horizontal movement \n", + " for i in range((2 * radius)+1): \n", + " \n", + " # for vertical movement \n", + " for j in range((2 * radius)+1): \n", + " \n", + " dist = math.sqrt((i - radius) * (i - radius) + \n", + " (j - radius) * (j - radius)) \n", + " \n", + " # dist should be in the \n", + " # range (radius - 0.5) \n", + " # and (radius + 0.5) to print stars(*) \n", + " if (dist > radius - 0.3 and dist < radius + 0.3): \n", + " print(\"*\",end=\"\") \n", + " else: \n", + " print(\" \",end=\"\") \n", + " \n", + " \n", + " print() \n", + " " + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "ebMvvFcal4Kl", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 109 + }, + "outputId": "d7e99d52-1ba1-4070-8ba5-c02ccbbb2522" + }, + "source": [ + "radius = 2\n", + "printPattern(radius) " + ], + "execution_count": 66, + "outputs": [ + { + "output_type": "stream", + "text": [ + " *** \n", + "* *\n", + "* *\n", + "* *\n", + " *** \n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "VpipnAL8qrfZ", + "colab_type": "text" + }, + "source": [ + "3. Use your classes to create a `RasterDrawing` that draws a happy face." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "IOdMo032qrfb", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 395 + }, + "outputId": "8db61fc2-6617-4dbe-de49-37aaef83023a" + }, + "source": [ + "import arcade\n", + "\n", + "# Set constants for the screen size\n", + "SCREEN_WIDTH = 600\n", + "SCREEN_HEIGHT = 600\n", + "SCREEN_TITLE = \"Happy Face Example\"\n", + "\n", + "# Open the window. Set the window title and dimensions\n", + "arcade.open_window(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)\n", + "\n", + "# Set the background color\n", + "arcade.set_background_color(arcade.color.WHITE)\n", + "\n", + "# Clear screen and start render process\n", + "arcade.start_render()\n", + "\n", + "# --- Drawing Commands Will Go Here ---\n", + "\n", + "# Draw the face\n", + "x = 300\n", + "y = 300\n", + "radius = 200\n", + "arcade.draw_circle_filled(x, y, radius, arcade.color.YELLOW)\n", + "\n", + "# Draw the right eye\n", + "x = 370\n", + "y = 350\n", + "radius = 20\n", + "arcade.draw_circle_filled(x, y, radius, arcade.color.BLACK)\n", + "\n", + "# Draw the left eye\n", + "x = 230\n", + "y = 350\n", + "radius = 20\n", + "arcade.draw_circle_filled(x, y, radius, arcade.color.BLACK)\n", + "\n", + "# Draw the smile\n", + "x = 300\n", + "y = 280\n", + "width = 120\n", + "height = 100\n", + "start_angle = 190\n", + "end_angle = 350\n", + "arcade.draw_arc_outline(x, y, width, height, arcade.color.BLACK,\n", + " start_angle, end_angle, 10)\n", + "\n", + "# Finish drawing and display the result\n", + "arcade.finish_render()\n", + "\n", + "# Keep the window open until the user hits the 'close' button\n", + "arcade.run()" + ], + "execution_count": 54, + "outputs": [ + { + "output_type": "error", + "ename": "ModuleNotFoundError", + "evalue": "ignored", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mModuleNotFoundError\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[0;32mimport\u001b[0m \u001b[0marcade\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;31m# Set constants for the screen size\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[0mSCREEN_WIDTH\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m600\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mSCREEN_HEIGHT\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m600\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'arcade'", + "", + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0;32m\nNOTE: If your import is failing due to a missing package, you can\nmanually install dependencies using either !pip or !apt.\n\nTo view examples of installing some common dependencies, click the\n\"Open Examples\" button below.\n\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n" + ] + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "19F8i7YvXxFr", + "colab_type": "code", + "outputId": "4fb043e8-4d60-4612-df9b-8a1706c88e35", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "source": [ + "class Circle():\n", + " def __init__(self, x0, y0, R):\n", + " self.x0, self.y0, self.R = x0, y0, R\n", + "\n", + " def area(self):\n", + " return pi*self.R**2\n", + "\n", + " def circumference(self):\n", + " return 2*pi*self.R\n", + "\n", + " def __str__(self):\n", + " return 'Circle(6,6,4,char=\"^\")' \n", + "\n", + "\n", + "obj = Circle('6','6','4')\n", + "print(obj) " + ], + "execution_count": 15, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Circle(6,6,4,char=\"^\")\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "C-rmUrmloOrj", + "colab_type": "code", + "colab": {} + }, + "source": [ + "\n" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "qqjTf4UNqrfh", + "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": "oAfhRnmUqrfi", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "027ced39-e7ce-423e-d2ec-2f0874291488" + }, + "source": [ + "class Circle():\n", + " def __init__(self, x0, y0, R):\n", + " self.x0, self.y0, self.R = x0, y0, R\n", + "\n", + " def area(self):\n", + " return pi*self.R**2\n", + "\n", + " def circumference(self):\n", + " return 2*pi*self.R\n", + "\n", + " def __str__(self):\n", + " return 'Circle(6,6,4,char=\"^\")' \n", + "\n", + "\n", + "obj = Circle('6','6','4')\n", + "print(obj) " + ], + "execution_count": 36, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Circle(6,6,4,char=\"^\")\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "NeB-sU7ePNcJ", + "colab_type": "code", + "outputId": "8fcd9843-ecb9-40f5-fe22-700067d234c0", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "source": [ + "class Oval():\n", + " def __init__(self, x0, y0, R):\n", + " self.x0, self.y0, self.R = x0, y0, R\n", + "\n", + " def area(self):\n", + " return pi*self.R**2\n", + "\n", + " def circumference(self):\n", + " return 2*pi*self.R\n", + "\n", + " def __str__(self):\n", + " return 'Oval(3,3,2,char=\"^\")' \n", + "\n", + "obj = Oval('3','3','2')\n", + "print(obj) " + ], + "execution_count": 37, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Oval(3,3,2,char=\"^\")\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Sh2TynSpqrfo", + "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": "IP-SI6iXqrfr", + "colab_type": "code", + "outputId": "261c2abd-0022-4a5f-f9b0-c19bb0fc4367", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "source": [ + "eval(\"print('Hello')\")" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Hello\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "opbIo69Xqrfw", + "colab_type": "code", + "outputId": "1d81ce15-66f9-40cb-a468-2a1853122b0f", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "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": "I7O3GTdBqrf4", + "colab_type": "code", + "colab": {} + }, + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + } + ] +} \ No newline at end of file