{ "metadata": { "name": "", "signature": "sha256:518873fef514db9a2bd8b6828593b1228a31558117bfbe91860f641eb3f4b0cf" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "#Homework assignment 1\n", "\n", "Your homework assignment takes the form of an IPython Notebook. To work on the assignment, you'll need to follow these steps: \n", "\n", "(1) Click [here](https://raw.githubusercontent.com/ledeprogram/courses/master/databases/Data%20and%20Databases%20Homework%20Assignment%201.ipynb) to download a copy of this file (with extension `.ipynb`).\n", "\n", "(2) Upload the copy that you just got from the server to your own IPython Notebook server. Here's how to do that:\n", "\n", "(2a) First, make sure your IPython Notebook server is active (check the EC2 admin panel). Open a window to that server and make sure you're on the \"Notebooks\" screen.\n", "\n", "(2b) Find the `.ipynb` file that you just downloaded on your computer, open a window to your IPython Notebook server, and drag it into the file list.\n", "\n", "(2c) You've now got a copy of the assignment where you can work on it. Congratulations!\n", "\n", "(2d) Make sure to change the name of your copy of the homework assignment to include your own name (e.g., change it to \"Your Name - Data and Databases Homework Assignment 1\").\n", "\n", "(3) There are a number of problem sets below that require you to write code to achieve a goal. I've left code cells incomplete blank after each question. Your job is to fill in the code cells so that running the code produces the requested output. Make sure to use the \"Run\" command to ensure that your code works and gives accurate results. Feel free to insert Markdown cells if you have any notes or explanations regarding your code. You must complete all of the problem sets!\n", "\n", "(4) When you're done with your assignment, you need to submit it. Go to `File > Download as...` and click `IPython Notebook`. A `.ipynb` will be downloaded to your computer. E-mail that file as an attachment to `dbv2107@columbia.edu`. (We may change the procedure for submitting homework assignments in the future, as you become more familiar with Github.)\n", "\n", "This homework assignment is due on June 3rd, 2014." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##Problem set 1: Simple indexing and list functions\n", "\n", "There is a list of numbers assigned to a variable `number_list` in the code below that has been assigned a list of numbers. Run this cell before filling in the answers below." ] }, { "cell_type": "code", "collapsed": false, "input": [ "number_list = [-13, 5, 12, 17, 20, 0]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write another expression in the cell below, using square bracket index notation, that causes the 4th element of `number_list` (i.e., the number `17`) to be displayed when running the code in the cell." ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now write an expression that evaluates to the number of items in the list (i.e., `6`), using the `len()` function." ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "The following expression:\n", "\n", " print max(number_list)\n", " \n", "... will print the largest value in `number_list` (i.e., `20`). Change the variable `index` in the code below so that the expression\n", "\n", " print sorted(number_list)[x]\n", " \n", "... does the same thing. (i.e., when you run the cell, it should display `20`.)" ] }, { "cell_type": "code", "collapsed": true, "input": [ "x = ?\n", "print sorted(number_list)[x]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##Problem set 2: Slices\n", "\n", "Write an expression below that evaluates to a slice of `number_list` starting with its second element and ending with its fifth element (exclusive). I.e., the expression should evaluate to `[5, 12, 17]`." ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write an expression below that evaluates to a slice of `number_list` starting with its third element and ending at the end of the list. I.e., the expression should evaluate to `[12, 17, 20, 0]`." ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 13 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally, fill in a value for the variable `x` below so that the expression below it evaluates to a slice of `number_list` starting at the second-to-last element of the list and ending at the end of the list. The expression should evaluate to `[20, 0]`. `x` should be a negative integer." ] }, { "cell_type": "code", "collapsed": false, "input": [ "x = ?\n", "print number_list[x:]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##Problem set 3: Comprehensions\n", "\n", "For this problem set, I'm introducing a new Python operator: the modulo operator, `%`. This operator returns the *remainder* of dividing one integer by another. For example:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print 22 % 3" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1\n" ] } ], "prompt_number": 15 }, { "cell_type": "markdown", "metadata": {}, "source": [ "This expression evaluates to `1` because the remainder of dividing `22` by `3` is `1`. We can use the modulo operator to test whether or not a number is even, by using the number `2` on the right side of the operator:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print 100 % 2\n", "print 101 % 2" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0\n", "1\n" ] } ], "prompt_number": 16 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Given the above information, write a list comprehension that evaluates to a list containing *only* the members of `number_list` that are *odd*---i.e., `[-13, 5, 17]`. Use the modulo operator in the membership expression of the list comprehension." ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 18 }, { "cell_type": "markdown", "metadata": {}, "source": [ "##Problem set 4: Splitting strings\n", "\n", "In the cell below, a variable `float_str` is set to a string containing a list of floating-point numbers, separated by semicolons (`;`). (Make sure to run this cell before you proceed, so that the variable will be available in subsequent cells.)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "float_str = \"5.8;6.9;3.1;5.9;6.6;6.5;6.5;5.6;6;6.4;3.32;6.0;6.0;6.3;6.6;6.6\"" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 17 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write an expression below that converts this string into a list of floating-point numbers. The type of the expression should be `list` and the type of individual elements in the list should be `float`. The expression should evaluate to something that looks like this:\n", "\n", " [5.8, 6.9, 3.1, 5.9, 6.6, 6.5, 6.5, 5.6, 6.0, 6.4, 3.32, 6.0, 6.0, 6.3, 6.6, 6.6]\n", " \n", "(Hint: you'll need to use the `.split()` method.)" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 21 }, { "cell_type": "markdown", "metadata": {}, "source": [ "##Problem set 5: LeBron James\n", "\n", "Write a code snippet below that displays LeBron James' average number of assists per game in the 2013-2014 regular season. Feel free to cut and paste from the course notes as appropriate to get the boilerplate code for loading the CSV file." ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }