{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": [ "from ipythonblocks import BlockGrid" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "grid = BlockGrid(10, 10, fill=(123, 234, 123))\n", "grid" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
" ], "metadata": {}, "output_type": "pyout", "prompt_number": 6, "text": [ "" ] } ], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [ "WHITE = (255, 255, 255)\n", "RED = (255, 0, 0)\n", "GREEN = (0, 255, 0)\n", "BLUE = (0, 0, 255)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Problem 1\n", "\n", "Modify the next cell to contain two for loops. The first for loop should set the last row to red. The second for loop should set the last column to white." ] }, { "cell_type": "code", "collapsed": false, "input": [ "grid = BlockGrid(10, 10, fill=(123, 234, 123))\n", "\n", "for i in range(10):\n", " grid[9, i] = RED\n", "for i in range(10):\n", " grid[i, 9] = WHITE\n", "\n", "grid" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
" ], "metadata": {}, "output_type": "pyout", "prompt_number": 9, "text": [ "" ] } ], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [ "# Note -- you could do this in a single loop pretty easily:\n", "grid = BlockGrid(10, 10, fill=(123, 234, 123))\n", "\n", "for i in range(10):\n", " grid[9, i] = RED\n", " grid[i, 9] = WHITE\n", "\n", "grid" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
" ], "metadata": {}, "output_type": "pyout", "prompt_number": 10, "text": [ "" ] } ], "prompt_number": 10 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Problem 2\n", "\n", "Write a for loop so that every other cell along the diagonal is white. Your final grid should look like the following, BUT with using a for loop instead.\n", "\n", "Hint: you want to color on an even set of numbers. You can either multiple i by 2 (i`*`2) or write an 'if' statement to test if i % 2 == 0." ] }, { "cell_type": "code", "collapsed": false, "input": [ "grid = BlockGrid(10, 10, fill=(123, 234, 123))\n", "grid[0, 0] = WHITE\n", "grid[2, 2] = WHITE\n", "grid[4, 4] = WHITE\n", "grid[6, 6] = WHITE\n", "grid[8, 8] = WHITE\n", "grid" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
" ], "metadata": {}, "output_type": "pyout", "prompt_number": 11, "text": [ "" ] } ], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": [ "# one way to do this is to use an if statement:\n", "grid = BlockGrid(10, 10, fill=(123, 234, 123))\n", "\n", "for i in range(10):\n", " if i % 2 == 0: # if even\n", " grid[i, i] = WHITE\n", "\n", "grid" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
" ], "metadata": {}, "output_type": "pyout", "prompt_number": 13, "text": [ "" ] } ], "prompt_number": 13 }, { "cell_type": "code", "collapsed": false, "input": [ "# you can also do this by multiplying by 2:\n", "grid = BlockGrid(10, 10, fill=(123, 234, 123))\n", "\n", "for i in range(5): # <-- only go up to 5\n", " j = i*2\n", " grid[j, j] = WHITE\n", "\n", "grid" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
" ], "metadata": {}, "output_type": "pyout", "prompt_number": 14, "text": [ "" ] } ], "prompt_number": 14 }, { "cell_type": "code", "collapsed": false, "input": [ "# or, if you go read up on the range function, you can have the range function do the counting for you!\n", "grid = BlockGrid(10, 10, fill=(123, 234, 123))\n", "\n", "for i in range(0, 10, 2): # <-- step by 2\n", " grid[i, i] = WHITE\n", "\n", "grid" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
" ], "metadata": {}, "output_type": "pyout", "prompt_number": 15, "text": [ "" ] } ], "prompt_number": 15 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Problem 3\n", "\n", "Write a nested for loop so that you color each row progressively more red, starting at black. (See definition of RED above.) Your final grid should look like the below, but with rows getting redder instead of columns." ] }, { "cell_type": "code", "collapsed": false, "input": [ "grid = BlockGrid(10, 10, fill=(123, 234, 123))\n", "grid[:,0] = (0, 0, 0)\n", "grid[:,1] = (10, 0, 0)\n", "grid[:,2] = (20, 0, 0)\n", "grid[:,3] = (30, 0, 0)\n", "grid[:,4] = (40, 0, 0)\n", "grid[:,5] = (50, 0, 0)\n", "grid[:,6] = (60, 0, 0)\n", "grid[:,7] = (70, 0, 0)\n", "grid[:,8] = (80, 0, 0)\n", "grid[:,9] = (90, 0, 0)\n", "grid" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
" ], "metadata": {}, "output_type": "pyout", "prompt_number": 16, "text": [ "" ] } ], "prompt_number": 16 }, { "cell_type": "code", "collapsed": false, "input": [ "# probably the simplest way to do it is like so:\n", "grid = BlockGrid(10, 10, fill=(123, 234, 123))\n", "\n", "for i in range(10):\n", " grid[i, :] = (i*10, 0, 0)\n", "grid" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
" ], "metadata": {}, "output_type": "pyout", "prompt_number": 17, "text": [ "" ] } ], "prompt_number": 17 }, { "cell_type": "code", "collapsed": false, "input": [ "# but, there are many, many ways to skin a cat:\n", "grid = BlockGrid(10, 10, fill=(123, 234, 123))\n", "\n", "for i in range(0, 100, 10):\n", " grid[i / 10, :] = (i, 0, 0)\n", "grid" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
" ], "metadata": {}, "output_type": "pyout", "prompt_number": 18, "text": [ "" ] } ], "prompt_number": 18 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }