{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "#Iterators and Generators Homework - Solution\n", "\n", "###Problem 1\n", "\n", "Create a generator that generates the squares of numbers up to some number N." ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def gensquares(N):\n", " for i in range(N):\n", " yield i ** 2" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "4\n", "9\n", "16\n", "25\n", "36\n", "49\n", "64\n", "81\n" ] } ], "source": [ "for x in gensquares(10):\n", " print x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "###Problem 2\n", "\n", "Create a generator that yields \"n\" random numbers between a low and high number (that are inputs). Note: Use the random library. For example:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import random\n", "\n", "random.randint(1,10)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def rand_num(low,high,n):\n", " \n", " for i in range(n):\n", " yield random.randint(low, high)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6\n", "7\n", "6\n", "9\n", "9\n", "4\n", "3\n", "7\n", "8\n", "1\n", "6\n", "1\n" ] } ], "source": [ "for num in rand_num(1,10,12):\n", " print num" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "###Problem 3\n", "\n", "Use the iter() function to convert the string below \n" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "h\n" ] } ], "source": [ "s = 'hello'\n", "\n", "s = iter(s)\n", "\n", "print next(s)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "###Problem 4\n", "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", "\n", "**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!)**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Extra Credit!\n", "Can you explain what bonus is in the code below? (Note: We never covered this in lecture!)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n", "5\n" ] } ], "source": [ "my_list = [1,2,3,4,5]\n", "\n", "gencomp = (item for item in my_list if item > 3)\n", "\n", "for item in gencomp:\n", " print item" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Hint google: generator comprehension is!\n", "\n", "#Great Job!" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.10" } }, "nbformat": 4, "nbformat_minor": 0 }