{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "combine for and if loops from hw1 to make lists containing 10 cells/last row, last column, every other diagonal\n", "(hint: start with indiivudal statements)\n", "\n", "write function with a for loop that returns a list containign the diagonal cells of any grid. hint: for i in range(grid.width)\n", "verify that this function works by creating a 10x10 grid and setting all diagonal cells to white\n", "\n", "introduce +\n", "write a function that returns the sum of all the nubmers in a list. give osme test data." ] }, { "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": [ "bottom = []" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "right = []" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "even_diagonal = []" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "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)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 5 }, { "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": "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" ], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }