{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Homework 1: Welcome to Python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " \n", "**Part I due at 11pm on Friday, January 11, 2019. Submit via Canvas.**\n", "\n", "**Part II due at 11pm on Wednesday, January 16, 2019. Submit via Canvas.**\n", "
\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Learning Objectives" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This homework is intended to provide you with introductions to both Python 3 and to Jupyter Notebooks. In particular, we hope you gain familiarity with the following:\n", "\n", "- writing Python code using variables and loops to solve simple mathematical problems\n", "- creating and using Jupyter Notebooks and running Python programs\n", "- printing results to match a specification exactly\n", "\n", "You will make use of all of these skills throughout the quarter.\n", "\n", "Python is an easy-to-use but powerful programming language that is particularly well-suited to quickly creating small programs." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Jupyter Notebooks" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Jupyter notebooks are being used in many labs as a way to carry out reproducible research. By now, you should already have read through the Jupyter Notebooks tutorial linked __[here](https://www.dataquest.io/blog/jupyter-notebook-tutorial/)__, and you should be reading this document in a Jupyter notebook of your own.\n", "\n", "Read the article linked __[here](https://arxiv.org/pdf/1810.08055.pdf)__ (also available for download from our Canvas website). You only need to skim the article for now, but it will be an especially useful resource when you are working on your team projects. For now, this article will just introduce you to some concepts that are worth keeping in mind as you progress through this class.\n", "\n", "The following links may also provide useful reference resources:\n", "- __[Jupyter Notebook Beginner Guide](https://jupyter-notebook-beginner-guide.readthedocs.io/en/latest/)__\n", "- __[Jupyter Notebook Markdown Cheatsheet](https://www.ibm.com/support/knowledgecenter/SSQNUZ_current/com.ibm.icpdata.doc/dsx/markd-jupyter.html)__" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Installation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The recommended way to obtain Python 3 and access to Jupyter Notebooks is by installing __[Anaconda](https://www.anaconda.com/)__. Select the Anaconda Distribution download. There are options for Windows, Mac, and Linux. You will want to install the Python 3.7 version." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Running Python Code" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python files typically end with the suffix .py -- these files can be fun from within a Jupiter Notebook. To see how this works, follow the steps listed below:\n", "\n", "1. Select: File --> Open... --> New (upper right hand corner) --> Text File\n", "2. Enter the following text in the empty document: print(\"Hello World!\")\n", "3. Select: File --> Rename \n", "4. Change the name to \"hello.py\" and save \n", "5. Run the code by using the following command: !python hello.py (this code is already in the next cell for you)\n", "6. If the words \"Hello World!\" appear below the cell when you run the code, everything is working as it should!\n", "\n", "**NOTE:** Since you saved the file from within the notebook, you will not need to specify an exact path. You should now also be able to see hello.py in your list of Jupyter files (on the Jupyter \"Home\" page - localhost:8888/tree)." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World!\r\n" ] } ], "source": [ "!python hello.py" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also run python code directly within a code cell in a Jupyter notebook.\n", "\n", "In the cell below, you should see the command: print(\"Hello World!\"). When you run this cell, you should get the same result as previously." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World!\n" ] } ], "source": [ "print(\"Hello World!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Part I: Personal Introduction - Due January 11" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Please complete the following task and submit the .ipynb file via the Canvas website.\n", "\n", "1. Create a new Jupyter Notebook titled **Personal_Intro_Lastname_Firstname.ipynb** (replace the last two parts of the title with your first and last names).\n", "2. Use one or more markdown cells to create a personal introduction telling me a bit about yourself and what you hope to gain from taking this class -- you can model your introduction after the one I presented in class (also provided in Jupyter Notebook format on the class Canvas site).\n", "3. Please include at least one recent photo of yourself in your personal introduction." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Part II: Programming Exercises - Due January 16" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Set Up" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Download the file hw1.py**\n", "\n", "You can open the file from the jupyter localhost page from wherever you have downloaded it to (File --> Open... --> navigate to the file location).\n", "\n", "**Tip:** To download, right click this link and select \"Save link as\" or if you click through to see the file in your browser, just right click and select \"Save as\" or \"Save Page as\" or whatever wording your browser uses.\n", "\n", "**NOTE:** If you don't want to provide the path to the file, you will need to save the file wherever your Jupyter Notebook files are saved. \n", "\n", "I prefer having my class files stored in a separate location, so to run this hw1.py file I need to use the follwing:\n", "\n", "!python ~/Documents/CSE_Winter_2019/CSE_160_W19/Assignments/new_assignments/hw1.py\n", "\n", "In the cell below, replace \"file_path\" with the actual path for your file system, or, if you have saved the file in the same location your Jupyter Notebook saves to by default, just use !python hw1.py" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "python: can't open file '/Users/emiliagan/file_path/hw1.py': [Errno 2] No such file or directory\r\n" ] } ], "source": [ "!python ~/file_path/hw1.py" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If everything works correctly, you should see the same output as after running the following command:\n" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Problem 1 solution follows:\n", "Problem 2 solution follows:\n", "Problem 3 solution follows:\n", "Problem 4 solution follows:\n", "Problem 5 solution follows:\n", "Problem 6 solution follows:\n" ] } ], "source": [ "print(\n", "\"Problem 1 solution follows:\\n\" +\n", "\"Problem 2 solution follows:\\n\" +\n", "\"Problem 3 solution follows:\\n\" +\n", "\"Problem 4 solution follows:\\n\" +\n", "\"Problem 5 solution follows:\\n\" + \n", "\"Problem 6 solution follows:\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Formatting Your Output" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For problems in this homework, you will edit your local copy of the hw1.py file. You may do your work by editing and running hw1.py (we recommend this approach), or by typing code into Jupyter cells (below) and copying it into the appropriate part of hw1.py when you are satisfied with it. When you have completed this homework, running the hw1.py file should compute the answers to all the problems, and print the following output (with the dashes (---) replaced by the values you calculated). Please make your output match the following exactly.\n", "\n", "**TO BE CLEAR:** You will not be copying and pasting output into the hw1.py file, nor will you be using print statements to display the correct responses. Instead, you will be pasting the **CODE** that **produces the output in the CORRECT format** for each problem." ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Problem 1 solution follows:\n", "Root 1: ---\n", "Root 2: ---\n", "\n", "Problem 2 solution follows:\n", "1/2: ---\n", "1/3: ---\n", "1/4: ---\n", "1/5: ---\n", "1/6: ---\n", "1/7: ---\n", "1/8: ---\n", "1/9: ---\n", "1/10: ---\n", "\n", "Problem 3 solution follows:\n", "Triangular number 10 via loop: ---\n", "Triangular number 10 via formula: ---\n", "\n", "Problem 4 solution follows:\n", "10!: ---\n", "\n", "Problem 5 solution follows:\n", "10!: ---\n", "9!: ---\n", "8!: ---\n", "7!: ---\n", "6!: ---\n", "5!: ---\n", "4!: ---\n", "3!: ---\n", "2!: ---\n", "1!: ---\n", "\n", "Problem 6 solution follows:\n", "e: ---\n", "\n" ] } ], "source": [ "# Running this cell will show you the format required for your final output when hw1.py is run.\n", "\n", "print(\"Problem 1 solution follows:\\n\" +\n", "\"Root 1: ---\\n\" + \n", "\"Root 2: ---\\n\")\n", "\n", "print(\"Problem 2 solution follows:\\n\" +\n", "\"1/2: ---\\n\" +\n", "\"1/3: ---\\n\" +\n", "\"1/4: ---\\n\" +\n", "\"1/5: ---\\n\" +\n", "\"1/6: ---\\n\" +\n", "\"1/7: ---\\n\" +\n", "\"1/8: ---\\n\" +\n", "\"1/9: ---\\n\" +\n", "\"1/10: ---\\n\")\n", "\n", "print(\"Problem 3 solution follows:\\n\" +\n", "\"Triangular number 10 via loop: ---\\n\" +\n", "\"Triangular number 10 via formula: ---\\n\")\n", "\n", "print(\"Problem 4 solution follows:\\n\" +\n", "\"10!: ---\\n\")\n", "\n", "print(\"Problem 5 solution follows:\\n\" +\n", "\"10!: ---\\n\" +\n", "\"9!: ---\\n\" +\n", "\"8!: ---\\n\" +\n", "\"7!: ---\\n\" +\n", "\"6!: ---\\n\" +\n", "\"5!: ---\\n\" +\n", "\"4!: ---\\n\" +\n", "\"3!: ---\\n\" +\n", "\"2!: ---\\n\" +\n", "\"1!: ---\\n\")\n", "\n", "print(\"Problem 6 solution follows:\\n\" +\n", "\"e: ---\\n\")\n", "\n", "# Don't forget to include documentation (source code comments, on lines starting with #) \n", "# as appropriate in file hw1.py.\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Tips for Formatting Output**\n", "\n", "Let's say you're trying to print the following line of output:\n", "\n", "1 plus 1 equals 2\n", "\n", "The Python code in the code cells below show two ways that will do that.\n", "\n", "**NOTE:** You can print a blank line by calling just print (and giving it nothing to print). You can print a new line using \"\\n\".\n" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 plus 1 equals 2\n" ] } ], "source": [ "# In this case, I'm explicitly inserting spaces when I want them.\n", "n = 1\n", "print(str(n) + \" plus \" + str(n) + \" equals \" + str(n + n))" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 plus 1 equals 2\n" ] } ], "source": [ "# In this case, the commas add spaces for me automatically.\n", "n = 1\n", "print(str(n), \"plus\", str(n), \"equals\", str(n + n))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 1: Roots" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Compute and print both roots of the quadratic equation with the smaller root first: $3x^2-5.86x+ 2.5408$.\n", "\n", "**Hint:** recall that the roots of a quadratic equation $ax^2+bx+c$ are $x=\\frac{-b\\pm\\sqrt{b^2-4ac}}{2a}$.\n", "\n", "**Hint:** use the *math.sqrt* function to compute the square root. (Remember, you need to first do \"import math\". \n", "\n", "This already appears in the hw1.py file that you will be editing, so everything is already set up for you if you are working within that file.)" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [], "source": [ "# Code cell for testing Problem 1 code." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 2: Reciprocals" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use a for loop to print the decimal representations of $\\frac{1}{2}$, $\\frac{1}{3}$, ..., $\\frac{1}{10}$, one on each line.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Code cell for testing Problem 2 code." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 3: Triangular numbers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use a for loop to compute the 10th triangular number. The nth triangular number is defined as $1+2+3 + ... + n$. (You can also compute the nth triangular number as $n*(n+1)/2$. Use this formula to double-check that your loop is correct.)\n", "\n", "**Hint:** This outline is an almost-complete solution. You only have to replace each ellipsis by an expression.\n", "\n", "```n = 10\n", "triangular = 0\n", "for i in ...:\n", " triangular = ...\n", "print (\"Triangular number\", n, \"via loop:\", triangular)\n", "print (\"Triangular number\", n, \"via formula:\", n * (n + 1) / 2)```\n", "\n", "Your code should correctly calculate the 11th, 12th, or any other triangular number just by changing the first line to set n to 11, 12, or any other number. Using the **range function** should help you accomplish this.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Code cell for testing Problem 3 code." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 4: Factorial" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use a for loop to compute 10!, the factorial of 10. Recall that the factorial of n is $1*2*3*...*n$. \n", "\n", "[Note: you may not use the math.factorial() function for this problem or any other problem in this homework. You should also NOT use recursion. (If you do not know what that means, do not worry because you should not use it :-)]\n", "\n", "**Hint:** Your answer will be similar to your answer to \"Problem 3: Triangular numbers\".\n", "\n", "As in Problem 3, your code should calculate 11!, 12!, or any other number's factorial just by changing the first line to set n to 11, 12, or any other number." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Code cell for testing Problem 4 code." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 5: Multiple factorials" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write code to print the first 10 factorials, in reverse order. In other words, write code that prints 10!, then prints 9!, then prints 8!, ..., then prints 1!. Its literal output will be:\n", "\n", "```\n", "10!: 3628800\n", "9!: 362880\n", "8!: 40320\n", "7!: 5040\n", "6!: 720\n", "5!: 120\n", "4!: 24\n", "3!: 6\n", "2!: 2\n", "1!: 1```\n", "The first line of your solution should assign a variable numlines to 10. Then, as in Problems 3 and 4, the rest of the code should print the correct number of lines and correct factorial on each line for values other than 10 just by setting numlines to a different value.\n", "\n", "**Hint:** Use two nested for loops.\n", "The outer loop sets the value of n to the values numlines, numlines-1, numlines-2, ..., 1, in succession.\n", "Then, the body of that loop is itself a loop — exactly your solution to \"Problem 4: Factorial\", without the first line n = 10 that hard-codes the value of n." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Code cell for testing Problem 5 code." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 6: Sums of reciprocals of factorials" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Compute the following value:\n", " 1 + 1/1! + 1/2! + 1/3! + 1/4! + ... + 1/10!\n", "The value should be close to e (≈ 2.71828), the base of the natural logarithms.\n", "\n", "**Hint:** The easiest way to solve this is with two nested for loops. It is possible, but tricky, to compute this using only one for loop. That is not necessary for this assignment.\n", "\n", "**Hint:** Copy your solution to \"Problem 5: Multiple factorials\", then modify it. Rather than printing the factorials, you will add their reciprocals to a running total, then print that total at the end.\n", "\n", "**Hint:** don't try to work the very first \"1 +\" into your loop; do it outside the loops (either at the very beginning or the very end of the outer loop)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Code cell for testing Problem 5 code." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# SUGGESTED: Code cell for testing your ENTIRE HW1 Part 2 code (i.e running your hw1.py file)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Submit your work" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**You are almost done!**\n", "\n", "Double-check that running your entire file produces the correct output for each problem in the assignment, and that you have removed any extraneous print statements (which you may have used for debugging, for example). \n", "Specifically, compare against our sample output listed above.\n", "At the bottom of your hw1.py file, state which students or other people (besides the course staff) helped you with the assignment, or that no one did.\n", "\n", "**Submit the following file via Canvas.**\n", "- hw1.py (this file will be used by the autograder)\n", "\n", "**You do not have to submit your Jupyter Notebook for Part II of this assignment.**\n", "\n", "\n", "Answer a brief REQUIRED survey asking how much time you spent and other reflections on this assignment.\n", "Now you're done!" ] } ], "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.7.1" } }, "nbformat": 4, "nbformat_minor": 2 }