|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "#Iterators and Generators Homework - Solution\n", |
| 8 | + "\n", |
| 9 | + "###Problem 1\n", |
| 10 | + "\n", |
| 11 | + "Create a generator that generates the squares of numbers up to some number N." |
| 12 | + ] |
| 13 | + }, |
| 14 | + { |
| 15 | + "cell_type": "code", |
| 16 | + "execution_count": 10, |
| 17 | + "metadata": { |
| 18 | + "collapsed": true |
| 19 | + }, |
| 20 | + "outputs": [], |
| 21 | + "source": [ |
| 22 | + "def gensquares(N):\n", |
| 23 | + " for i in range(N):\n", |
| 24 | + " yield i ** 2" |
| 25 | + ] |
| 26 | + }, |
| 27 | + { |
| 28 | + "cell_type": "code", |
| 29 | + "execution_count": 11, |
| 30 | + "metadata": { |
| 31 | + "collapsed": false |
| 32 | + }, |
| 33 | + "outputs": [ |
| 34 | + { |
| 35 | + "name": "stdout", |
| 36 | + "output_type": "stream", |
| 37 | + "text": [ |
| 38 | + "0\n", |
| 39 | + "1\n", |
| 40 | + "4\n", |
| 41 | + "9\n", |
| 42 | + "16\n", |
| 43 | + "25\n", |
| 44 | + "36\n", |
| 45 | + "49\n", |
| 46 | + "64\n", |
| 47 | + "81\n" |
| 48 | + ] |
| 49 | + } |
| 50 | + ], |
| 51 | + "source": [ |
| 52 | + "for x in gensquares(10):\n", |
| 53 | + " print x" |
| 54 | + ] |
| 55 | + }, |
| 56 | + { |
| 57 | + "cell_type": "markdown", |
| 58 | + "metadata": {}, |
| 59 | + "source": [ |
| 60 | + "###Problem 2\n", |
| 61 | + "\n", |
| 62 | + "Create a generator that yields \"n\" random numbers between a low and high number (that are inputs). Note: Use the random library. For example:" |
| 63 | + ] |
| 64 | + }, |
| 65 | + { |
| 66 | + "cell_type": "code", |
| 67 | + "execution_count": 6, |
| 68 | + "metadata": { |
| 69 | + "collapsed": false |
| 70 | + }, |
| 71 | + "outputs": [ |
| 72 | + { |
| 73 | + "data": { |
| 74 | + "text/plain": [ |
| 75 | + "6" |
| 76 | + ] |
| 77 | + }, |
| 78 | + "execution_count": 6, |
| 79 | + "metadata": {}, |
| 80 | + "output_type": "execute_result" |
| 81 | + } |
| 82 | + ], |
| 83 | + "source": [ |
| 84 | + "import random\n", |
| 85 | + "\n", |
| 86 | + "random.randint(1,10)" |
| 87 | + ] |
| 88 | + }, |
| 89 | + { |
| 90 | + "cell_type": "code", |
| 91 | + "execution_count": 7, |
| 92 | + "metadata": { |
| 93 | + "collapsed": true |
| 94 | + }, |
| 95 | + "outputs": [], |
| 96 | + "source": [ |
| 97 | + "def rand_num(low,high,n):\n", |
| 98 | + " # returns 6 numbers between 1 and 40\n", |
| 99 | + " for i in range(n):\n", |
| 100 | + " yield random.randint(low, high)" |
| 101 | + ] |
| 102 | + }, |
| 103 | + { |
| 104 | + "cell_type": "code", |
| 105 | + "execution_count": 9, |
| 106 | + "metadata": { |
| 107 | + "collapsed": false |
| 108 | + }, |
| 109 | + "outputs": [ |
| 110 | + { |
| 111 | + "name": "stdout", |
| 112 | + "output_type": "stream", |
| 113 | + "text": [ |
| 114 | + "6\n", |
| 115 | + "7\n", |
| 116 | + "6\n", |
| 117 | + "9\n", |
| 118 | + "9\n", |
| 119 | + "4\n", |
| 120 | + "3\n", |
| 121 | + "7\n", |
| 122 | + "8\n", |
| 123 | + "1\n", |
| 124 | + "6\n", |
| 125 | + "1\n" |
| 126 | + ] |
| 127 | + } |
| 128 | + ], |
| 129 | + "source": [ |
| 130 | + "for num in rand_num(1,10,12):\n", |
| 131 | + " print num" |
| 132 | + ] |
| 133 | + }, |
| 134 | + { |
| 135 | + "cell_type": "markdown", |
| 136 | + "metadata": {}, |
| 137 | + "source": [ |
| 138 | + "###Problem 3\n", |
| 139 | + "\n", |
| 140 | + "Use the iter() function to convert the string below \n" |
| 141 | + ] |
| 142 | + }, |
| 143 | + { |
| 144 | + "cell_type": "code", |
| 145 | + "execution_count": 17, |
| 146 | + "metadata": { |
| 147 | + "collapsed": false |
| 148 | + }, |
| 149 | + "outputs": [ |
| 150 | + { |
| 151 | + "name": "stdout", |
| 152 | + "output_type": "stream", |
| 153 | + "text": [ |
| 154 | + "h\n" |
| 155 | + ] |
| 156 | + } |
| 157 | + ], |
| 158 | + "source": [ |
| 159 | + "s = 'hello'\n", |
| 160 | + "\n", |
| 161 | + "s = iter(s)\n", |
| 162 | + "\n", |
| 163 | + "print next(s)" |
| 164 | + ] |
| 165 | + }, |
| 166 | + { |
| 167 | + "cell_type": "markdown", |
| 168 | + "metadata": {}, |
| 169 | + "source": [ |
| 170 | + "###Problem 4\n", |
| 171 | + "Explain a use case for a generator using a yield statement where you would not want to use a normal function with a return statement.\n", |
| 172 | + "\n", |
| 173 | + "*If the output has the potential of taking up a large amount of memory and you only intend to iterate through it, you would want to use a generator. (Multiple answers are acceptable here!)*" |
| 174 | + ] |
| 175 | + }, |
| 176 | + { |
| 177 | + "cell_type": "markdown", |
| 178 | + "metadata": {}, |
| 179 | + "source": [ |
| 180 | + "### Extra Credit!\n", |
| 181 | + "Can you explain what bonus is in the code below? (Note: We never covered this in lecture!)" |
| 182 | + ] |
| 183 | + }, |
| 184 | + { |
| 185 | + "cell_type": "code", |
| 186 | + "execution_count": 18, |
| 187 | + "metadata": { |
| 188 | + "collapsed": false |
| 189 | + }, |
| 190 | + "outputs": [ |
| 191 | + { |
| 192 | + "name": "stdout", |
| 193 | + "output_type": "stream", |
| 194 | + "text": [ |
| 195 | + "4\n", |
| 196 | + "5\n" |
| 197 | + ] |
| 198 | + } |
| 199 | + ], |
| 200 | + "source": [ |
| 201 | + "my_list = [1,2,3,4,5]\n", |
| 202 | + "\n", |
| 203 | + "gencomp = (item for item in my_list if item > 3)\n", |
| 204 | + "\n", |
| 205 | + "for item in gencomp:\n", |
| 206 | + " print item" |
| 207 | + ] |
| 208 | + }, |
| 209 | + { |
| 210 | + "cell_type": "markdown", |
| 211 | + "metadata": {}, |
| 212 | + "source": [ |
| 213 | + "Hint google: generator comprehnsion is!\n", |
| 214 | + "\n", |
| 215 | + "#Great Job!" |
| 216 | + ] |
| 217 | + } |
| 218 | + ], |
| 219 | + "metadata": { |
| 220 | + "kernelspec": { |
| 221 | + "display_name": "Python 2", |
| 222 | + "language": "python", |
| 223 | + "name": "python2" |
| 224 | + }, |
| 225 | + "language_info": { |
| 226 | + "codemirror_mode": { |
| 227 | + "name": "ipython", |
| 228 | + "version": 2 |
| 229 | + }, |
| 230 | + "file_extension": ".py", |
| 231 | + "mimetype": "text/x-python", |
| 232 | + "name": "python", |
| 233 | + "nbconvert_exporter": "python", |
| 234 | + "pygments_lexer": "ipython2", |
| 235 | + "version": "2.7.10" |
| 236 | + } |
| 237 | + }, |
| 238 | + "nbformat": 4, |
| 239 | + "nbformat_minor": 0 |
| 240 | +} |
0 commit comments