{ "cells": [ { "cell_type": "markdown", "id": "tribal-chrome", "metadata": {}, "source": [ "# Iterators and Generators (21/4 - 2021)" ] }, { "cell_type": "markdown", "id": "faced-cornell", "metadata": {}, "source": [ "## Exercise\n", "\n", "Create an iterable class ``arange(start, end, step)`` to return values start, start + step, start + 2 * step, ..." ] }, { "cell_type": "code", "execution_count": 44, "id": "strange-piano", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "L = [1, 2, 3, 4] # iterabel\n", "# ^\n", "# | iterator\n", "\n", "for x in L:\n", " print(x)" ] }, { "cell_type": "code", "execution_count": 45, "id": "trying-watershed", "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n" ] } ], "source": [ "L = [1,2,3]\n", "\n", "print(type(L))\n", "print(type(iter(L)))" ] }, { "cell_type": "code", "execution_count": 14, "id": "blocked-cycling", "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a\n", "b\n", "c\n", "d\n" ] }, { "ename": "StopIteration", "evalue": "", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mStopIteration\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnext\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mit\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnext\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mit\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 7\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnext\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mit\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mStopIteration\u001b[0m: " ] } ], "source": [ "L = ['a', 'b', 'c', 'd'] # iterabel\n", "it = iter(L)\n", "print(next(it))\n", "print(next(it))\n", "print(next(it))\n", "print(next(it))\n", "print(next(it))" ] }, { "cell_type": "code", "execution_count": 16, "id": "unlimited-sarah", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "L = ['a', 'b', 'c', 'd'] # iterabel\n", "print(L.__iter__())" ] }, { "cell_type": "code", "execution_count": 62, "id": "welsh-steps", "metadata": {}, "outputs": [], "source": [ "class arange: # iterabel\n", " def __init__(self, start, end, step):\n", " self.start = start\n", " self.end = end\n", " self.step = step\n", " \n", " def __iter__(self): # skal returnere en iterator\n", " return arange_iterator(self)\n", " \n", " def __repr__(self):\n", " return f'arange({self.start}, {self.end}, {self.step})'\n", " \n", "class arange_iterator:\n", " def __init__(self, arange_object):\n", " self.arange_object = arange_object\n", " self.value = self.arange_object.start\n", " \n", " def __next__(self):\n", " if self.value >= self.arange_object.end:\n", " raise StopIteration\n", " answer = self.value\n", " self.value += self.arange_object.step\n", " return answer" ] }, { "cell_type": "code", "execution_count": 46, "id": "capital-scanner", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n", "10.3\n", "10.600000000000001\n", "10.900000000000002\n", "11.200000000000003\n", "11.500000000000004\n", "11.800000000000004\n" ] } ], "source": [ "#r = arange(10, 20, .3)\n", "r = arange(10, float('inf'), .3)\n", "\n", "#it = iter(r)\n", "#print(next(it))\n", "#print(next(it))\n", "#print(next(it))\n", "#print(next(it))\n", "#print(next(it))\n", "\n", "for i, x in enumerate(r):\n", " print(x)\n", " if i > 5:\n", " break" ] }, { "cell_type": "code", "execution_count": 65, "id": "acoustic-violence", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "arange(3, 5, 0.25)" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "arange(3, 5, 0.25)" ] }, { "cell_type": "code", "execution_count": 69, "id": "distinct-notification", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[9, 10.5625, 12.25, 14.0625, 16.0, 18.0625, 20.25, 22.5625]" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[x ** 2 for x in arange(3, 5, 0.25)]" ] }, { "cell_type": "markdown", "id": "horizontal-offset", "metadata": {}, "source": [ "## Exercise\n", "\n", "Generator expression to create powers of two." ] }, { "cell_type": "code", "execution_count": 86, "id": "complex-infrastructure", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0, 1, 4, 9, 16]\n" ] } ], "source": [ "g = (x ** 2 for x in range(5))\n", "print(list(g))\n", "\n", "#h = (x + 1 for x in g)\n", "\n", "#print(list(h))\n", "\n", "#it = iter(g)\n", "#print(next(it))\n", "#print(next(it))\n", "#print(next(it))\n", "#print(next(it))\n", "#print(next(it))\n", "#print(next(it))\n", "#print(next(it))\n", "#print(next(it))\n", "\n", "#print(type(g))\n", "#for x in g:\n", "# print(x)\n", "\n", "#print(sum(g))" ] }, { "cell_type": "code", "execution_count": 93, "id": "illegal-minister", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1023\n", "1023\n", "1023\n" ] } ], "source": [ "powers_of_two = (2 ** i for i in range(10))\n", "\n", "print(sum(powers_of_two))\n", "print(sum( (2 ** i for i in range(10)) ))\n", "print(sum(2 ** i for i in range(10)))" ] }, { "cell_type": "markdown", "id": "sized-bulgaria", "metadata": {}, "source": [ "## Exercise\n", "\n", "Create generator ``g(n)`` to generate all pairs (x, y) where x and y are from range(n) and x + y is not divisble by 3." ] }, { "cell_type": "code", "execution_count": 112, "id": "controversial-purchase", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(0, 1)\n", "(0, 2)\n", "(1, 0)\n", "(1, 1)\n", "(2, 0)\n", "(2, 2)\n" ] } ], "source": [ "def g(n):\n", " for x in range(n):\n", " for y in range(n):\n", " if (x + y) % 3 != 0:\n", " yield (x, y)\n", " \n", "\n", "for value in g(3):\n", " print(value)" ] }, { "cell_type": "code", "execution_count": 116, "id": "arranged-drill", "metadata": {}, "outputs": [], "source": [ "def arange(start, slut, step):\n", " value = start\n", " while value < slut:\n", " yield value\n", " value += step\n", " \n", "r = arange(3, 6, 0.25)" ] }, { "cell_type": "code", "execution_count": 117, "id": "opposite-shadow", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[3, 3.25, 3.5, 3.75, 4.0, 4.25, 4.5, 4.75, 5.0, 5.25, 5.5, 5.75]\n" ] } ], "source": [ "print(list(r))" ] }, { "cell_type": "code", "execution_count": null, "id": "regulated-distribution", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.4" } }, "nbformat": 4, "nbformat_minor": 5 }