From 573184ea3371ee6b1f970a849755864e48572895 Mon Sep 17 00:00:00 2001 From: matthewbruce90 <70781709+matthewbruce90@users.noreply.github.com> Date: Fri, 11 Sep 2020 14:18:27 -0500 Subject: [PATCH 01/10] Create Lab1_solutions --- Lab1_solutions | 1 + 1 file changed, 1 insertion(+) create mode 100644 Lab1_solutions diff --git a/Lab1_solutions b/Lab1_solutions new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Lab1_solutions @@ -0,0 +1 @@ + From d849ce5434a85e7f55f758ac456b7362a5789ed2 Mon Sep 17 00:00:00 2001 From: matthewbruce90 <70781709+matthewbruce90@users.noreply.github.com> Date: Fri, 11 Sep 2020 14:25:18 -0500 Subject: [PATCH 02/10] Create Lab1_solutions --- Labs/Lab.1/Lab1_solutions | 1 + 1 file changed, 1 insertion(+) create mode 100644 Labs/Lab.1/Lab1_solutions diff --git a/Labs/Lab.1/Lab1_solutions b/Labs/Lab.1/Lab1_solutions new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Labs/Lab.1/Lab1_solutions @@ -0,0 +1 @@ + From e1efe1adf08fbb55b2bf57852e89dd871727d59f Mon Sep 17 00:00:00 2001 From: matthewbruce90 <70781709+matthewbruce90@users.noreply.github.com> Date: Fri, 11 Sep 2020 14:26:48 -0500 Subject: [PATCH 03/10] Add files via upload --- Labs/Lab.1/Lab_1_solutions.ipynb | 894 +++++++++++++++++++++++++++++++ 1 file changed, 894 insertions(+) create mode 100644 Labs/Lab.1/Lab_1_solutions.ipynb diff --git a/Labs/Lab.1/Lab_1_solutions.ipynb b/Labs/Lab.1/Lab_1_solutions.ipynb new file mode 100644 index 0000000..1166e15 --- /dev/null +++ b/Labs/Lab.1/Lab_1_solutions.ipynb @@ -0,0 +1,894 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Copy of Lab-1.ipynb", + "provenance": [], + "collapsed_sections": [] + }, + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.0" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "O5vg8KKRq0sy" + }, + "source": [ + "# Lab 1\n", + "\n", + "## Python Notebooks on Google Colab\n", + "\n", + "Data 1401's Labs, Homework, and Exams will be all in form of iPython notebooks. You may already be familiar with python notebooks if you have used Jupyter before. 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). You may also choose to store your 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/UTA-DataScience/DATA1401.2020.Fall/blob/master/Labs/Lab.1/Lab.1.ipynb)\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "cdtC9qVbjk1x", + "colab_type": "code", + "colab": {} + }, + "source": [ + "echo" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "FVt_1hPt1dAK" + }, + "source": [ + "## Notebooks in Colab\n", + "\n", + "You now are presumably in Colab. Word of caution, by default, Google Colab does not save your notebooks, so if you close your session, you will loose your work.\n", + "\n", + "So first thing: from the file menu above select \"Save a copy in Drive\"." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "x0JBL_RFrDDj" + }, + "source": [ + "## Storing Notebooks in Google Drive (Optional)\n", + "If you like, you may save your notebooks directly into Google Drive and upload directly to Git (where you will be downloading and uploading your homework). In order properly setup Git, we'll need to work more directly in your Google Drive.\n", + "\n", + "On the left sidebar, press the file icon to see a listing of files accessibile to this Notebook. Then press \"Mount Drive\" and follow the instructions to mount your Google Drive in this notebook. A new cell will be inserted into this notebook, which after you run by pressing the play button will instruct you to follow a link to log into your Google Account and enable access to your Drive in another tab. Finally you will copy a link from the new tab back into the cell in this notebook. Once you are done, press refresh under files in the left sidebar and you should have \"drive/My Drive\" appear." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "hwJ6wJk3tiLv" + }, + "source": [ + "## Github\n", + "All the class material will be stored on github. You will also submit your homework using github. To do so, you will need a github account.\n", + "\n", + "If you do not already have a github account or wish to create a new one for this course, create one:\n", + "* Browse to [github.com](https://github.com).\n", + "* Click the green “Sign up for GitHub”\tbutton.\n", + "* Follow instructions for creating an account.\n", + "* Make sure you remember your github username and password.\n", + "\n", + "Write an email to the course Instructor titled \"Data 1401: Github account\" with your github username (not your password) as the contents.\n", + "\n", + "## Google Groups\n", + "\n", + "Class annoucements will be made via google groups. If you did not already receive an invite to the class google group, had trouble with the invite, or wish to use a different email address, write an email to the course TA titled \"Data 1401: Google Group\" with your preferred email.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "TjfIzdQZqvzk" + }, + "source": [ + "## Introduction: Unix, Git, and Jupyter\n", + "\n", + "This lab aims to introduce you to basic Unix, familiarize you with iPython notebooks and get you setup to submit your homework.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "C_LmOgzFqvzp" + }, + "source": [ + "\n", + "\n", + "### Terminal, Shell, and ssh\n", + "\n", + "\n", + "The terminal is a simple program that generally runs another program, taking mostly keyboard input from you, passing it to this other program, and taking the output of the program and displaying on the screen for you.\n", + "\n", + "The terminal usually runs a program called a shell. Shells present a command prompt where you can type in commands, which are then executed when you press enter. In most shells, there are some special commands which the shell will execute. Everything else you type in, the shell will assume is a name of a program you want to run and arguments you want to pass that program. So if the shell doesn't recognize something you type in, it'll try to find a program with a name that is the same as the first word you gave it. \n", + "\n", + "### Shell in Colab\n", + "\n", + "Unfortunately, google Colab does not allow you to open a terminal window. Jupyter does, so if you are running in Jupyter (which most of you will not be), you may choose to open a terminal window by returning to the jupyter file list tab and selecting new terminal from the top right.\n", + "\n", + "For Colab, we will have to do something non-ideal, but functional. There are several ways to execute shell commands from within a python notebook. For example, you can use any shell command by putting \"!\" in front of the command:\n", + "\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "KJ5f-WO0wcAv", + "colab": {} + }, + "source": [ + "!ls\n", + "!echo \"----------\"\n", + "!ls sample_data" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "8f-n4AXFw-dD" + }, + "source": [ + "Unfortunately, every time you use \"!\" a new environment is created and the state reverted to the original state. Try to understand the difference between the following two sets of commands:\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "99nrBYTWxZJr", + "colab": {} + }, + "source": [ + "!echo \"Technique 1:\"\n", + "!ls\n", + "!cd sample_data\n", + "!ls" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "2-Znf97Lxl-Z", + "colab": {} + }, + "source": [ + "!echo \"Technique 2:\"\n", + "!ls ; cd sample_data ;ls" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "4x9n1rAkxyYl" + }, + "source": [ + "Notebooks allow a bit of \"magic\" (using \"%\") to avoid some of these limitations:\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "vLBPTX4rx3gd", + "colab": {} + }, + "source": [ + "!echo \"Technique 3:\"\n", + "!ls \n", + "%cd sample_data \n", + "!ls" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "U8XpvPjcyH0w" + }, + "source": [ + "For our purposes, we are just going to explicitly start a new shell and interact with it in the output cell. Execute the following cell. You will be able to type and execute commands. Look around a bit using \"ls\" and \"cd. You can stop the cell from running by typing \"exit\". You may also stop the shell by pressing play/stop button." + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "MIDFitLZyuZy", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 97 + }, + "outputId": "3fe007ce-d69d-4ab1-93b5-cd5513293260" + }, + "source": [ + "!/bin/bash --noediting" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "bash: cannot set terminal process group (101): Inappropriate ioctl for device\n", + "bash: no job control in this shell\n", + "\u001b]0;root@326b2a288787: /content\u0007\u001b[01;32mroot@326b2a288787\u001b[00m:\u001b[01;34m/content\u001b[00m# " + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "q-4hfZBywW25" + }, + "source": [ + "While in this instance your shell is running in a this notebook, you can also run terminals natively on your own computer. On Linux or MacOS, you just have to run a program called terminal. In Windows you can start a \"command prompt\". \n", + "\n", + "Type in \"ls\" into the terminal above 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. \n", + "\n", + "For your reference, [Unix/Linux Command Reference](https://github.com/UTA-DataScience/VersionControlSystem/blob/master/VersionControlSystem/linuxRef.pdf) summarizes some of the common shell commands.\n", + "\n", + "A subtle point to realize here is that while the terminal is running in the browser that is running on the computer in front of you, the shell is actually running on a machine on google hardware. The shell prompt typically displays the name of the machine you are using. What you are not seeing is that there is an intermidate program between the terminal running on your computer and the shell running on google. This intermidary program is taking your input from the terminal sending it over the network to google and bringing back the responses for you terminal to display.\n", + "\n", + "A bit of extra information. If you start a terminal on your own computer, the shell runs locally. The \"ls\" command would then list contents of a directory on your computer. You can typically connect to Unix computers by evoking a shell running on that machine over the network. In this case, you would have to initiate this intermidiary program yourself. The program is called \"ssh\" (secure shell). You can \"ssh\" to another machine from your machine, by simply typing \"ssh\" followed by the machine name or IP address. Most likely you would be prompted for a password, after which you would dropped into the prompt of a shell running on the remote machine. \n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "51Eya4LBqvzs" + }, + "source": [ + "## Programs and Environment Variables\n", + "\n", + "You have a listing of your current directory, but you don't know where that directory resides. You can see what directory you are using the command \"pwd\" (print working directory). Issue the command and look at the response. You'll get a slash (\"/\") separated list, known as the path, of the directory hierarchy of your current working directory. On Colab, this will start with \"contents\"\n", + "\n", + "Now back to thinking about the command prompt. Since \"ls\" is a program, it most be stored somewhere. It is clearly not in your working directory, because you didn't see it when you executed \"ls\". We can ask the shell to tell us where it found \"ls\" using the \"which ls\" command. Note that \"which\" is also a program. \"which ls\" comes back with \"/bin/ls\", telling you the \"ls\" program is sitting in \"/bin\" directory of the system. \n", + "\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "VnNKvkCbjk26", + "colab_type": "code", + "colab": {} + }, + "source": [ + "!which ls" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "PR3x19xcjk29", + "colab_type": "text" + }, + "source": [ + "Lets see what else is in there by issuing a \"ls /bin\" command. " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "LMcRycHBjk2-", + "colab_type": "code", + "colab": {} + }, + "source": [ + "!ls /bin" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "xKlksRAIjk3I", + "colab_type": "text" + }, + "source": [ + "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\". Quick note that MacOS is a bit different and so \"--help\" doesn't seem to work." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Oi4uRhAZjk3J", + "colab_type": "code", + "colab": {} + }, + "source": [ + "!ls --help" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "qjtklhj4jk3V", + "colab_type": "text" + }, + "source": [ + " 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. " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "BwpBB7TEjk3V", + "colab_type": "code", + "colab": {} + }, + "source": [ + "!man ls" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "8ADR5MATjk3a", + "colab_type": "text" + }, + "source": [ + "Another command interesting is \"echo\". \"echo\" simply prints whatever you put after it to the screen. " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "mcpIgpkwjk3a", + "colab_type": "code", + "colab": {} + }, + "source": [ + "!echo \"Hello World\"" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "DluCpgjojk3f", + "colab_type": "text" + }, + "source": [ + "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." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Cjxe643yjk3h", + "colab_type": "code", + "colab": {} + }, + "source": [ + "!echo $PATH" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "EJUvZrLDjk3p", + "colab_type": "text" + }, + "source": [ + "You can set you own environment variables. Different shells have different syntax. Lets first figure out what shell we are running. \n", + "\n", + "*Exercise 1:* Use the \"echo\" command to print out the value of the \"SHELL\" environment variable:" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "YS7YFiPwqvzu" + }, + "source": [ + "!/bin/bash --noediting" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "YoEgruUhqvzw" + }, + "source": [ + "## Navigating Directories\n", + "\n", + "You can change your current directory using the \"cd\" shell command. Note that \"cd\" is not a Unix program. Once in a directory, you can use the \"ls\" command to list the contents or \"pwd\" to remind yourself your current working directory. You can move back one level in your current directory hierarchy using \"cd ..\". In general \"..\" represents the path to a directory one level above your current directory, \"../..\" represents two levels up, and so on. \".\" represents the current directory. If you look at the PATH environment variable, you'll notice that the last item is \".\", telling the shell to look into your current directory for commands. Finally the \"~\" character always refers to your home directory.\n", + "\n", + "Some other file manipulation commands:\n", + "\n", + " - The \"mkdir\" command creates new directories. \n", + " - \"cp\" and \"mv\" allow you to copy and move (or rename) files, taking 2 arguments: the original path/filename and the target path/filename. \n", + " - The \"rm\" and \"rmdir\" commands remove (delete) files and directories.\n", + "\n", + "\n", + "*Exercise 2:* Create a new directory called \"Data-1441\", and another directory inside \"Data-1441\" called \"Lab-1-Solutions\". Perform the rest of the lab in this directory." + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "A16VzZ3G0J8x", + "colab": {} + }, + "source": [ + "!/bin/bash --noediting" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "o38c4lbsqvzy" + }, + "source": [ + "## Exploring Unix Filesystem\n", + "\n", + "You can look at the root directory of the system by issuing \"ls /\". As explained in lecture, Unix uses the file system to communicate with devices and between processes. \"/etc\" keeps the configuration files of the system. \"/bin\" and \"/sbin\" store most of the standard Unix programs. \"/usr\" stores installed programs and their associate files, with \"/usr/bin\" usually storing the commands you can run. \n", + "\n", + "*Exercise 3:* List the \"/dev\" directory. How many SSD storage devices do you see? How many partitions does each device have? (Answer in box below)" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "yNj2LXzP2ksl", + "colab": {} + }, + "source": [ + "!/bin/bash --noediting" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "7P9EG0KOqvz2" + }, + "source": [ + "## Text File Manipulation\n", + "\n", + "As explained in lecture, Unix stores most information in text files. For example, the list of all users and their home directories are stored in \"/etc/passwd\". Let get some familiarity with the most commonly used commands to manipulate files.\n", + "\n", + " - You can see the contents contents a file using the \"cat\" (concatenate) command. Try executing \"cat /etc/passwd\". You'll get a huge list that will go by your screen quickly. \n", + " \n", + " - To go through the file page by page, you can use the \"less\" or \"more\" commands. \n", + " \n", + " - You can see the first or last N (N=10 by default) lines of a file using \"head\" or \"tail\" commands. For example \"tail -20 /etc/passwd\" will list the last 20 lines. \n", + " \n", + " - You can search a test file using the \"grep\" command, which takes a string keyword as the first argument and a filename as the second, and by default prints out every line in the file that contrains the string. So for example you can do \"grep \\$USER /etc/passwd\" to find the line corresponding to your account. Some useful flags: \n", + " \n", + " - \"-i\" ignores the case of the keyword\n", + " - \"-v\" display those lines that do NOT match \n", + " - \"-n\" precede each matching line with the line number \n", + " - \"-c\" print only the total count of matched lines \n", + " \n", + " For example \"grep -c \\$USER /etc/passwd\" should show that you are in the password file just once. \n", + " \n", + " - The \"wc\" (word count) command counts the number of lines, words, and characters in a file. By default \"wc\" gives you all three numbers, but \"-w\", \"-l\", or \"-c\" flags \n", + "\n", + "*Exercise 4:* Count how many lines in the password file contain the letter \"w\". " + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "UlsANMuf2qMs", + "colab": {} + }, + "source": [ + "!/bin/bash --noediting" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "SZuhLbD8qvz5" + }, + "source": [ + "## Redirection\n", + "\n", + "Unix provides programs \"pipes\" for input and output. Most of what you see on the screen when you run a program was written to the \"stdout\" (standard output) pipe. Other pipes are \"stdin\" (standard input) and \"stderr\" (standard error), where error messages are written.\n", + "\n", + "As discussed in lecture, the basic commands of are simple, but you can chain them to do complicated things. Redirection is how you chain these commands, directing the output of one command to the input of the next.\n", + "\n", + "As an example, consider the \"cat\" command. Cat takes stdin and outputs it to stdout. Type \"cat\" and press enter and confirm. You can get back to the command prompt by pressing \"control-c\" (sends terminate singal) or \"control-d\" (end of file character). Note that from now on we will use the convention: \"control-d\" = \"^D\"\n", + "\n", + "*Exercise 5a:* Using \"cat\" and indirection you can write things into a file. The \">\" symbol directs stdout into a file. Try \"cat > favorite-colors-list.txt\" and then type in your 3 favorite colors, each on it's own line. Use \"^D\" to end your input." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "H5vxtcXnqvz6" + }, + "source": [ + "Use \"cat\", \"more\", or \"less\" to confirm that you file is as you expect it. \">>\" allows you to append to the file. \n", + "\n", + "*Exercise 5b:* Append 2 more colors to your file." + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "twRKNaGy3XGw", + "colab": {} + }, + "source": [ + "!/bin/bash --noediting" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "DZODNKiAqvz8" + }, + "source": [ + "The \"sort\" command sorts what it sees on stdin. Instead of taking input from the terminal, you can direct the shell to take stdin from a file using \"<\". Try \"sort < favorite-color-list.txt\" and \"sort < favorite-color-list.txt > sorted-favorite-color-list.txt\".\n", + "\n", + "Finally, instead of piping input / output into files, you can directly chain one program into another using \"|\". So for example, you can do \"cat /etc/passwd | grep -i \\$USER | wc -l\". \n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "v5IaZXNyqvz_" + }, + "source": [ + "## Git\n", + "\n", + "`git` is a Version Control System (VCS), typically used to organize the source code of software project but also good source of documents or web-pages. An instance of `git` server stores repositories, each typically containing the code relevant to a specific project. Users create local `clones` of repositories, change and develop the local copies of the code, `commit` the changes to their local repository, `push` to the server as a contribution, `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/UTA-DataScience/DATA1401.2020.Fall)\n", + "\n", + "\n", + "Read through [Version Control System Lecture](https://github.com/UTA-DataScience/SpecialTopics/blob/master/VersionControlSystem/README.md) to better understand `git`. Note that you do not need to do the exercises at the end of the lecture.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "J_R64sQDqv0A" + }, + "source": [ + "## Plan\n", + "\n", + "You made a clone of the class repository at start of this lab. We will create a new fork where you can keep track and submit your work, following [these instructions](https://help.github.com/articles/fork-a-repo/).\n", + "\n", + "Goto to github.com and log in.\n", + "\n", + "Next, lets create a fork of the [class repository](https://github.com/UTA-DataScience/DATA1401.2020.Fall). Click the link and press the \"Fork\" button on the top right. Select your repository as where you want to place the fork.\n", + "\n", + "We show here two methods of using `git` to submit your work for this class. " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "pEYeeHoPjk4G", + "colab_type": "text" + }, + "source": [ + "## Using Your Web Browser\n", + "\n", + "### Updating your fork\n", + "\n", + "You should update your fork of the class repository everytime frequently to make sure you have all of up-to-date class material. \n", + "\n", + "* Goto the github page for *your fork* of the class repository. If there is an update, uyou should see a message \"This branch is N commit behind UTA-DataScience:master\" (where N=>1). \n", + "\n", + "* Follow the \"Pull Request\" link. \n", + "\n", + "* Select *your* fork of the repository as the base.\n", + "\n", + "* Select the main course repository as the head.\n", + "\n", + "* Create a pull request. (Takes two steps.)\n", + "\n", + "* Merge the pull request and confirm (green buttons).\n", + "\n", + "### Submitting your work\n", + "\n", + "Once you are done working on a lab (presumably on Colab) and are ready to submit:\n", + "\n", + "* Download your lab via the File -> \"Download .ipynb\". You will now have a copy of your notebook on your personal computer. \n", + "\n", + "* Rename the file so it is not identical to the filename provide. I suggest \"Lab.1.solutions.ipynb\" for example. Make sure you keep the \".ipynb\" extension.\n", + "\n", + "* Goto the github page for *your fork* of the class repository. \n", + "\n", + "* Navigate to the approprate directories (for example \"Labs/Lab.1\"). \n", + "\n", + "* Use the \"Add file\" near the top left of the page to \"Add files\" (if first time) or \"Upload files\" (if you are updating your solutions).\n", + "\n", + "* After the upload, make sure that your solutions were properly upload to navigating them in github.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "edTvE6rOqv0C" + }, + "source": [ + "## Using Command Line Interface\n", + "\n", + "Now we will check out your fork in your Google Drive / Colab.\n", + "\n", + "Note: Jupyter allows you to run shell directly in a notebook. We will use `!` and `%` to call shell commands directly in this notebook. Follow along yourself. Either create a new notebook or open a terminal. \n", + "\n", + "Start by listing the contents of your current directory." + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "e5tXg0f8qv0D", + "colab": {} + }, + "source": [ + "!ls" + ], + "execution_count": null, + "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": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "fwsBdTnYqv0Q" + }, + "source": [ + "From the github page for your fork, press the green \"Clone or download\" button and copy the URL.\n", + "\n", + "Goto to your notebook and use the following command to clone the repository, pasting the URL you just copied:\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "8w42MH6Jqv0S", + "colab": {} + }, + "source": [ + "# What you past here should look like:\n", + "#!git clone https://github.com//DATA1401.2020.Fall.git" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "cOAuqTVUqv0V" + }, + "source": [ + "Go into the directory:" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "b1Ew4tEZqv0X", + "colab": {} + }, + "source": [ + "%cd DATA1401.2020.Fall\n", + "!ls" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "IrhWToc-qv0a" + }, + "source": [ + "We will now connect your fork to the original so you can pull changes from there. \n", + "\n", + "Check remote status:" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "JxtMYR-9qv0c", + "colab": {} + }, + "source": [ + "!git remote -v" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "9ud3X0fBqv0f" + }, + "source": [ + "Now use the original class URL to set your upstream:" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "pgJlKxBqqv0h", + "colab": {} + }, + "source": [ + "!git remote add upstream https://github.com/UTA-DataScience/DATA1401.2020.Fall" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "id2yUEt9qv0k", + "colab": {} + }, + "source": [ + "!git remote -v" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "sAkgeJ6Iqv0n" + }, + "source": [ + "From now on, you can get the newest version of class material by using:" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "AGDsfTFLqv0o", + "colab": {} + }, + "source": [ + "!git pull" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "u9RAhs5b4vXY" + }, + "source": [ + "We will submit your Lab 1 using git at the next Lab." + ] + } + ] +} \ No newline at end of file From 27e00f7a37f24e25d5c92ee373b2917fff88fe37 Mon Sep 17 00:00:00 2001 From: matthewbruce90 <70781709+matthewbruce90@users.noreply.github.com> Date: Fri, 25 Sep 2020 16:21:26 -0500 Subject: [PATCH 04/10] Add files via upload --- Labs/Lab_2_solutions.ipynb | 668 +++++++++++++++++++++++++++++++++++++ 1 file changed, 668 insertions(+) create mode 100644 Labs/Lab_2_solutions.ipynb diff --git a/Labs/Lab_2_solutions.ipynb b/Labs/Lab_2_solutions.ipynb new file mode 100644 index 0000000..3964b58 --- /dev/null +++ b/Labs/Lab_2_solutions.ipynb @@ -0,0 +1,668 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Copy of Lab-2.ipynb", + "provenance": [], + "collapsed_sections": [] + }, + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.0" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "uk7yc0nadBGa" + }, + "source": [ + "# Lab 2\n", + "\n", + "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/UTA-DataScience/DATA1401.2020.Fall/blob/master/Labs/Lab.2/Lab.2.ipynb)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "u9RAhs5b4vXY" + }, + "source": [ + "\n", + "## Make your fork private\n", + "\n", + "Navigate your fork of the course repository on GitHub and click the \"gear\" icon to change the settings. Select \"Options\" on the left and scroll all the way down. Then click on \"Make Private\" to make your repository private. \n", + "\n", + "Next select the collaborators on the left and add your Professor as a collaborator. " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "q-gMWiE4dBG_" + }, + "source": [ + "## Python Programming\n", + "\n", + "In the remainder of this lab you will practice python by solving some simple exercises. \n", + "\n", + "*Exercise 1:* Write 2 functions `even(x)` and `odd(x)` that take an integer and returns True if the input is even or odd, otherwise returns False. Use cell below for your solution. Use the subsequent cell to demonstrate that your solution works. Feel free to add additional cell as needed using the \"+\" button on the button bar above. " + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "jwN5jff1dBG_", + "colab": {} + }, + "source": [ + "# Write you solution here\n", + "\n", + "def even(num):\n", + " if num%2==0:\n", + " return True\n", + " else:\n", + " return False" + ], + "execution_count": 68, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "xNJAcodhdBHB", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "35c73f4e-279e-4c54-9d9b-d51e4d7c1a9a" + }, + "source": [ + "# Test your solution here\n", + "\n", + "print(\"Even test, even input:\",even(2))\n", + "print(\"Even test, odd input:\",even(223))\n", + "\n", + "\n" + ], + "execution_count": 69, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Even test, even input: True\n", + "Even test, odd input: False\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "KL_pyzG8dBHD" + }, + "source": [ + "*Exercise 2:* Write a function that takes a list of numbers as input and returns a list of the subset of elements that are less that 10. Test your solution." + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "g8nt0wnldBHE", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "fc862e68-9189-471f-f4ef-8d95aa2b360a" + }, + "source": [ + "# Write you solution here\n", + "\n", + "input_string = input(\"Enter a list element separated by comma \")\n", + "list = input_string.split(\",\")\n" + ], + "execution_count": 130, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Enter a list element separated by comma 1,12,6\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "__HTUWA1dBHH", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 214 + }, + "outputId": "de6824d7-7db6-4a61-a8db-ab61bb0da1dc" + }, + "source": [ + "# Test your solution here\n", + "\n", + "if list < 10:\n", + " print(list)" + ], + "execution_count": 131, + "outputs": [ + { + "output_type": "error", + "ename": "TypeError", + "evalue": "ignored", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Test your solution here\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0;32mif\u001b[0m \u001b[0mlist\u001b[0m \u001b[0;34m<\u001b[0m \u001b[0;36m10\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlist\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mTypeError\u001b[0m: '<' not supported between instances of 'list' and 'int'" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "T0cx91JudBHK" + }, + "source": [ + "*Exercise 3:* Write a function that takes a number `x_max` as input and returns a function that performs the same task as exercise 2, but for `x_max` instead of 10." + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "PqummMcmdBHK", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "4deaa53c-5172-4ee3-b5d8-a53408c058d5" + }, + "source": [ + "# Write you solution here\n", + "\n", + "input_string = input(\"Enter a list element separated by comma \")\n", + "list = input_string.split(\",\")\n", + "\n" + ], + "execution_count": 123, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Enter a list element separated by comma 4,7,8\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "evRYemjXdBHN", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "77b59b85-f9a1-409a-81c6-b2ff31e50059" + }, + "source": [ + "# Test your solution here\n", + "\n", + "print(\"The largest number is:\", max(list))" + ], + "execution_count": 124, + "outputs": [ + { + "output_type": "stream", + "text": [ + "The largest number is: 8\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "h0I8M27LdBHP" + }, + "source": [ + "*Exercise 4:* Write a function that takes an interger as input and returns a list of all divisors of that number." + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "k6GUpDyrdBHP", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 85 + }, + "outputId": "e3750006-1cde-45d2-e781-8ac5d1bff84d" + }, + "source": [ + "# Write you solution here\n", + "\n", + "a=int(input(\"Enter a number:\"))\n", + "\n", + "print(\"The divisors are:\")\n", + "for x in range(1,a+1):\n", + " if(a%x==0):\n", + " print(x)\n", + " " + ], + "execution_count": 66, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Enter a number:3\n", + "The divisors are:\n", + "1\n", + "3\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "IOWnLMvxdBHT" + }, + "source": [ + "*Exercise 5:* Write a function that takes 2 lists as input and returns a list that contains only the elements that are common between the lists (without duplicates). Make sure your program works on two lists of different sizes." + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "tR31bnTDdBHT", + "colab": {} + }, + "source": [ + "# Write you solution here\n", + "\n", + "x = [2, 4, 6, 6, 8, 2, 68, 4, 9, 23, 48, 25, 11]\n", + "y = [1, 2, 2, 68, 13, 23, 7, 43, 45, 11, 99, 101, 123]\n", + "\n", + "a=set(x) & set(y)\n", + "\n" + ], + "execution_count": 35, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "IJDf6ebYdBHV", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "14ada68b-848a-46f4-bb9a-fd5065ccb92d" + }, + "source": [ + "# Test your solution here\n", + "\n", + "print(a)" + ], + "execution_count": 36, + "outputs": [ + { + "output_type": "stream", + "text": [ + "{2, 11, 68, 23}\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "3vA7M9pldBHX" + }, + "source": [ + "*Exercise 6:* Write a function that reads takes a string and returns `True` if the string is a palindrome. (A palindrome is a string that reads the same forwards and backwards.)" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "ncyMDzp6dBHX", + "colab": {} + }, + "source": [ + "# Write you solution here\n", + "\n", + "def palindrome(x):\n", + " return x == x[::-1]\n" + ], + "execution_count": 59, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "v5cmVQ6MdBHZ", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "67afbd41-5159-402b-ccba-fe31650fb742" + }, + "source": [ + "# Test your solution here\n", + "\n", + "x= input()\n", + "ans = palindrome(x)\n", + " \n", + "if ans:\n", + " print(\"Yes\")\n", + "else:\n", + " print(\"No\")" + ], + "execution_count": 65, + "outputs": [ + { + "output_type": "stream", + "text": [ + "non\n", + "Yes\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "fT35xwandBHc" + }, + "source": [ + "*Exercise 7:* Write a Rock-Paper-Scissors game function, that takes 2 strings, the inputs of player 1 and player 2, and output 1 or 2 corresponding to which player wins, or 0 if draw.\n", + "\n", + "Implement a Rock-Paper-Scissors game by soliciting input from 2 players, testing with this function, and repeating if there is a draw.\n", + "\n", + "Remember the rules:\n", + "\n", + "* Rock beats scissors\n", + "* Scissors beats paper\n", + "* Paper beats rock" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "U1_HxxaWdBHd", + "colab": {} + }, + "source": [ + "# Write you solution here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "ooR2ldZBdBHf", + "colab": {} + }, + "source": [ + "# Test your solution here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "dJQzXNKzdBHh" + }, + "source": [ + "*Exercise 8:* Write a function that takes a integer `n` as input and \n", + "outputs a list of the first `n` Fibonnaci numbers.\n", + "\n", + "The Fibonnaci seqence is a sequence of numbers where the next number in the sequence is the sum of the previous two numbers in the sequence. The sequence looks like this: 1, 1, 2, 3, 5, 8, 13, …)" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "G_4ooRXTdBHh", + "colab": {} + }, + "source": [ + "# Write you solution here\n", + "\n", + "def Fibonacci(n): \n", + " if n<0: \n", + " return\n", + " \n", + " elif n==0: \n", + " return 0\n", + " \n", + " elif n==1: \n", + " return 1\n", + " else: \n", + " return Fibonacci(n-1)+Fibonacci(n-2) " + ], + "execution_count": 92, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "rzK5FskJdBHj", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "2ba0aba7-9889-4d4c-dcbd-f8831f516474" + }, + "source": [ + "# Test your solution here\n", + "\n", + "n=int(input(\"Enter a number:\"))\n", + "print(Fibonacci(n))\n" + ], + "execution_count": 93, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Enter a number:9\n", + "34\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "q6c_AskadBHl" + }, + "source": [ + "*Exercise 9:* Write a function that takes a string of consisting of several words and returns a string that reverses the order of the words.\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "aJdXX6FHdBHl", + "colab": {} + }, + "source": [ + "# Write you solution here\n", + "\n", + "def word(a):\n", + " words=a.split(' ')\n", + " string=[]\n", + " for word in words:\n", + " string.insert(0, word)\n", + "\n" + ], + "execution_count": 106, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "nQyhnLZ_dBHn", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "6403322f-8750-45ae-c149-b14ab51ac9c6" + }, + "source": [ + "# Test your solution here\n", + "\n", + "a=input()\n", + "\n", + "print(\" \".join(string))" + ], + "execution_count": 107, + "outputs": [ + { + "output_type": "stream", + "text": [ + "i am matt\n", + "matt am i\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "NFSmRaSydBHq" + }, + "source": [ + "*Exercise 10:* Write a guessing game program that will repeatedly guess a number that the users picks, with the user indicating higher or lower, until it correctly guesses the number." + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "Ie2E1JzCdBHr", + "colab": {} + }, + "source": [ + "# Write you solution here\n", + "\n", + "right = 9\n", + "guess = 0\n", + "c = 0\n" + ], + "execution_count": 135, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "6T8YdWSMdBHs", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 221 + }, + "outputId": "b7fb5467-ce24-449e-80a6-d4a62329735a" + }, + "source": [ + "# Test your solution here\n", + "while guess != right and guess != \"exit\":\n", + " guess = input(\"Enter a guess \")\n", + "\n", + " if guess == \"exit\":\n", + " break\n", + "\n", + " guess = int(guess)\n", + " c += 1\n", + "\n", + " if guess < right:\n", + " print(\"low\")\n", + " elif guess > right:\n", + " print(\"high\")\n", + " else:\n", + " print(\"Yes\")\n" + ], + "execution_count": 136, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Enter a guess 7\n", + "low\n", + "Enter a guess 4\n", + "low\n", + "Enter a guess 25\n", + "high\n", + "Enter a guess 11\n", + "high\n", + "Enter a guess 10\n", + "high\n", + "Enter a guess 9\n", + "Yes\n" + ], + "name": "stdout" + } + ] + } + ] +} \ No newline at end of file From 1dfd4b1cd13e06e8adaec0293f99ad8cc8a0958b Mon Sep 17 00:00:00 2001 From: matthewbruce90 <70781709+matthewbruce90@users.noreply.github.com> Date: Fri, 25 Sep 2020 16:22:14 -0500 Subject: [PATCH 05/10] Add files via upload --- Labs/Lab.2/Lab_2_solutions.ipynb | 668 +++++++++++++++++++++++++++++++ 1 file changed, 668 insertions(+) create mode 100644 Labs/Lab.2/Lab_2_solutions.ipynb diff --git a/Labs/Lab.2/Lab_2_solutions.ipynb b/Labs/Lab.2/Lab_2_solutions.ipynb new file mode 100644 index 0000000..3964b58 --- /dev/null +++ b/Labs/Lab.2/Lab_2_solutions.ipynb @@ -0,0 +1,668 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Copy of Lab-2.ipynb", + "provenance": [], + "collapsed_sections": [] + }, + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.0" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "uk7yc0nadBGa" + }, + "source": [ + "# Lab 2\n", + "\n", + "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/UTA-DataScience/DATA1401.2020.Fall/blob/master/Labs/Lab.2/Lab.2.ipynb)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "u9RAhs5b4vXY" + }, + "source": [ + "\n", + "## Make your fork private\n", + "\n", + "Navigate your fork of the course repository on GitHub and click the \"gear\" icon to change the settings. Select \"Options\" on the left and scroll all the way down. Then click on \"Make Private\" to make your repository private. \n", + "\n", + "Next select the collaborators on the left and add your Professor as a collaborator. " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "q-gMWiE4dBG_" + }, + "source": [ + "## Python Programming\n", + "\n", + "In the remainder of this lab you will practice python by solving some simple exercises. \n", + "\n", + "*Exercise 1:* Write 2 functions `even(x)` and `odd(x)` that take an integer and returns True if the input is even or odd, otherwise returns False. Use cell below for your solution. Use the subsequent cell to demonstrate that your solution works. Feel free to add additional cell as needed using the \"+\" button on the button bar above. " + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "jwN5jff1dBG_", + "colab": {} + }, + "source": [ + "# Write you solution here\n", + "\n", + "def even(num):\n", + " if num%2==0:\n", + " return True\n", + " else:\n", + " return False" + ], + "execution_count": 68, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "xNJAcodhdBHB", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "35c73f4e-279e-4c54-9d9b-d51e4d7c1a9a" + }, + "source": [ + "# Test your solution here\n", + "\n", + "print(\"Even test, even input:\",even(2))\n", + "print(\"Even test, odd input:\",even(223))\n", + "\n", + "\n" + ], + "execution_count": 69, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Even test, even input: True\n", + "Even test, odd input: False\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "KL_pyzG8dBHD" + }, + "source": [ + "*Exercise 2:* Write a function that takes a list of numbers as input and returns a list of the subset of elements that are less that 10. Test your solution." + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "g8nt0wnldBHE", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "fc862e68-9189-471f-f4ef-8d95aa2b360a" + }, + "source": [ + "# Write you solution here\n", + "\n", + "input_string = input(\"Enter a list element separated by comma \")\n", + "list = input_string.split(\",\")\n" + ], + "execution_count": 130, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Enter a list element separated by comma 1,12,6\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "__HTUWA1dBHH", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 214 + }, + "outputId": "de6824d7-7db6-4a61-a8db-ab61bb0da1dc" + }, + "source": [ + "# Test your solution here\n", + "\n", + "if list < 10:\n", + " print(list)" + ], + "execution_count": 131, + "outputs": [ + { + "output_type": "error", + "ename": "TypeError", + "evalue": "ignored", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Test your solution here\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0;32mif\u001b[0m \u001b[0mlist\u001b[0m \u001b[0;34m<\u001b[0m \u001b[0;36m10\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlist\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mTypeError\u001b[0m: '<' not supported between instances of 'list' and 'int'" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "T0cx91JudBHK" + }, + "source": [ + "*Exercise 3:* Write a function that takes a number `x_max` as input and returns a function that performs the same task as exercise 2, but for `x_max` instead of 10." + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "PqummMcmdBHK", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "4deaa53c-5172-4ee3-b5d8-a53408c058d5" + }, + "source": [ + "# Write you solution here\n", + "\n", + "input_string = input(\"Enter a list element separated by comma \")\n", + "list = input_string.split(\",\")\n", + "\n" + ], + "execution_count": 123, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Enter a list element separated by comma 4,7,8\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "evRYemjXdBHN", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "77b59b85-f9a1-409a-81c6-b2ff31e50059" + }, + "source": [ + "# Test your solution here\n", + "\n", + "print(\"The largest number is:\", max(list))" + ], + "execution_count": 124, + "outputs": [ + { + "output_type": "stream", + "text": [ + "The largest number is: 8\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "h0I8M27LdBHP" + }, + "source": [ + "*Exercise 4:* Write a function that takes an interger as input and returns a list of all divisors of that number." + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "k6GUpDyrdBHP", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 85 + }, + "outputId": "e3750006-1cde-45d2-e781-8ac5d1bff84d" + }, + "source": [ + "# Write you solution here\n", + "\n", + "a=int(input(\"Enter a number:\"))\n", + "\n", + "print(\"The divisors are:\")\n", + "for x in range(1,a+1):\n", + " if(a%x==0):\n", + " print(x)\n", + " " + ], + "execution_count": 66, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Enter a number:3\n", + "The divisors are:\n", + "1\n", + "3\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "IOWnLMvxdBHT" + }, + "source": [ + "*Exercise 5:* Write a function that takes 2 lists as input and returns a list that contains only the elements that are common between the lists (without duplicates). Make sure your program works on two lists of different sizes." + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "tR31bnTDdBHT", + "colab": {} + }, + "source": [ + "# Write you solution here\n", + "\n", + "x = [2, 4, 6, 6, 8, 2, 68, 4, 9, 23, 48, 25, 11]\n", + "y = [1, 2, 2, 68, 13, 23, 7, 43, 45, 11, 99, 101, 123]\n", + "\n", + "a=set(x) & set(y)\n", + "\n" + ], + "execution_count": 35, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "IJDf6ebYdBHV", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "14ada68b-848a-46f4-bb9a-fd5065ccb92d" + }, + "source": [ + "# Test your solution here\n", + "\n", + "print(a)" + ], + "execution_count": 36, + "outputs": [ + { + "output_type": "stream", + "text": [ + "{2, 11, 68, 23}\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "3vA7M9pldBHX" + }, + "source": [ + "*Exercise 6:* Write a function that reads takes a string and returns `True` if the string is a palindrome. (A palindrome is a string that reads the same forwards and backwards.)" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "ncyMDzp6dBHX", + "colab": {} + }, + "source": [ + "# Write you solution here\n", + "\n", + "def palindrome(x):\n", + " return x == x[::-1]\n" + ], + "execution_count": 59, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "v5cmVQ6MdBHZ", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "67afbd41-5159-402b-ccba-fe31650fb742" + }, + "source": [ + "# Test your solution here\n", + "\n", + "x= input()\n", + "ans = palindrome(x)\n", + " \n", + "if ans:\n", + " print(\"Yes\")\n", + "else:\n", + " print(\"No\")" + ], + "execution_count": 65, + "outputs": [ + { + "output_type": "stream", + "text": [ + "non\n", + "Yes\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "fT35xwandBHc" + }, + "source": [ + "*Exercise 7:* Write a Rock-Paper-Scissors game function, that takes 2 strings, the inputs of player 1 and player 2, and output 1 or 2 corresponding to which player wins, or 0 if draw.\n", + "\n", + "Implement a Rock-Paper-Scissors game by soliciting input from 2 players, testing with this function, and repeating if there is a draw.\n", + "\n", + "Remember the rules:\n", + "\n", + "* Rock beats scissors\n", + "* Scissors beats paper\n", + "* Paper beats rock" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "U1_HxxaWdBHd", + "colab": {} + }, + "source": [ + "# Write you solution here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "ooR2ldZBdBHf", + "colab": {} + }, + "source": [ + "# Test your solution here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "dJQzXNKzdBHh" + }, + "source": [ + "*Exercise 8:* Write a function that takes a integer `n` as input and \n", + "outputs a list of the first `n` Fibonnaci numbers.\n", + "\n", + "The Fibonnaci seqence is a sequence of numbers where the next number in the sequence is the sum of the previous two numbers in the sequence. The sequence looks like this: 1, 1, 2, 3, 5, 8, 13, …)" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "G_4ooRXTdBHh", + "colab": {} + }, + "source": [ + "# Write you solution here\n", + "\n", + "def Fibonacci(n): \n", + " if n<0: \n", + " return\n", + " \n", + " elif n==0: \n", + " return 0\n", + " \n", + " elif n==1: \n", + " return 1\n", + " else: \n", + " return Fibonacci(n-1)+Fibonacci(n-2) " + ], + "execution_count": 92, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "rzK5FskJdBHj", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "2ba0aba7-9889-4d4c-dcbd-f8831f516474" + }, + "source": [ + "# Test your solution here\n", + "\n", + "n=int(input(\"Enter a number:\"))\n", + "print(Fibonacci(n))\n" + ], + "execution_count": 93, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Enter a number:9\n", + "34\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "q6c_AskadBHl" + }, + "source": [ + "*Exercise 9:* Write a function that takes a string of consisting of several words and returns a string that reverses the order of the words.\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "aJdXX6FHdBHl", + "colab": {} + }, + "source": [ + "# Write you solution here\n", + "\n", + "def word(a):\n", + " words=a.split(' ')\n", + " string=[]\n", + " for word in words:\n", + " string.insert(0, word)\n", + "\n" + ], + "execution_count": 106, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "nQyhnLZ_dBHn", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "6403322f-8750-45ae-c149-b14ab51ac9c6" + }, + "source": [ + "# Test your solution here\n", + "\n", + "a=input()\n", + "\n", + "print(\" \".join(string))" + ], + "execution_count": 107, + "outputs": [ + { + "output_type": "stream", + "text": [ + "i am matt\n", + "matt am i\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "NFSmRaSydBHq" + }, + "source": [ + "*Exercise 10:* Write a guessing game program that will repeatedly guess a number that the users picks, with the user indicating higher or lower, until it correctly guesses the number." + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "Ie2E1JzCdBHr", + "colab": {} + }, + "source": [ + "# Write you solution here\n", + "\n", + "right = 9\n", + "guess = 0\n", + "c = 0\n" + ], + "execution_count": 135, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "6T8YdWSMdBHs", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 221 + }, + "outputId": "b7fb5467-ce24-449e-80a6-d4a62329735a" + }, + "source": [ + "# Test your solution here\n", + "while guess != right and guess != \"exit\":\n", + " guess = input(\"Enter a guess \")\n", + "\n", + " if guess == \"exit\":\n", + " break\n", + "\n", + " guess = int(guess)\n", + " c += 1\n", + "\n", + " if guess < right:\n", + " print(\"low\")\n", + " elif guess > right:\n", + " print(\"high\")\n", + " else:\n", + " print(\"Yes\")\n" + ], + "execution_count": 136, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Enter a guess 7\n", + "low\n", + "Enter a guess 4\n", + "low\n", + "Enter a guess 25\n", + "high\n", + "Enter a guess 11\n", + "high\n", + "Enter a guess 10\n", + "high\n", + "Enter a guess 9\n", + "Yes\n" + ], + "name": "stdout" + } + ] + } + ] +} \ No newline at end of file From eba2d11013c45b8d55f308943f57badbd059d732 Mon Sep 17 00:00:00 2001 From: matthewbruce90 <70781709+matthewbruce90@users.noreply.github.com> Date: Fri, 9 Oct 2020 13:54:52 -0500 Subject: [PATCH 06/10] Add files via upload --- Labs/Lab.3/lab_3_answers.ipynb | 664 +++++++++++++++++++++++++++++++++ 1 file changed, 664 insertions(+) create mode 100644 Labs/Lab.3/lab_3_answers.ipynb diff --git a/Labs/Lab.3/lab_3_answers.ipynb b/Labs/Lab.3/lab_3_answers.ipynb new file mode 100644 index 0000000..8e64279 --- /dev/null +++ b/Labs/Lab.3/lab_3_answers.ipynb @@ -0,0 +1,664 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.0" + }, + "colab": { + "name": "Copy of Lab.3.ipynb", + "provenance": [] + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "uSAcgSyHfW8G" + }, + "source": [ + "# Lab 3- Tic Tac Toe\n", + "\n", + "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/UTA-DataScience/DATA1401.2020.Fall/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": "JozB9qb9fW8H" + }, + "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": { + "id": "fu9jrzAHfW8M", + "outputId": "ffd20e7d-d04b-46fe-e481-d5f763cd19d7", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 546 + } + }, + "source": [ + "import itertools\n", + "\n", + "play = True\n", + "player = [1, 2]\n", + "while play:\n", + " board_size = int(input(\"Board Size? \"))\n", + " game = [[0 for n in range(board_size)] for n in range(board_size)]\n", + " \n", + " game_won = False\n", + " game, _ = game_board(game, just_display=True)\n", + " player_choice = itertools.cycle([1, 2])\n", + " while not game_won:\n", + " player = next(player_choice)\n", + " print(f\"Player: {player} make a move\")\n", + " played = False\n", + " \n", + " while not played:\n", + " column_choice = int(input(\"choose column. : \"))\n", + " row_choice = int(input(\"choose row. : \"))\n", + " game, played = game_board(game, player, row_choice, column_choice)\n", + " \n", + " if win(game):\n", + " game_won = True\n", + " \n", + "\n", + "\n" + ], + "execution_count": 115, + "outputs": [ + { + "output_type": "error", + "ename": "KeyboardInterrupt", + "evalue": "ignored", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m/usr/local/lib/python3.6/dist-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36m_input_request\u001b[0;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[1;32m 728\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 729\u001b[0;31m \u001b[0mident\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mreply\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msession\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrecv\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstdin_socket\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 730\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mException\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.6/dist-packages/jupyter_client/session.py\u001b[0m in \u001b[0;36mrecv\u001b[0;34m(self, socket, mode, content, copy)\u001b[0m\n\u001b[1;32m 802\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 803\u001b[0;31m \u001b[0mmsg_list\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msocket\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrecv_multipart\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmode\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcopy\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcopy\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 804\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mzmq\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mZMQError\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.6/dist-packages/zmq/sugar/socket.py\u001b[0m in \u001b[0;36mrecv_multipart\u001b[0;34m(self, flags, copy, track)\u001b[0m\n\u001b[1;32m 490\u001b[0m \"\"\"\n\u001b[0;32m--> 491\u001b[0;31m \u001b[0mparts\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrecv\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mflags\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcopy\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcopy\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtrack\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtrack\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 492\u001b[0m \u001b[0;31m# have first part already, only loop while more to receive\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32mzmq/backend/cython/socket.pyx\u001b[0m in \u001b[0;36mzmq.backend.cython.socket.Socket.recv\u001b[0;34m()\u001b[0m\n", + "\u001b[0;32mzmq/backend/cython/socket.pyx\u001b[0m in \u001b[0;36mzmq.backend.cython.socket.Socket.recv\u001b[0;34m()\u001b[0m\n", + "\u001b[0;32mzmq/backend/cython/socket.pyx\u001b[0m in \u001b[0;36mzmq.backend.cython.socket._recv_copy\u001b[0;34m()\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.6/dist-packages/zmq/backend/cython/checkrc.pxd\u001b[0m in \u001b[0;36mzmq.backend.cython.checkrc._check_rc\u001b[0;34m()\u001b[0m\n", + "\u001b[0;31mKeyboardInterrupt\u001b[0m: ", + "\nDuring handling of the above exception, another exception occurred:\n", + "\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mplayer\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mwhile\u001b[0m \u001b[0mplay\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m \u001b[0mboard_size\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Board Size? \"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 7\u001b[0m \u001b[0mgame\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mn\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mboard_size\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mn\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mboard_size\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.6/dist-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36mraw_input\u001b[0;34m(self, prompt)\u001b[0m\n\u001b[1;32m 702\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_parent_ident\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 703\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_parent_header\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 704\u001b[0;31m \u001b[0mpassword\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 705\u001b[0m )\n\u001b[1;32m 706\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.6/dist-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36m_input_request\u001b[0;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[1;32m 732\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 733\u001b[0m \u001b[0;31m# re-raise KeyboardInterrupt, to truncate traceback\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 734\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 735\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 736\u001b[0m \u001b[0;32mbreak\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mKeyboardInterrupt\u001b[0m: " + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "R4spY7ePfW8P" + }, + "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": { + "id": "SckgX5SNfW8Q" + }, + "source": [ + "winner_is_2 = [[2, 2, 0],\n", + "\t[2, 1, 0],\n", + "\t[2, 1, 1]]\n", + "\n", + "winner_is_1 = [[1, 2, 0],\n", + "\t[2, 1, 0],\n", + "\t[2, 1, 1]]\n", + "\n", + "winner_is_also_1 = [[0, 1, 0],\n", + "\t[2, 1, 0],\n", + "\t[2, 1, 1]]\n", + "\n", + "no_winner = [[1, 2, 0],\n", + "\t[2, 1, 0],\n", + "\t[2, 1, 2]]\n", + "\n", + "also_no_winner = [[1, 2, 0],\n", + "\t[2, 1, 0],\n", + "\t[2, 1, 0]]" + ], + "execution_count": 6, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "6BoRZkUUfW8T", + "outputId": "005fa953-b3be-427b-d1de-62a286b89133", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 85 + } + }, + "source": [ + "def win(the_game):\n", + " \n", + " def all_same(l):\n", + " if l.count(l[0]) == len(l) and l[0] !=0:\n", + " return True\n", + " else:\n", + " return False\n", + " \n", + " for row in game:\n", + " print(row)\n", + " if all_same(row):\n", + " print(f\"player {row[0]} horizontal win\")\n", + " return True\n", + " \n", + " \n", + " for col in range(len(game)):\n", + " check = []\n", + " for row in game:\n", + " check.append(row[col])\n", + " if all_same(check):\n", + " print(f\"player {check[0]} vertical win\")\n", + " return True\n", + "\n", + " diags_2 = []\n", + " for ix in range(len(game)):\n", + " diags_2.append(game[ix][-ix-1])\n", + " if all_same(diags_2):\n", + " print(f\"player {diags_2[0]} diagonal win\")\n", + " return True\n", + " \n", + " diags_1 = []\n", + " for ix in range(len(game)):\n", + " diags_1.append(game[ix][ix])\n", + " if all_same(diags_1):\n", + " print(f\"player {diags_1[0]} diagonal win\") \n", + " return True\n", + " \n", + " print(\"game incomplete\")\n", + " return False\n", + " \n", + "\n", + "win(game)\n" + ], + "execution_count": 113, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[1, 2, 0]\n", + "[1, 1, 1]\n", + "player 1 horizontal win\n" + ], + "name": "stdout" + }, + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "True" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 113 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "tIEdNbKHfW8X" + }, + "source": [ + "# Test your solution here\n", + "game = [[1, 2, 0],\n", + " \t[1, 1, 1],\n", + " \t[2, 1, 0]]\n" + ], + "execution_count": 112, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "SVd4KcLyfW8c" + }, + "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": { + "id": "SXlhA91cfW8c", + "outputId": "40a70fa2-5cb3-4b6a-cfd5-3012d741ca2a", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 221 + } + }, + "source": [ + "# Write you solution heredef print_horiz_line():\n", + "\n", + "def print_horiz_line():\n", + " print(\"--- \" * hor_board_size)\n", + "\n", + "def print_vert_line():\n", + " print(\"| \" * (vert_board_size + 1))\n", + "\n", + "if __name__ == \"__main__\":\n", + " hor_board_size = int(input(\"What horizontal size of game board? \"))\n", + " vert_board_size = int(input(\"What vertical size of game board? \"))\n", + "\n", + " for index in range(board_size):\n", + " print_horiz_line()\n", + " print_vert_line()\n", + " print_horiz_line()\n", + "\n" + ], + "execution_count": 146, + "outputs": [ + { + "output_type": "stream", + "text": [ + "What size of game board? 5\n", + "--- --- --- --- --- \n", + "| | | | | | \n", + "--- --- --- --- --- \n", + "| | | | | | \n", + "--- --- --- --- --- \n", + "| | | | | | \n", + "--- --- --- --- --- \n", + "| | | | | | \n", + "--- --- --- --- --- \n", + "| | | | | | \n", + "--- --- --- --- --- \n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "h7zWAYTkfW8h" + }, + "source": [ + "# Test your solution here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "tcz46Wi6fW8k" + }, + "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": { + "id": "zugCqLGJfW8l" + }, + "source": [ + "# Write you solution here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "HKkE7SYxfW8n" + }, + "source": [ + "# Test your solution here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "r2CSSY9pfW8q" + }, + "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": { + "id": "hCwtt2d1fW8r" + }, + "source": [ + "# Write you solution here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "LB5m7X2RfW8t" + }, + "source": [ + "# Test your solution here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "iYvjSNpyfW8w" + }, + "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": "BDn2ZJPIfW8x" + }, + "source": [ + "# Write you solution here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "KtBFKVTPfW80" + }, + "source": [ + "# Test your solution here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "oqZG9AQUfW83" + }, + "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": { + "id": "JWI17-gZfW83" + }, + "source": [ + "# Write you solution here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "kOZJWZahfW86" + }, + "source": [ + "# Test your solution here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "odhLjNH7fW89" + }, + "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": { + "id": "2kFORkHRfW9A" + }, + "source": [ + "# Write you solution here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "r_yoHR6ZfW9G" + }, + "source": [ + "# Test your solution here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "7DC_2ncgfW9M" + }, + "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": { + "id": "_HFKpNSRfW9N", + "outputId": "36585559-35a9-4021-94ab-fcae4b8ae1ec", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 129 + } + }, + "source": [ + "# Write you solution here\n", + "\n", + "import itertools\n", + "\n", + "\n", + "def win(the_game):\n", + " \n", + " def all_same(l):\n", + " if l.count(l[0]) == len(l) and l[0] !=0:\n", + " return True\n", + " else:\n", + " return False\n", + " \n", + " for row in game:\n", + " print(row)\n", + " if all_same(row):\n", + " print(f\"player {row[0]} horizontal win\")\n", + " return True\n", + " \n", + " \n", + " for col in range(len(game)):\n", + " check = []\n", + " for row in game:\n", + " check.append(row[col])\n", + " if all_same(check):\n", + " print(f\"player {check[0]} vertical win\")\n", + " return True\n", + "\n", + " diags_2 = []\n", + " for ix in range(len(game)):\n", + " diags_2.append(game[ix][-ix-1])\n", + " if all_same(diags_2):\n", + " print(f\"player {diags_2[0]} diagonal win\")\n", + " return True\n", + " \n", + " diags_1 = []\n", + " for ix in range(len(game)):\n", + " diags_1.append(game[ix][ix])\n", + " if all_same(diags_1):\n", + " print(f\"player {diags_1[0]} diagonal win\") \n", + " return True\n", + " \n", + " \n", + " return False\n", + "\n", + "\n", + "\n", + "\n", + "def game_board(game_map, player=0, row=0, column=0, just_display=False):\n", + " try:\n", + " if game_map[row][column] !=0:\n", + " print(\"choose another spot\")\n", + " return game_map, False\n", + " print( \" \"+\" \".join([str(n) for n in range(len(game_map))]))\n", + " if not just_display:\n", + " game_map[row][column] = player\n", + " for count, row in enumerate(game_map):\n", + " print(count, row)\n", + " return game_map, True\n", + " \n", + " except:\n", + " print(\"Error. Input row/column space 0, 1, or 2\")\n", + " return False\n", + "\n", + "\n", + "\n", + "play = True\n", + "player = [1, 2]\n", + "while play:\n", + " board_size = int(input(\"Board Size? \"))\n", + " game = [[0 for n in range(board_size)] for n in range(board_size)]\n", + " \n", + " game_won = False\n", + " game, _ = game_board(game, just_display=True)\n", + " player_choice = itertools.cycle([1, 2])\n", + " while not game_won:\n", + " player = next(player_choice)\n", + " print(f\"Player: {player} make a move\")\n", + " played = False\n", + " \n", + " while not played:\n", + " column_choice = int(input(\"choose column. : \"))\n", + " row_choice = int(input(\"choose row. : \"))\n", + " game, played = game_board(game, player, row_choice, column_choice)\n", + " \n", + " if win(game):\n", + " game_won = True" + ], + "execution_count": 147, + "outputs": [ + { + "output_type": "error", + "ename": "IndentationError", + "evalue": "ignored", + "traceback": [ + "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m55\u001b[0m\n\u001b[0;31m print(\"--- \" * board_size)\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m expected an indented block\n" + ] + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "D7Gx5fe3fW9R" + }, + "source": [ + "# Test your solution here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "rN60HF_1fW9c" + }, + "source": [ + "*Exercise 10:* Test that your game works for 5x5 Tic Tac Toe. " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "smuu6OLefW9c" + }, + "source": [ + "# Test your solution here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "E6EwlWNjfW9f" + }, + "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": { + "id": "OVa11NykfW9g" + }, + "source": [ + "# Write you solution here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "RJOSuTl-fW9k" + }, + "source": [ + "# Test your solution here" + ], + "execution_count": null, + "outputs": [] + } + ] +} \ No newline at end of file From 9fd8350e3b8c0e91da9bd210eb6dfbcf64e33f1f Mon Sep 17 00:00:00 2001 From: matthewbruce90 <70781709+matthewbruce90@users.noreply.github.com> Date: Fri, 9 Oct 2020 13:57:47 -0500 Subject: [PATCH 07/10] Add files via upload --- Labs/Lab.3/Copy_of_Lab_3(2).ipynb | 794 ++++++++++++++++++++++++++++++ 1 file changed, 794 insertions(+) create mode 100644 Labs/Lab.3/Copy_of_Lab_3(2).ipynb diff --git a/Labs/Lab.3/Copy_of_Lab_3(2).ipynb b/Labs/Lab.3/Copy_of_Lab_3(2).ipynb new file mode 100644 index 0000000..b63350f --- /dev/null +++ b/Labs/Lab.3/Copy_of_Lab_3(2).ipynb @@ -0,0 +1,794 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.0" + }, + "colab": { + "name": "Copy of Lab.3.ipynb", + "provenance": [] + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "uSAcgSyHfW8G" + }, + "source": [ + "# Lab 3- Tic Tac Toe\n", + "\n", + "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/UTA-DataScience/DATA1401.2020.Fall/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": "JozB9qb9fW8H" + }, + "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": { + "id": "fu9jrzAHfW8M", + "outputId": "ffd20e7d-d04b-46fe-e481-d5f763cd19d7", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 546 + } + }, + "source": [ + "import itertools\n", + "\n", + "play = True\n", + "player = [1, 2]\n", + "while play:\n", + " board_size = int(input(\"Board Size? \"))\n", + " game = [[0 for n in range(board_size)] for n in range(board_size)]\n", + " \n", + " game_won = False\n", + " game, _ = game_board(game, just_display=True)\n", + " player_choice = itertools.cycle([1, 2])\n", + " while not game_won:\n", + " player = next(player_choice)\n", + " print(f\"Player: {player} make a move\")\n", + " played = False\n", + " \n", + " while not played:\n", + " column_choice = int(input(\"choose column. : \"))\n", + " row_choice = int(input(\"choose row. : \"))\n", + " game, played = game_board(game, player, row_choice, column_choice)\n", + " \n", + " if win(game):\n", + " game_won = True\n", + " \n", + "\n", + "\n" + ], + "execution_count": 115, + "outputs": [ + { + "output_type": "error", + "ename": "KeyboardInterrupt", + "evalue": "ignored", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m/usr/local/lib/python3.6/dist-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36m_input_request\u001b[0;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[1;32m 728\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 729\u001b[0;31m \u001b[0mident\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mreply\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msession\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrecv\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstdin_socket\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 730\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mException\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.6/dist-packages/jupyter_client/session.py\u001b[0m in \u001b[0;36mrecv\u001b[0;34m(self, socket, mode, content, copy)\u001b[0m\n\u001b[1;32m 802\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 803\u001b[0;31m \u001b[0mmsg_list\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msocket\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrecv_multipart\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmode\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcopy\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcopy\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 804\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mzmq\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mZMQError\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.6/dist-packages/zmq/sugar/socket.py\u001b[0m in \u001b[0;36mrecv_multipart\u001b[0;34m(self, flags, copy, track)\u001b[0m\n\u001b[1;32m 490\u001b[0m \"\"\"\n\u001b[0;32m--> 491\u001b[0;31m \u001b[0mparts\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrecv\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mflags\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcopy\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcopy\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtrack\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtrack\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 492\u001b[0m \u001b[0;31m# have first part already, only loop while more to receive\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32mzmq/backend/cython/socket.pyx\u001b[0m in \u001b[0;36mzmq.backend.cython.socket.Socket.recv\u001b[0;34m()\u001b[0m\n", + "\u001b[0;32mzmq/backend/cython/socket.pyx\u001b[0m in \u001b[0;36mzmq.backend.cython.socket.Socket.recv\u001b[0;34m()\u001b[0m\n", + "\u001b[0;32mzmq/backend/cython/socket.pyx\u001b[0m in \u001b[0;36mzmq.backend.cython.socket._recv_copy\u001b[0;34m()\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.6/dist-packages/zmq/backend/cython/checkrc.pxd\u001b[0m in \u001b[0;36mzmq.backend.cython.checkrc._check_rc\u001b[0;34m()\u001b[0m\n", + "\u001b[0;31mKeyboardInterrupt\u001b[0m: ", + "\nDuring handling of the above exception, another exception occurred:\n", + "\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mplayer\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mwhile\u001b[0m \u001b[0mplay\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m \u001b[0mboard_size\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Board Size? \"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 7\u001b[0m \u001b[0mgame\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mn\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mboard_size\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mn\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mboard_size\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.6/dist-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36mraw_input\u001b[0;34m(self, prompt)\u001b[0m\n\u001b[1;32m 702\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_parent_ident\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 703\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_parent_header\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 704\u001b[0;31m \u001b[0mpassword\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 705\u001b[0m )\n\u001b[1;32m 706\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.6/dist-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36m_input_request\u001b[0;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[1;32m 732\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 733\u001b[0m \u001b[0;31m# re-raise KeyboardInterrupt, to truncate traceback\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 734\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 735\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 736\u001b[0m \u001b[0;32mbreak\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mKeyboardInterrupt\u001b[0m: " + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "R4spY7ePfW8P" + }, + "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": { + "id": "SckgX5SNfW8Q" + }, + "source": [ + "winner_is_2 = [[2, 2, 0],\n", + "\t[2, 1, 0],\n", + "\t[2, 1, 1]]\n", + "\n", + "winner_is_1 = [[1, 2, 0],\n", + "\t[2, 1, 0],\n", + "\t[2, 1, 1]]\n", + "\n", + "winner_is_also_1 = [[0, 1, 0],\n", + "\t[2, 1, 0],\n", + "\t[2, 1, 1]]\n", + "\n", + "no_winner = [[1, 2, 0],\n", + "\t[2, 1, 0],\n", + "\t[2, 1, 2]]\n", + "\n", + "also_no_winner = [[1, 2, 0],\n", + "\t[2, 1, 0],\n", + "\t[2, 1, 0]]" + ], + "execution_count": 6, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "6BoRZkUUfW8T", + "outputId": "005fa953-b3be-427b-d1de-62a286b89133", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 85 + } + }, + "source": [ + "def win(the_game):\n", + " \n", + " def all_same(l):\n", + " if l.count(l[0]) == len(l) and l[0] !=0:\n", + " return True\n", + " else:\n", + " return False\n", + " \n", + " for row in game:\n", + " print(row)\n", + " if all_same(row):\n", + " print(f\"player {row[0]} horizontal win\")\n", + " return True\n", + " \n", + " \n", + " for col in range(len(game)):\n", + " check = []\n", + " for row in game:\n", + " check.append(row[col])\n", + " if all_same(check):\n", + " print(f\"player {check[0]} vertical win\")\n", + " return True\n", + "\n", + " diags_2 = []\n", + " for ix in range(len(game)):\n", + " diags_2.append(game[ix][-ix-1])\n", + " if all_same(diags_2):\n", + " print(f\"player {diags_2[0]} diagonal win\")\n", + " return True\n", + " \n", + " diags_1 = []\n", + " for ix in range(len(game)):\n", + " diags_1.append(game[ix][ix])\n", + " if all_same(diags_1):\n", + " print(f\"player {diags_1[0]} diagonal win\") \n", + " return True\n", + " \n", + " print(\"game incomplete\")\n", + " return False\n", + " \n", + "\n", + "win(game)\n" + ], + "execution_count": 113, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[1, 2, 0]\n", + "[1, 1, 1]\n", + "player 1 horizontal win\n" + ], + "name": "stdout" + }, + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "True" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 113 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "tIEdNbKHfW8X" + }, + "source": [ + "# Test your solution here\n", + "game = [[1, 2, 0],\n", + " \t[1, 1, 1],\n", + " \t[2, 1, 0]]\n" + ], + "execution_count": 112, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "SVd4KcLyfW8c" + }, + "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": { + "id": "SXlhA91cfW8c", + "outputId": "40a70fa2-5cb3-4b6a-cfd5-3012d741ca2a", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 221 + } + }, + "source": [ + "# Write you solution heredef print_horiz_line():\n", + "\n", + "def print_horiz_line():\n", + " print(\"--- \" * hor_board_size)\n", + "\n", + "def print_vert_line():\n", + " print(\"| \" * (vert_board_size + 1))\n", + "\n", + "if __name__ == \"__main__\":\n", + " hor_board_size = int(input(\"What horizontal size of game board? \"))\n", + " vert_board_size = int(input(\"What vertical size of game board? \"))\n", + "\n", + " for index in range(board_size):\n", + " print_horiz_line()\n", + " print_vert_line()\n", + " print_horiz_line()\n", + "\n" + ], + "execution_count": 146, + "outputs": [ + { + "output_type": "stream", + "text": [ + "What size of game board? 5\n", + "--- --- --- --- --- \n", + "| | | | | | \n", + "--- --- --- --- --- \n", + "| | | | | | \n", + "--- --- --- --- --- \n", + "| | | | | | \n", + "--- --- --- --- --- \n", + "| | | | | | \n", + "--- --- --- --- --- \n", + "| | | | | | \n", + "--- --- --- --- --- \n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "h7zWAYTkfW8h" + }, + "source": [ + "# Test your solution here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "tcz46Wi6fW8k" + }, + "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": { + "id": "zugCqLGJfW8l" + }, + "source": [ + "# Write you solution here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "HKkE7SYxfW8n" + }, + "source": [ + "# Test your solution here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "r2CSSY9pfW8q" + }, + "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": { + "id": "hCwtt2d1fW8r" + }, + "source": [ + "# Write you solution here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "LB5m7X2RfW8t" + }, + "source": [ + "# Test your solution here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "iYvjSNpyfW8w" + }, + "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": "BDn2ZJPIfW8x" + }, + "source": [ + "# Write you solution here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "KtBFKVTPfW80" + }, + "source": [ + "# Test your solution here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "oqZG9AQUfW83" + }, + "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": { + "id": "JWI17-gZfW83" + }, + "source": [ + "# Write you solution here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "kOZJWZahfW86" + }, + "source": [ + "# Test your solution here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "odhLjNH7fW89" + }, + "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": { + "id": "2kFORkHRfW9A" + }, + "source": [ + "# Write you solution here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "r_yoHR6ZfW9G" + }, + "source": [ + "# Test your solution here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "7DC_2ncgfW9M" + }, + "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": { + "id": "_HFKpNSRfW9N", + "outputId": "36585559-35a9-4021-94ab-fcae4b8ae1ec", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 129 + } + }, + "source": [ + "# Write you solution here\n", + "\n", + "import itertools\n", + "\n", + "\n", + "def win(the_game):\n", + " \n", + " def all_same(l):\n", + " if l.count(l[0]) == len(l) and l[0] !=0:\n", + " return True\n", + " else:\n", + " return False\n", + " \n", + " for row in game:\n", + " print(row)\n", + " if all_same(row):\n", + " print(f\"player {row[0]} horizontal win\")\n", + " return True\n", + " \n", + " \n", + " for col in range(len(game)):\n", + " check = []\n", + " for row in game:\n", + " check.append(row[col])\n", + " if all_same(check):\n", + " print(f\"player {check[0]} vertical win\")\n", + " return True\n", + "\n", + " diags_2 = []\n", + " for ix in range(len(game)):\n", + " diags_2.append(game[ix][-ix-1])\n", + " if all_same(diags_2):\n", + " print(f\"player {diags_2[0]} diagonal win\")\n", + " return True\n", + " \n", + " diags_1 = []\n", + " for ix in range(len(game)):\n", + " diags_1.append(game[ix][ix])\n", + " if all_same(diags_1):\n", + " print(f\"player {diags_1[0]} diagonal win\") \n", + " return True\n", + " \n", + " \n", + " return False\n", + "\n", + "\n", + "\n", + "\n", + "def game_board(game_map, player=0, row=0, column=0, just_display=False):\n", + " try:\n", + " if game_map[row][column] !=0:\n", + " print(\"choose another spot\")\n", + " return game_map, False\n", + " print( \" \"+\" \".join([str(n) for n in range(len(game_map))]))\n", + " if not just_display:\n", + " game_map[row][column] = player\n", + " for count, row in enumerate(game_map):\n", + " print(count, row)\n", + " return game_map, True\n", + " \n", + " except:\n", + " print(\"Error. Input row/column space 0, 1, or 2\")\n", + " return False\n", + "\n", + "\n", + "\n", + "play = True\n", + "player = [1, 2]\n", + "while play:\n", + " board_size = int(input(\"Board Size? \"))\n", + " game = [[0 for n in range(board_size)] for n in range(board_size)]\n", + " \n", + " game_won = False\n", + " game, _ = game_board(game, just_display=True)\n", + " player_choice = itertools.cycle([1, 2])\n", + " while not game_won:\n", + " player = next(player_choice)\n", + " print(f\"Player: {player} make a move\")\n", + " played = False\n", + " \n", + " while not played:\n", + " column_choice = int(input(\"choose column. : \"))\n", + " row_choice = int(input(\"choose row. : \"))\n", + " game, played = game_board(game, player, row_choice, column_choice)\n", + " \n", + " if win(game):\n", + " game_won = True" + ], + "execution_count": 147, + "outputs": [ + { + "output_type": "error", + "ename": "IndentationError", + "evalue": "ignored", + "traceback": [ + "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m55\u001b[0m\n\u001b[0;31m print(\"--- \" * board_size)\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m expected an indented block\n" + ] + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "D7Gx5fe3fW9R" + }, + "source": [ + "# Test your solution here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "rN60HF_1fW9c" + }, + "source": [ + "*Exercise 10:* Test that your game works for 5x5 Tic Tac Toe. " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "smuu6OLefW9c", + "outputId": "5f0c8aa8-791a-4c09-ad0d-4f8c67504250", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 702 + } + }, + "source": [ + "# Test your solution here\n", + "\n", + "import itertools\n", + "\n", + "\n", + "def win(the_game):\n", + " \n", + " def all_same(l):\n", + " if l.count(l[0]) == len(l) and l[0] !=0:\n", + " return True\n", + " else:\n", + " return False\n", + " \n", + " for row in game:\n", + " print(row)\n", + " if all_same(row):\n", + " print(f\"player {row[0]} horizontal win\")\n", + " return True\n", + " \n", + " \n", + " for col in range(len(game)):\n", + " check = []\n", + " for row in game:\n", + " check.append(row[col])\n", + " if all_same(check):\n", + " print(f\"player {check[0]} vertical win\")\n", + " return True\n", + "\n", + " diags_2 = []\n", + " for ix in range(len(game)):\n", + " diags_2.append(game[ix][-ix-1])\n", + " if all_same(diags_2):\n", + " print(f\"player {diags_2[0]} diagonal win\")\n", + " return True\n", + " \n", + " diags_1 = []\n", + " for ix in range(len(game)):\n", + " diags_1.append(game[ix][ix])\n", + " if all_same(diags_1):\n", + " print(f\"player {diags_1[0]} diagonal win\") \n", + " return True\n", + " \n", + " \n", + " return False\n", + "\n", + "\n", + "\n", + "\n", + "def game_board(game_map, player=0, row=0, column=0, just_display=False):\n", + " try:\n", + " if game_map[row][column] !=0:\n", + " print(\"choose another spot\")\n", + " return game_map, False\n", + " print( \" \"+\" \".join([str(n) for n in range(len(game_map))]))\n", + " if not just_display:\n", + " game_map[row][column] = player\n", + " for count, row in enumerate(game_map):\n", + " print(count, row)\n", + " return game_map, True\n", + " \n", + " except:\n", + " print(\"Error. Input row/column space 0, 1, or 2\")\n", + " return False\n", + "\n", + "\n", + "\n", + "play = True\n", + "player = [1, 2]\n", + "while play:\n", + " board_size = int(input(\"Board Size? \"))\n", + " game = [[0 for n in range(board_size)] for n in range(board_size)]\n", + " \n", + " game_won = False\n", + " game, _ = game_board(game, just_display=True)\n", + " player_choice = itertools.cycle([1, 2])\n", + " while not game_won:\n", + " player = next(player_choice)\n", + " print(f\"Player: {player} make a move\")\n", + " played = False\n", + " \n", + " while not played:\n", + " column_choice = int(input(\"choose column. : \"))\n", + " row_choice = int(input(\"choose row. : \"))\n", + " game, played = game_board(game, player, row_choice, column_choice)\n", + " \n", + " if win(game):\n", + " game_won = True\n", + " " + ], + "execution_count": 148, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Board Size? 5\n", + " 0 1 2 3 4\n", + "0 [0, 0, 0, 0, 0]\n", + "1 [0, 0, 0, 0, 0]\n", + "2 [0, 0, 0, 0, 0]\n", + "3 [0, 0, 0, 0, 0]\n", + "4 [0, 0, 0, 0, 0]\n", + "Player: 1 make a move\n" + ], + "name": "stdout" + }, + { + "output_type": "error", + "ename": "KeyboardInterrupt", + "evalue": "ignored", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m/usr/local/lib/python3.6/dist-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36m_input_request\u001b[0;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[1;32m 728\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 729\u001b[0;31m \u001b[0mident\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mreply\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msession\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrecv\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstdin_socket\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 730\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mException\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.6/dist-packages/jupyter_client/session.py\u001b[0m in \u001b[0;36mrecv\u001b[0;34m(self, socket, mode, content, copy)\u001b[0m\n\u001b[1;32m 802\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 803\u001b[0;31m \u001b[0mmsg_list\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msocket\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrecv_multipart\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmode\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcopy\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcopy\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 804\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mzmq\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mZMQError\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.6/dist-packages/zmq/sugar/socket.py\u001b[0m in \u001b[0;36mrecv_multipart\u001b[0;34m(self, flags, copy, track)\u001b[0m\n\u001b[1;32m 490\u001b[0m \"\"\"\n\u001b[0;32m--> 491\u001b[0;31m \u001b[0mparts\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrecv\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mflags\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcopy\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcopy\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtrack\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtrack\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 492\u001b[0m \u001b[0;31m# have first part already, only loop while more to receive\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32mzmq/backend/cython/socket.pyx\u001b[0m in \u001b[0;36mzmq.backend.cython.socket.Socket.recv\u001b[0;34m()\u001b[0m\n", + "\u001b[0;32mzmq/backend/cython/socket.pyx\u001b[0m in \u001b[0;36mzmq.backend.cython.socket.Socket.recv\u001b[0;34m()\u001b[0m\n", + "\u001b[0;32mzmq/backend/cython/socket.pyx\u001b[0m in \u001b[0;36mzmq.backend.cython.socket._recv_copy\u001b[0;34m()\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.6/dist-packages/zmq/backend/cython/checkrc.pxd\u001b[0m in \u001b[0;36mzmq.backend.cython.checkrc._check_rc\u001b[0;34m()\u001b[0m\n", + "\u001b[0;31mKeyboardInterrupt\u001b[0m: ", + "\nDuring handling of the above exception, another exception occurred:\n", + "\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 80\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 81\u001b[0m \u001b[0;32mwhile\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mplayed\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 82\u001b[0;31m \u001b[0mcolumn_choice\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"choose column. : \"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 83\u001b[0m \u001b[0mrow_choice\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"choose row. : \"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 84\u001b[0m \u001b[0mgame\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mplayed\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mgame_board\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mgame\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mplayer\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrow_choice\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcolumn_choice\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.6/dist-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36mraw_input\u001b[0;34m(self, prompt)\u001b[0m\n\u001b[1;32m 702\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_parent_ident\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 703\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_parent_header\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 704\u001b[0;31m \u001b[0mpassword\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 705\u001b[0m )\n\u001b[1;32m 706\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.6/dist-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36m_input_request\u001b[0;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[1;32m 732\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 733\u001b[0m \u001b[0;31m# re-raise KeyboardInterrupt, to truncate traceback\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 734\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 735\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 736\u001b[0m \u001b[0;32mbreak\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mKeyboardInterrupt\u001b[0m: " + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "E6EwlWNjfW9f" + }, + "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": { + "id": "OVa11NykfW9g" + }, + "source": [ + "# Write you solution here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "RJOSuTl-fW9k" + }, + "source": [ + "# Test your solution here" + ], + "execution_count": null, + "outputs": [] + } + ] +} \ No newline at end of file From db6b4701f56f7a1262c887a060fa52d9ee74dd12 Mon Sep 17 00:00:00 2001 From: matthewbruce90 <70781709+matthewbruce90@users.noreply.github.com> Date: Fri, 9 Oct 2020 14:00:18 -0500 Subject: [PATCH 08/10] Add files via upload --- Quizzes/Quiz.1/quiz4.ipynb | 212 +++++++++++++++++++++++++++++++++++++ 1 file changed, 212 insertions(+) create mode 100644 Quizzes/Quiz.1/quiz4.ipynb diff --git a/Quizzes/Quiz.1/quiz4.ipynb b/Quizzes/Quiz.1/quiz4.ipynb new file mode 100644 index 0000000..8aee63b --- /dev/null +++ b/Quizzes/Quiz.1/quiz4.ipynb @@ -0,0 +1,212 @@ +{ + "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 Quiz.1.ipynb", + "provenance": [], + "collapsed_sections": [] + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "Sjle8uPbkgJJ" + }, + "source": [ + "# Quiz 1\n", + "\n", + "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/UTA-DataScience/DATA1401.2020.Fall/blob/master/Quizzes/Quiz.1/Quiz.1.ipynb)\n", + "\n", + "\n", + "\n", + "\n", + "1. Using the lists below, create a function (`make_deck`) that returns a list of the 52 cards in a usual playing card deck. Represent each card as a tuple (e.g. `('Clubs', 3)`). " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "VPcYHsblkgJK", + "outputId": "7ea9493f-e337-481e-8b15-7d9856efb9ae", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "suits = [\"Clubs\", \"Diamonds\", \"Hearts\", \"Spades\"]\n", + "values = list(range(2,11)) + [ \"Jack\", \"Queen\", \"King\", \"Ace\"]\n", + "print(suits)\n", + "print(values)" + ], + "execution_count": 3, + "outputs": [ + { + "output_type": "stream", + "text": [ + "['Clubs', 'Diamonds', 'Hearts', 'Spades']\n", + "[2, 3, 4, 5, 6, 7, 8, 9, 10, 'Jack', 'Queen', 'King', 'Ace']\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "l8XM15KhkgJO" + }, + "source": [ + "# Your solution here\n", + "\n", + "suits = ['spades', 'clubs', 'hearts', 'diamonds']\n", + "vals = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'jack', 'queen', 'king', 'ace']\n", + "\n", + "make_deck = list(itertools.product(vals, suits))\n", + "\n", + "random.shuffle(make_deck)\n", + "\n" + ], + "execution_count": 16, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "EOj0_pIzkgJS", + "outputId": "6f0840ae-ee8a-48bc-987a-f7dd80f8d986", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 901 + } + }, + "source": [ + "# Test your solution here\n", + "\n", + "for val, suit in deck:\n", + " print('%s, %s' % (suit, val))" + ], + "execution_count": 17, + "outputs": [ + { + "output_type": "stream", + "text": [ + "hearts, king\n", + "diamonds, 5\n", + "clubs, 7\n", + "clubs, 10\n", + "diamonds, 6\n", + "hearts, 4\n", + "diamonds, 4\n", + "diamonds, 2\n", + "spades, 3\n", + "diamonds, queen\n", + "hearts, 3\n", + "clubs, king\n", + "spades, 7\n", + "diamonds, 9\n", + "hearts, 7\n", + "hearts, jack\n", + "clubs, 6\n", + "hearts, 5\n", + "clubs, 2\n", + "diamonds, ace\n", + "clubs, 5\n", + "hearts, 2\n", + "hearts, 8\n", + "diamonds, 3\n", + "diamonds, king\n", + "spades, 4\n", + "diamonds, jack\n", + "spades, 2\n", + "clubs, ace\n", + "spades, jack\n", + "spades, 9\n", + "spades, queen\n", + "spades, 5\n", + "diamonds, 8\n", + "hearts, 9\n", + "spades, ace\n", + "clubs, jack\n", + "diamonds, 10\n", + "clubs, queen\n", + "clubs, 3\n", + "spades, 10\n", + "spades, 8\n", + "diamonds, 7\n", + "clubs, 4\n", + "hearts, queen\n", + "spades, king\n", + "clubs, 8\n", + "hearts, 10\n", + "clubs, 9\n", + "spades, 6\n", + "hearts, 6\n", + "hearts, ace\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "E_N_HUHikgJX" + }, + "source": [ + "2. Create the following functions that take in a list of [usually 5] cards (`hand`) and return True/False:\n", + " * `flush(hand)`: Checks if the cards in the hand are of the same suit.\n", + " * `straight(hand)`: Checks if the cards in the hand are in a order.\n", + " * You can start with just checking numerical cards.\n", + " * Extra Credit: Include all cards.\n", + " * `straight_flush(hand)`: Combination of both." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "q5ZLpqJqkgJX" + }, + "source": [ + "# Your solution here\n", + "\n", + "def flush(hand):\n", + " suits = [h[-2:] for h in hand]\n", + " suits = np.unique(suits)\n", + " return (len(suits) == 1)" + ], + "execution_count": 18, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "55SXXICRkgJa" + }, + "source": [ + "# Test your solution here" + ], + "execution_count": null, + "outputs": [] + } + ] +} \ No newline at end of file From 8f5aab217e9c731460e67a25d5dc1bfe75dbc4fd Mon Sep 17 00:00:00 2001 From: matthewbruce90 <70781709+matthewbruce90@users.noreply.github.com> Date: Mon, 19 Oct 2020 13:22:23 -0500 Subject: [PATCH 09/10] Add files via upload --- Quizzes/Quiz.2/quiz2answers.ipynb | 232 ++++++++++++++++++++++++++++++ 1 file changed, 232 insertions(+) create mode 100644 Quizzes/Quiz.2/quiz2answers.ipynb diff --git a/Quizzes/Quiz.2/quiz2answers.ipynb b/Quizzes/Quiz.2/quiz2answers.ipynb new file mode 100644 index 0000000..4c4bc1a --- /dev/null +++ b/Quizzes/Quiz.2/quiz2answers.ipynb @@ -0,0 +1,232 @@ +{ + "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 Copy of Quiz.2.ipynb", + "provenance": [], + "collapsed_sections": [] + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "hMbmfWjXCCZS" + }, + "source": [ + "# Quiz 2\n", + "\n", + "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/UTA-DataScience/DATA1401.2020.Fall/blob/master/Quizzes/Quiz.2/Quiz.2.ipynb)\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "4QnyAKXiCCZW" + }, + "source": [ + "1. Using the python math and random libraries, implement the Box-Muller transform. This transform generates 2 random variables $Z_0$ and $Z_1$ that are Normal distributed from two uniformly distributed random variables $U_1$ and $U_2$ using the relation:\n", + "\n", + "\\begin{align}\n", + "Z_0&=&\\sqrt{-2 \\log{U_1}} \\cos{(2\\pi U_2)}\\\\\n", + "Z_1&=&\\sqrt{-2 \\log{U_1}} \\sin{(2\\pi U_2)}\\\\\n", + "\\end{align}\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "IYkcSRgrCCZY" + }, + "source": [ + "Recall that you can import the math and random libraries using:\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "mwpsiltaCCZZ" + }, + "source": [ + "import math\n", + "import random" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "wVba20nYCCZh" + }, + "source": [ + "The functions and variables you will need are:" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "7kzUHmp8CCZi", + "outputId": "550b8259-792f-41f1-9ef9-de6cd5efe2ec" + }, + "source": [ + "x = .1\n", + "\n", + "# Math\n", + "math.pi\n", + "math.sin(x)\n", + "math.cos(x)\n", + "math.log(x)\n", + "math.sqrt(x)\n", + "\n", + "# Random\n", + "random.random()" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "0.9766680279361165" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 3 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "v0_GNTLSCCZn" + }, + "source": [ + "Note that math.log is the natural logarithm. Your solution should look like:" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "12fk1IgGCCZo" + }, + "source": [ + "def generate_normal(N):\n", + " out = list() \n", + " # Write your solution here\n", + " # …\n", + " return out\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "opJyGFLfCCZw" + }, + "source": [ + "Where N is the number of values of the random variable to generate, and the return value out is a list containing N normal distributed values. \n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "sSyyA5rzCCZw" + }, + "source": [ + "2. Test your code by generating 1000 normal distributed values and showing that the mean is 0 and variance is 1. You can use the following 2 functions:\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "mQaw0tsGCCZx" + }, + "source": [ + "def mean(x):\n", + " return sum(x)/float(len(x))\n", + "\n", + "def variance(x):\n", + " return sum(map(lambda y: y**2,x))/float(len(x))-mean(x)**2\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "5FgP89m6CCZ0" + }, + "source": [ + "3. Submit your solutions to GitHub in same way as your labs/homework." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "V5mcdaYpCCZ1", + "outputId": "db00b295-620e-4e02-c0df-e85bca1dbaf3", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 231 + } + }, + "source": [ + "import math\n", + "import random\n", + "\n", + "def generate_normal(N):\n", + " out = list() \n", + " z1 = math.sqrt(-2*math.log(u1))*math.cos(2*math.pi*u2)\n", + " z2 = math.sqrt(-2*math.log(u1))*math.sin(2*math.pi*u2)\n", + " return z1,z2\n", + " \n", + "u1 = random.random(1000)\n", + "u2 = random.random(1000)\n", + "\n", + "z1,z2 = generate_normal(u1,u2)\n", + "\n", + "def mean(x):\n", + " return sum(x)/float(len(x))\n", + "\n" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "error", + "ename": "TypeError", + "evalue": "ignored", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mz1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mz2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 10\u001b[0;31m \u001b[0mu1\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mrandom\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrandom\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1000\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 11\u001b[0m \u001b[0mu2\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mrandom\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrandom\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1000\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 12\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mTypeError\u001b[0m: random() takes no arguments (1 given)" + ] + } + ] + } + ] +} \ No newline at end of file From a12656bd57a2ba55077e60c85e08fc366c1fbf40 Mon Sep 17 00:00:00 2001 From: matthewbruce90 <70781709+matthewbruce90@users.noreply.github.com> Date: Fri, 23 Oct 2020 17:47:13 -0500 Subject: [PATCH 10/10] Add files via upload --- Labs/Lab.4/Lab_4_answers.ipynb | 768 +++++++++++++++++++++++++++++++++ 1 file changed, 768 insertions(+) create mode 100644 Labs/Lab.4/Lab_4_answers.ipynb diff --git a/Labs/Lab.4/Lab_4_answers.ipynb b/Labs/Lab.4/Lab_4_answers.ipynb new file mode 100644 index 0000000..b2e4277 --- /dev/null +++ b/Labs/Lab.4/Lab_4_answers.ipynb @@ -0,0 +1,768 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.0" + }, + "colab": { + "name": "Copy of Lab.4.ipynb", + "provenance": [] + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "YmMi2gPmcHdx" + }, + "source": [ + "## Lab 4\n", + "\n", + "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/UTA-DataScience/DATA1401.2020.Fall/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": "WQGY32RBcHdy", + "outputId": "6be3975c-7595-4426-ef33-2925a263616b", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "import random\n", + "x=random.random()\n", + "print(\"The Value of x is\", x)" + ], + "execution_count": 1, + "outputs": [ + { + "output_type": "stream", + "text": [ + "The Value of x is 0.3334715012458438\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "39VJS7jkcHd5" + }, + "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": "3G88bylccHd6" + }, + "source": [ + "# Skeleton\n", + "\n", + "def generate_uniform(N,x_min,x_max):\n", + " out = []\n", + " ### BEGIN SOLUTION\n", + "\n", + " import random\n", + " for i in range(0,1000):\n", + " x = random.uniform(-10,10)\n", + " out.append(x) \n", + " \n", + " ### END SOLUTION\n", + " return out" + ], + "execution_count": 99, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "uikjlwDLcHd9", + "outputId": "fa70f5c9-78b7-4af1-8553-6267251ee6ae", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 105 + } + }, + "source": [ + "# Test your solution here\n", + "data=generate_uniform(1000,-10,10)\n", + "print (\"Data Type:\", type(data))\n", + "print (\"Data Length:\", len(data))\n", + "if len(data)>0: \n", + " print (\"Type of Data Contents:\", type(data[0]))\n", + " print (\"Data Minimum:\", min(data))\n", + " print (\"Data Maximum:\", max(data))" + ], + "execution_count": 100, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Data Type: \n", + "Data Length: 1000\n", + "Type of Data Contents: \n", + "Data Minimum: -9.999264597574884\n", + "Data Maximum: 9.978259626083279\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ajfQHCWBcHeA" + }, + "source": [ + "*Exercise 2a:* \n", + "Write a function that computes the mean of values in a list." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "ZIjuc2cZcHeA" + }, + "source": [ + "# Skeleton\n", + "def mean(Data):\n", + " m=([4.2, 9, 3, 1, 41, 5.2, 4, 99, 3, 9])\n", + " \n", + " ### BEGIN SOLUTION\n", + "\n", + " return sum(m) / len(m) \n", + " \n", + " ### END SOLUTION\n", + " \n", + " return m" + ], + "execution_count": 81, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "s5MQVz9UcHeD", + "outputId": "6322ca7b-c18a-479e-f812-ed4df748e901", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "# Test your solution here\n", + "print (\"Mean of Data:\", mean(data))" + ], + "execution_count": 82, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Mean of Data: 17.84\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "L9fe9rlQcHeH" + }, + "source": [ + "*Exercise 2b:* \n", + "Write a function that computes the variance of values in a list." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "KyjZcaHBcHeI" + }, + "source": [ + "# Skeleton\n", + "def variance(Data):\n", + " m=([4.2, 9, 3, 1, 41, 5.2, 4, 99, 3, 9])\n", + "\n", + " \n", + " ### BEGIN SOLUTION\n", + "\n", + "\n", + " \n", + " return sum((i - (sum(m) / len(m))) ** 2 for i in m) / len(m) \n", + " \n", + "\n", + " ### END SOLUTION\n", + " \n", + " return m" + ], + "execution_count": 54, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "xFckYTKicHeN", + "outputId": "60cb6f89-301f-4d13-85a6-efb57fa10ce9", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "# Test your solution here\n", + "print (\"Variance of Data:\", variance(data))" + ], + "execution_count": 55, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Variance of Data: 854.1024\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "oM0noaZAcHeS" + }, + "source": [ + "## Histogramming" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "rqg6HA3FcHeT" + }, + "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": "81WjeIu8CjWd", + "outputId": "7627f47d-e03a-436f-ce8c-3b09adc7b176", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 300 + } + }, + "source": [ + "def histogram(x,n_bins=10,x_min=None,x_max=None):\n", + "\n", + " import matplotlib.pyplot as plt\n", + " import numpy as np\n", + " %matplotlib inline\n", + "\n", + " inp=input(\"enter values seperated by space: \")\n", + "\n", + " data = [float(x) for x in inp.split(\" \")]\n", + "\n", + " x_min = (min(data))\n", + " x_max = (max(data)) \n", + " n_bins = 10\n", + "\n", + "\n", + " plt.hist(data, 10, (x_min,x_max), rwidth=0.95, edgecolor='red', density=True)\n", + "\n", + " if x_min==None:\n", + " x_min=min(data)\n", + " if x_max==None:\n", + " x_max=max(data)\n", + "\n", + " bin_edges = np.arange(x_min,x_max,n_bins)\n", + "\n", + " hist=[0]*n_bins\n", + "\n", + " for d in data:\n", + " for i,(low_edge,high_edge) in enumerate(zip(bin_edges[:-1],bin_edges[1:])):\n", + " if d>=low_edge and d" + ] + }, + "metadata": { + "tags": [], + "needs_background": "light" + } + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "OK8puuQJcHed" + }, + "source": [ + "*Exercise 4:* Write a function that uses the histogram function in the previous exercise to create a text-based \"graph\". For example the output could look like the following:\n", + "```\n", + "[ 0, 1] : ######\n", + "[ 1, 2] : #####\n", + "[ 2, 3] : ######\n", + "[ 3, 4] : ####\n", + "[ 4, 5] : ####\n", + "[ 5, 6] : ######\n", + "[ 6, 7] : #####\n", + "[ 7, 8] : ######\n", + "[ 8, 9] : ####\n", + "[ 9, 10] : #####\n", + "```\n", + "\n", + "Where each line corresponds to a bin and the number of `#`'s are proportional to the value of the data in the bin. " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "UZNnzbPFcHee", + "outputId": "6378bcab-5855-40bd-cea1-5835fb6e9d5d", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 238 + } + }, + "source": [ + "# Solution\n", + "#def draw_histogram(x,n_bins,x_min=None,x_max=None,character=\"#\",max_character_per_line=20):\n", + " ### BEGIN SOLUTION\n", + "\n", + "import numpy as np\n", + "\n", + "def hist(a, bins=10, width=3):\n", + " h, b = np.histogram(a, bins)\n", + " \n", + " \n", + " for i in range (0, bins):\n", + " print('{:10f} | {:{width}s} {}'.format(\n", + " b[i], \n", + " '#'*int(width*h[i]/np.amax(h)), \n", + " h[i], \n", + " width=width))\n", + " print('{:10f} |'.format(b[bins]))\n", + " \n", + "hist(np.random.normal(size=10)) \n", + " ### END SOLUTION\n", + "\n", + " #return hist,bin_edges" + ], + "execution_count": 126, + "outputs": [ + { + "output_type": "error", + "ename": "TypeError", + "evalue": "ignored", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 17\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'{:10f} |'\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mbins\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 18\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 19\u001b[0;31m \u001b[0mhist\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msize\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 20\u001b[0m \u001b[0;31m### END SOLUTION\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 21\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mTypeError\u001b[0m: hist() got an unexpected keyword argument 'size'" + ] + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "mcnbaKnocHeg" + }, + "source": [ + "# Test your solution here\n", + "h,b=histogram(data,20)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "A4BJysR0cHej" + }, + "source": [ + "## Functional Programming\n", + "\n", + "*Exercise 5:* Write a function the applies a booling function (that returns true/false) to every element in data, and return a list of indices of elements where the result was true. Use this function to find the indices of entries greater than 0.5. " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "IggESAxvcHek", + "outputId": "663708ed-1ce7-435b-9a8f-45506549d94e", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 150 + } + }, + "source": [ + "#def where(mylist,myfunc):\n", + " #out= []\n", + " \n", + " ### BEGIN SOLUTION\n", + "\n", + "mylist = [.2, 68, 3, 9, .1]\n", + "\n", + "num = .5\n", + "\n", + "def check(mylist, num):\n", + "\n", + " for value in mylist:\n", + " ## if value less than num returns False\n", + " if value <= num:\n", + " return False\n", + "## if the following statement executes i.e., list contains values which are greater than given num\n", + " return True\n", + "print(check(mylist, num)) \n", + "### END SOLUTION\n", + "\n", + "return out\n", + "\n", + "#where(mylist,myfunc)" + ], + "execution_count": 139, + "outputs": [ + { + "output_type": "stream", + "text": [ + "False\n" + ], + "name": "stdout" + }, + { + "output_type": "error", + "ename": "SyntaxError", + "evalue": "ignored", + "traceback": [ + "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m21\u001b[0m\n\u001b[0;31m return out\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m 'return' outside function\n" + ] + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "VsRYzyi2cHeo" + }, + "source": [ + "# Test your solution here" + ], + "execution_count": 12, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "U10ObhiacHes" + }, + "source": [ + "*Exercise 6:* The inrange(mymin,mymax) function below returns a function that tests if it's input is between the specified values. Write corresponding functions that test:\n", + "* Even\n", + "* Odd\n", + "* Greater than\n", + "* Less than\n", + "* Equal\n", + "* Divisible by" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "rzJJrqUOcHet", + "outputId": "7709016f-3f91-45e1-ac6e-648c17470224", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 256 + } + }, + "source": [ + "def in_range(mymin,mymax):\n", + " def testrange(x):\n", + " return x=mymin\n", + " return testrange\n", + "\n", + "# Examples:\n", + "F1=in_range(0,10)\n", + "F2=in_range(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": 140, + "outputs": [ + { + "output_type": "stream", + "text": [ + "True True False False False\n", + "False False True True False\n" + ], + "name": "stdout" + }, + { + "output_type": "error", + "ename": "TypeError", + "evalue": "ignored", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 12\u001b[0m \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mF2\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mF2\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mF2\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mF2\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m15\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mF2\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m20\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 14\u001b[0;31m \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m\"Number of Entries passing F1:\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mwhere\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mF1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 15\u001b[0m \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m\"Number of Entries passing F2:\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mwhere\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mF2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mTypeError\u001b[0m: object of type 'bool' has no len()" + ] + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "cXWIFkY0fyFZ", + "outputId": "6c2fefed-9154-4ae4-d549-ca71e9d46af4", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 52 + } + }, + "source": [ + "num = int(input(\"Enter a number: \"))\n", + "\n", + "\n", + "if (num % 2) == 0:\n", + " print(\"{0} is Even\".format(num))\n", + "else:\n", + " print(\"{0} is Odd\".format(num))" + ], + "execution_count": 106, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Enter a number: 8\n", + "8 is Even\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "1FPK7_lzcHey" + }, + "source": [ + "# Test your solution" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "oD-Og_6ncHe2" + }, + "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": "UbFjvoREcHe3" + }, + "source": [ + "### BEGIN SOLUTION\n", + "\n", + " # Fill in your solution here \n", + " \n", + "### END SOLUTION" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "x9aqJMKEcHe6" + }, + "source": [ + "## Monte Carlo\n", + "\n", + "*Exercise 7:* Write a \"generator\" function called `generate_function(func,x_min,x_max,N)`, that instead of generating a flat distribution, generates a distribution with functional form coded in `func`. Note that `func` will always be > 0. \n", + "\n", + "Use the test function below and your histogramming functions above to demonstrate that your generator is working properly.\n", + "\n", + "Hint: A simple, but slow, solution is to a draw random number test_x within the specified range and another number p between the min and max of the function (which you will have to determine). If p<=function(test_x), then place test_x on the output. If not, repeat the process, drawing two new numbers. Repeat until you have the specified number of generated numbers, N. For this problem, it's OK to determine the min and max by numerically sampling the function. " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "MjfSpBJ3cHe7" + }, + "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": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "zuOlAEr7cHfC" + }, + "source": [ + "# A test function\n", + "def test_func(x,a=1,b=1):\n", + " return abs(a*x+b)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "oArzdXufcHfK" + }, + "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": "D3qRva1AcHfL" + }, + "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": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Zj0Uy_mUcHfO" + }, + "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": "eOReQzMTcHfP" + }, + "source": [ + "def integrate(func, x_min, x_max, n_points=1000):\n", + " \n", + " return integral" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "tzREpdnJcHfc" + }, + "source": [ + "" + ], + "execution_count": null, + "outputs": [] + } + ] +} \ No newline at end of file