{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Problem 1\n", "\n", "Build a BlockGrid of size 10x10, and then combine 'for' and 'if' loops to make three lists:\n", "\n", "one list, 'bottom', containing the 10 cells of the last row;\n", "one list, 'right', containing the 10 cells of the last column;\n", "one list, 'even_diagonal', containing the 5 cells down the diagonal spaced one apart." ] }, { "cell_type": "code", "collapsed": false, "input": [ "from ipythonblocks import BlockGrid\n", "grid = BlockGrid(10, 10, fill=(123, 234, 123))\n", "grid" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
" ], "metadata": {}, "output_type": "pyout", "prompt_number": 29, "text": [ "" ] } ], "prompt_number": 29 }, { "cell_type": "code", "collapsed": false, "input": [ "bottom = []\n", "for i in range(10):\n", " bottom.append(grid[9,i])\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 30 }, { "cell_type": "code", "collapsed": false, "input": [ "right = []\n", "for i in range(10):\n", " right.append(grid[i,9])" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 31 }, { "cell_type": "code", "collapsed": false, "input": [ "even_diagonal = []\n", "for i in range(5):\n", " even_diagonal.append(grid[2*i,2*i])" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 32 }, { "cell_type": "code", "collapsed": false, "input": [ "# now, color 'em\n", "for item in bottom:\n", " item.set_colors(255, 0, 0)\n", "\n", "for item in right:\n", " item.set_colors(0, 255, 0)\n", " \n", "for item in even_diagonal:\n", " item.set_colors(0, 0, 255)\n", " \n", "grid" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
" ], "metadata": {}, "output_type": "pyout", "prompt_number": 33, "text": [ "" ] } ], "prompt_number": 33 }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Problem 2\n", "\n", "First, create a new grid (10x10).\n", "\n", "Then, create a single dictionary 'd' with three keys, 'bottom', 'right', and 'even_diagonal'.\n", "Assign the lists you created above as values associated with each key.\n", "\n", "Finally, retrieve each of those lists and use them in a for loop to color each cell in each list blue." ] }, { "cell_type": "code", "collapsed": false, "input": [ "from ipythonblocks import BlockGrid\n", "grid = BlockGrid(10, 10, fill=(123, 234, 123))\n", "grid" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
" ], "metadata": {}, "output_type": "pyout", "prompt_number": 38, "text": [ "" ] } ], "prompt_number": 38 }, { "cell_type": "code", "collapsed": false, "input": [ "d = {}\n", "\n", "bottom = []\n", "for i in range(10):\n", " bottom.append(grid[9,i])\n", "\n", "d['bottom'] = bottom\n", "\n", "right = []\n", "for i in range(10):\n", " right.append(grid[i,9])\n", "\n", "d['right'] = right\n", " \n", "even_diagonal = []\n", "for i in range(5):\n", " even_diagonal.append(grid[2*i,2*i])\n", "d['even_diagonal'] = even_diagonal" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 39 }, { "cell_type": "code", "collapsed": false, "input": [ "for item in d['bottom']:\n", " item.set_colors(0, 0, 255)\n", "\n", "for item in d['right']:\n", " item.set_colors(0, 0, 255)\n", " \n", "for item in d['even_diagonal']:\n", " item.set_colors(0, 0, 255)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 40 }, { "cell_type": "code", "collapsed": false, "input": [ "grid" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
" ], "metadata": {}, "output_type": "pyout", "prompt_number": 41, "text": [ "" ] } ], "prompt_number": 41 }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Problem 3\n", "\n", "Build a BlockGrid of size 10x10, and then write a function\n", "that returns a list containing all of the diagonal cells of that grid. Make the function work for any\n", "size grid. Then, for the grid you created, make all of the cells in the diagonal red.\n", "\n", "Note, 'for i in range(grid.width)' will make 'i' go from 0 to the size of the grid." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def get_diagonal_cells(grid):\n", " diagonal = []\n", " # code goes here <--\n", " return diagonal" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# define grid, and then\n", "# 'diag = get_diagonal_cells(grid)' to call the function.\n", "\n" ], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }