diff --git a/students/JohnRudolph/session8/circle_class.py b/students/JohnRudolph/session8/circle_class.py new file mode 100644 index 0000000..c4f6f34 --- /dev/null +++ b/students/JohnRudolph/session8/circle_class.py @@ -0,0 +1,66 @@ +#!/usr/bin/env python +from math import pi + +###################################################### +############## CIRCLE CLASS EXERCISE ############## +###################################################### + +class Circle(object): + ''' + Defines a circle object which takes + A radius or diameter as an input + And performs some operations + ''' + + #init takes radius or diameter as an input + def __init__(self, **kwargs): + if [x for x in kwargs if x not in ['radius', 'diameter']]: + raise AttributeError('radius or diameter is required') + #if radius then set radius + if 'radius' in kwargs: + self._radius = kwargs['radius'] + #if diameter then set radius + if'diameter' in kwargs: + self._radius = kwargs['diameter']/2 + + @property + def radius(self): + return self._radius + + @property + def diameter(self): + return self.radius*2 + + @property + def area(self): + return self._radius**2*pi + + @radius.setter + def radius(self, radius): + self._radius = radius + + @diameter.setter + def diameter(self, diameter): + self._radius = diameter/2 + + def __repr__(self): + return 'Circle({})'.format(self.radius) + + def __str__(self): + return 'Circle with radius: {}'.format(self.radius) + #set __add__ method to create new circle class with added radius values + def __add__(self, other): + add_radius= self.radius + other.radius + return Circle(radius= add_radius) + #set __mul__ method to create new circle class with multiplied radius + def __mul__(self, num): + return Circle(radius=(self.radius*num)) + #set comparison operators + def __gt__(self, other): + return self.radius > other.radius + + def __lt__(self, other): + return self.radius < other.radius + + def __eq__(self, other): + return self.radius == other.radius diff --git a/students/JohnRudolph/session8/test_circle_class.py b/students/JohnRudolph/session8/test_circle_class.py new file mode 100644 index 0000000..6865085 --- /dev/null +++ b/students/JohnRudolph/session8/test_circle_class.py @@ -0,0 +1,139 @@ +import circle_class as cc +import pytest +from math import pi + +def test_radius(): + ''' + test that radius can be set on init + and called via get property + ''' + radius = 4 + c = cc.Circle(radius=radius) + assert radius == c.radius + +def test_diameter(): + ''' + test that diameter is set when radius is provided + ''' + radius = 4 + c = cc.Circle(radius=radius) + assert radius*2 == c.diameter + +def test_set_diameter(): + ''' + test that a new diameter can be set based on set property + test that radius is reset when diameter is set + ''' + radius = 4 + c = cc.Circle(radius=radius) + new_diameter = 2 + c.diameter = new_diameter + assert c.diameter == new_diameter + assert c.radius == 1 + +def test_area(): + ''' + test that an area attribute is set when Circle is init + ''' + radius = 4 + c = cc.Circle(radius=radius) + area = (c.radius**2*pi) + assert c.area == area + +def test_area_error(): + ''' + Check if setting area raises an Attribute Error + ''' + radius = 4 + c = cc.Circle(radius=radius) + with pytest.raises(AttributeError): + c.area = 2 + +def test_alternate_constructor(): + ''' + Check if setting area raises an Attribute Error + ''' + diameter = 8 + c = cc.Circle(diameter = diameter) + assert c.radius == (diameter/2) + +def test_str_method(): + ''' + Check if __str__ method works as intended + ''' + radius = 4 + c = cc.Circle(radius = radius) + s = 'Circle with radius: {}'.format(radius) + assert s == str(c) + +def test_repr_method(): + ''' + Check if __str__ method works as intended + ''' + radius = 4 + c = cc.Circle(radius=radius) + s = 'Circle({})'.format(radius) + d = repr(c) + assert s == d + +def test_add_method(): + ''' + Check if 2 circles can be added + ''' + radius = 4 + diameter = 10 + c1 = cc.Circle(radius = radius) + c2 = cc.Circle(diameter= diameter) + c3 = c1 + c2 + assert c3.radius == radius + diameter/2 + +def test_mult_method(): + ''' + Check if 2 circles can be added + ''' + radius = 4 + num = 3 + c1 = cc.Circle(radius = radius) + c2 = c1*num + assert c2.radius == radius*num + +def test_gt_method(): + ''' + Check if circle1 radius greater than circle2 radius + ''' + radius1 = 4 + radius2 = 6 + c1 = cc.Circle(radius=radius1) + c2 = cc.Circle(radius=radius2) + assert (c2 > c1) == True + +def test_lt_method(): + ''' + Check if circle1 radius less than circle2 radius + ''' + radius1 = 4 + radius2 = 6 + c1 = cc.Circle(radius=radius1) + c2 = cc.Circle(radius=radius2) + assert (c1 < c2) == True + +def test_eq_method(): + ''' + Check if circle1 radius less than circle2 radius + ''' + radius1 = 6 + radius2 = 6 + c1 = cc.Circle(radius=radius1) + c2 = cc.Circle(radius=radius2) + assert (c1 == c2) == True + +def test_sort_method(): + ''' + Test of a list of circle objects is sortable + ''' + c1, c2, c3 = cc.Circle(radius=4), cc.Circle(radius=7), cc.Circle(radius=2) + circles = [c1, c2, c3] + sort_circles = [c3, c1, c2] + for i,n in zip(sorted(circles), sort_circles): + assert i.radius == n.radius + \ No newline at end of file diff --git a/students/JohnRudolph/session9/.ipynb_checkpoints/Session9 Notes-checkpoint.ipynb b/students/JohnRudolph/session9/.ipynb_checkpoints/Session9 Notes-checkpoint.ipynb new file mode 100644 index 0000000..1e35992 --- /dev/null +++ b/students/JohnRudolph/session9/.ipynb_checkpoints/Session9 Notes-checkpoint.ipynb @@ -0,0 +1,1182 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Session 9 Notes" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Using map" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "l = [2, 5, 6, 8, 11, 4]" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def fun(x):\n", + " return x * 10 + 10" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "m = map(fun, l)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "m" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "map" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(m)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[30, 60, 70, 90, 120, 50]" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list(m)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Using filter" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def fil(x):\n", + " return x%2 == 0" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "f = filter(fil, l)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[2, 6, 8, 4]" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list(f)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Using comprehensions" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[30, 60, 70, 90, 120, 50]" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "[x * 10 + 10 for x in l]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "the y in the front is the value that will be added to the new list" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[4, 7, 8, 10, 13, 6]" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "[y+2 for y in l]" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "my_new_list = [x * 10 + 10 for x in l]" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[30, 60, 70, 90, 120, 50]" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "my_new_list" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "filter using compression" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[2, 6, 8, 4]" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "[x for x in l if x%2 == 0]" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[4, 8, 10, 6]" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "[y+2 for y in l if y%2 ==0]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "map and a filter together in a compression" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Comprehensions and looping constructs" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "1\n", + "4\n" + ] + } + ], + "source": [ + "for x in range(3):\n", + " print(x **2)" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "my_happy_list = [x **2 for x in range(3)]" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 1, 4]" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "my_happy_list" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n", + "6\n", + "10\n" + ] + } + ], + "source": [ + "for x in range(6):\n", + " if x%2 !=0:\n", + " print(x*2)" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[2, 6, 10]" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "[x*2 for x in range(6) if x%2 !=0]" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "['ArithmeticError',\n", + " 'AssertionError',\n", + " 'AttributeError',\n", + " 'BlockingIOError',\n", + " 'BrokenPipeError',\n", + " 'BufferError',\n", + " 'ChildProcessError',\n", + " 'ConnectionAbortedError',\n", + " 'ConnectionError',\n", + " 'ConnectionRefusedError',\n", + " 'ConnectionResetError',\n", + " 'EOFError',\n", + " 'EnvironmentError',\n", + " 'FileExistsError',\n", + " 'FileNotFoundError',\n", + " 'FloatingPointError',\n", + " 'IOError',\n", + " 'ImportError',\n", + " 'IndentationError',\n", + " 'IndexError',\n", + " 'InterruptedError',\n", + " 'IsADirectoryError',\n", + " 'KeyError',\n", + " 'LookupError',\n", + " 'MemoryError',\n", + " 'NameError',\n", + " 'NotADirectoryError',\n", + " 'NotImplementedError',\n", + " 'OSError',\n", + " 'OverflowError',\n", + " 'PermissionError',\n", + " 'ProcessLookupError',\n", + " 'ReferenceError',\n", + " 'RuntimeError',\n", + " 'SyntaxError',\n", + " 'SystemError',\n", + " 'TabError',\n", + " 'TimeoutError',\n", + " 'TypeError',\n", + " 'UnboundLocalError',\n", + " 'UnicodeDecodeError',\n", + " 'UnicodeEncodeError',\n", + " 'UnicodeError',\n", + " 'UnicodeTranslateError',\n", + " 'ValueError',\n", + " 'ZeroDivisionError']" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "[name for name in dir(__builtin__) if \"Error\" in name]" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[4, 8, 12]" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "[x*2 for x in range(20) if x in [2, 4, 6]]" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n", + "6\n", + "6\n", + "7\n", + "7\n", + "8\n" + ] + } + ], + "source": [ + "for x in range(3):\n", + " for y in range(5, 7):\n", + " print(x + y)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[5, 6, 6, 7, 7, 8]" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "[x + y for x in range(3) for y in range(5, 7)]" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "from functools import reduce" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def my_sum(x, y):\n", + " return x+y" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "r= reduce(my_sum, l)" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "36" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "r" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "feast = ['lambs', 'sloths', 'orangutans', \n", + " 'breakfast cereals', 'fruit bats']" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "comprehension = [delicacy.capitalize() for delicacy in feast]" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "('Lambs', 'Orangutans')" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "comprehension[0], comprehension[2]" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'Fruit bats'" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "comprehension[4]" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'Breakfast cereals'" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "comprehension[3]" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "feast = ['spam', 'sloths', 'orangutans', 'breakfast cereals',\n", + " 'fruit bats']" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "comp = [delicacy for delicacy in feast if len(delicacy) > 6]" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "5" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(feast)" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(comp)" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "list_of_tuples = [(1, 'lumberjack'), (2, 'inquisition'), (4, 'spam')]" + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "comprehension = [ skit * number for number, skit in list_of_tuples]" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'lumberjack'" + ] + }, + "execution_count": 60, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "comprehension[0]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Lambda" + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def f(x, y):\n", + " return x+y" + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "7" + ] + }, + "execution_count": 64, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "f(3,4)" + ] + }, + { + "cell_type": "code", + "execution_count": 69, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "l = lambda x, y: x+y" + ] + }, + { + "cell_type": "code", + "execution_count": 72, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "7" + ] + }, + "execution_count": 72, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "l(3,4)" + ] + }, + { + "cell_type": "code", + "execution_count": 70, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "function" + ] + }, + "execution_count": 70, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(l)" + ] + }, + { + "cell_type": "code", + "execution_count": 71, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[>]" + ] + }, + "execution_count": 71, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "[lambda x,y: x+y]" + ] + }, + { + "cell_type": "code", + "execution_count": 73, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "my_func_list = [lambda x,y: x+y, lambda x,y: x*y]" + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "7" + ] + }, + "execution_count": 74, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "my_func_list[0](3,4)" + ] + }, + { + "cell_type": "code", + "execution_count": 75, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "12" + ] + }, + "execution_count": 75, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "my_func_list[1](3,4)" + ] + }, + { + "cell_type": "code", + "execution_count": 96, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "my_set_dict = {'add': lambda x, y, z: x+y+z, 'mult': lambda x,y: x*y}" + ] + }, + { + "cell_type": "code", + "execution_count": 97, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "12" + ] + }, + "execution_count": 97, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "my_set_dict['add'](3,4, 5)" + ] + }, + { + "cell_type": "code", + "execution_count": 98, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "my_set_list = list(range(5))" + ] + }, + { + "cell_type": "code", + "execution_count": 99, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 1, 2, 3, 4]" + ] + }, + "execution_count": 99, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "my_set_list" + ] + }, + { + "cell_type": "code", + "execution_count": 100, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "my_lambda = lambda my_list: print(my_list*2)" + ] + }, + { + "cell_type": "code", + "execution_count": 101, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0, 1, 2, 3, 4, 0, 1, 2, 3, 4]\n" + ] + } + ], + "source": [ + "my_lambda(my_set_list)" + ] + }, + { + "cell_type": "code", + "execution_count": 103, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "12" + ] + }, + "execution_count": 103, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "my_set_dict['add'](z=5, y=4, x=3)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# for lambda lab" + ] + }, + { + "cell_type": "code", + "execution_count": 105, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def function_builder():\n", + " pass" + ] + }, + { + "cell_type": "code", + "execution_count": 106, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def function_builder(n):\n", + " func_list = []\n", + " # can shorten using a compression\n", + " for i in range(n):\n", + " func_list.append(insert lambda function here)\n", + " return(func_list)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Closures" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "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.4.3" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/students/JohnRudolph/session9/Session9 Notes.ipynb b/students/JohnRudolph/session9/Session9 Notes.ipynb new file mode 100644 index 0000000..56eff6e --- /dev/null +++ b/students/JohnRudolph/session9/Session9 Notes.ipynb @@ -0,0 +1,1333 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Session 9 Notes" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Using map" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "l = [2, 5, 6, 8, 11, 4]" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def fun(x):\n", + " return x * 10 + 10" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "m = map(fun, l)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "m" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "map" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(m)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[30, 60, 70, 90, 120, 50]" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list(m)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Using filter" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def fil(x):\n", + " return x%2 == 0" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "f = filter(fil, l)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[2, 6, 8, 4]" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list(f)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Using comprehensions" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[30, 60, 70, 90, 120, 50]" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "[x * 10 + 10 for x in l]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "the y in the front is the value that will be added to the new list" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[4, 7, 8, 10, 13, 6]" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "[y+2 for y in l]" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "my_new_list = [x * 10 + 10 for x in l]" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[30, 60, 70, 90, 120, 50]" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "my_new_list" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "filter using compression" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[2, 6, 8, 4]" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "[x for x in l if x%2 == 0]" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[4, 8, 10, 6]" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "[y+2 for y in l if y%2 ==0]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "map and a filter together in a compression" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Comprehensions and looping constructs" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "1\n", + "4\n" + ] + } + ], + "source": [ + "for x in range(3):\n", + " print(x **2)" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "my_happy_list = [x **2 for x in range(3)]" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 1, 4]" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "my_happy_list" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n", + "6\n", + "10\n" + ] + } + ], + "source": [ + "for x in range(6):\n", + " if x%2 !=0:\n", + " print(x*2)" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[2, 6, 10]" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "[x*2 for x in range(6) if x%2 !=0]" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "['ArithmeticError',\n", + " 'AssertionError',\n", + " 'AttributeError',\n", + " 'BlockingIOError',\n", + " 'BrokenPipeError',\n", + " 'BufferError',\n", + " 'ChildProcessError',\n", + " 'ConnectionAbortedError',\n", + " 'ConnectionError',\n", + " 'ConnectionRefusedError',\n", + " 'ConnectionResetError',\n", + " 'EOFError',\n", + " 'EnvironmentError',\n", + " 'FileExistsError',\n", + " 'FileNotFoundError',\n", + " 'FloatingPointError',\n", + " 'IOError',\n", + " 'ImportError',\n", + " 'IndentationError',\n", + " 'IndexError',\n", + " 'InterruptedError',\n", + " 'IsADirectoryError',\n", + " 'KeyError',\n", + " 'LookupError',\n", + " 'MemoryError',\n", + " 'NameError',\n", + " 'NotADirectoryError',\n", + " 'NotImplementedError',\n", + " 'OSError',\n", + " 'OverflowError',\n", + " 'PermissionError',\n", + " 'ProcessLookupError',\n", + " 'ReferenceError',\n", + " 'RuntimeError',\n", + " 'SyntaxError',\n", + " 'SystemError',\n", + " 'TabError',\n", + " 'TimeoutError',\n", + " 'TypeError',\n", + " 'UnboundLocalError',\n", + " 'UnicodeDecodeError',\n", + " 'UnicodeEncodeError',\n", + " 'UnicodeError',\n", + " 'UnicodeTranslateError',\n", + " 'ValueError',\n", + " 'ZeroDivisionError']" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "[name for name in dir(__builtin__) if \"Error\" in name]" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[4, 8, 12]" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "[x*2 for x in range(20) if x in [2, 4, 6]]" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n", + "6\n", + "6\n", + "7\n", + "7\n", + "8\n" + ] + } + ], + "source": [ + "for x in range(3):\n", + " for y in range(5, 7):\n", + " print(x + y)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[5, 6, 6, 7, 7, 8]" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "[x + y for x in range(3) for y in range(5, 7)]" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "from functools import reduce" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def my_sum(x, y):\n", + " return x+y" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "r= reduce(my_sum, l)" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "36" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "r" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "feast = ['lambs', 'sloths', 'orangutans', \n", + " 'breakfast cereals', 'fruit bats']" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "comprehension = [delicacy.capitalize() for delicacy in feast]" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "('Lambs', 'Orangutans')" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "comprehension[0], comprehension[2]" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'Fruit bats'" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "comprehension[4]" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'Breakfast cereals'" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "comprehension[3]" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "feast = ['spam', 'sloths', 'orangutans', 'breakfast cereals',\n", + " 'fruit bats']" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "comp = [delicacy for delicacy in feast if len(delicacy) > 6]" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "5" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(feast)" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(comp)" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "list_of_tuples = [(1, 'lumberjack'), (2, 'inquisition'), (4, 'spam')]" + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "comprehension = [ skit * number for number, skit in list_of_tuples]" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'lumberjack'" + ] + }, + "execution_count": 60, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "comprehension[0]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Lambda" + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def f(x, y):\n", + " return x+y" + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "7" + ] + }, + "execution_count": 64, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "f(3,4)" + ] + }, + { + "cell_type": "code", + "execution_count": 69, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "l = lambda x, y: x+y" + ] + }, + { + "cell_type": "code", + "execution_count": 72, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "7" + ] + }, + "execution_count": 72, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "l(3,4)" + ] + }, + { + "cell_type": "code", + "execution_count": 70, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "function" + ] + }, + "execution_count": 70, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(l)" + ] + }, + { + "cell_type": "code", + "execution_count": 71, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[>]" + ] + }, + "execution_count": 71, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "[lambda x,y: x+y]" + ] + }, + { + "cell_type": "code", + "execution_count": 73, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "my_func_list = [lambda x,y: x+y, lambda x,y: x*y]" + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "7" + ] + }, + "execution_count": 74, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "my_func_list[0](3,4)" + ] + }, + { + "cell_type": "code", + "execution_count": 75, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "12" + ] + }, + "execution_count": 75, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "my_func_list[1](3,4)" + ] + }, + { + "cell_type": "code", + "execution_count": 96, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "my_set_dict = {'add': lambda x, y, z: x+y+z, 'mult': lambda x,y: x*y}" + ] + }, + { + "cell_type": "code", + "execution_count": 97, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "12" + ] + }, + "execution_count": 97, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "my_set_dict['add'](3,4, 5)" + ] + }, + { + "cell_type": "code", + "execution_count": 98, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "my_set_list = list(range(5))" + ] + }, + { + "cell_type": "code", + "execution_count": 99, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 1, 2, 3, 4]" + ] + }, + "execution_count": 99, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "my_set_list" + ] + }, + { + "cell_type": "code", + "execution_count": 100, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "my_lambda = lambda my_list: print(my_list*2)" + ] + }, + { + "cell_type": "code", + "execution_count": 101, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0, 1, 2, 3, 4, 0, 1, 2, 3, 4]\n" + ] + } + ], + "source": [ + "my_lambda(my_set_list)" + ] + }, + { + "cell_type": "code", + "execution_count": 103, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "12" + ] + }, + "execution_count": 103, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "my_set_dict['add'](z=5, y=4, x=3)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# for lambda lab" + ] + }, + { + "cell_type": "code", + "execution_count": 105, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def function_builder():\n", + " pass" + ] + }, + { + "cell_type": "code", + "execution_count": 106, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def function_builder(n):\n", + " func_list = []\n", + " # can shorten using a compression\n", + " for i in range(n):\n", + " func_list.append(insert lambda function here)\n", + " return(func_list)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Closures" + ] + }, + { + "cell_type": "code", + "execution_count": 107, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def make_multiplier_of(n):\n", + " def multiplier(x):\n", + " return x * n\n", + " return multiplier " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "locking down n in the function below to make immutable" + ] + }, + { + "cell_type": "code", + "execution_count": 108, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "times3 = make_multiplier_of(3)" + ] + }, + { + "cell_type": "code", + "execution_count": 109, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "times5 = make_multiplier_of(5)" + ] + }, + { + "cell_type": "code", + "execution_count": 111, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "24" + ] + }, + "execution_count": 111, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "times3(8)" + ] + }, + { + "cell_type": "code", + "execution_count": 112, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "50" + ] + }, + "execution_count": 112, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "times5(10)" + ] + }, + { + "cell_type": "code", + "execution_count": 113, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "['__annotations__',\n", + " '__call__',\n", + " '__class__',\n", + " '__closure__',\n", + " '__code__',\n", + " '__defaults__',\n", + " '__delattr__',\n", + " '__dict__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__get__',\n", + " '__getattribute__',\n", + " '__globals__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__init__',\n", + " '__kwdefaults__',\n", + " '__le__',\n", + " '__lt__',\n", + " '__module__',\n", + " '__name__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__qualname__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__setattr__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__']" + ] + }, + "execution_count": 113, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dir(times3)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "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.4.3" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/students/JohnRudolph/session9/lambda_lab.py b/students/JohnRudolph/session9/lambda_lab.py new file mode 100644 index 0000000..8a6c7dd --- /dev/null +++ b/students/JohnRudolph/session9/lambda_lab.py @@ -0,0 +1,9 @@ +#!/usr/bin/env python +def increment_function(n): return lambda x: x + n + +def function_builder(n): + return[increment_function(x) for x in range(n)] + +f_list = function_builder(5) +for i in f_list: + print(i(2)) diff --git a/students/JohnRudolph/session9/test_lambda_lab.py b/students/JohnRudolph/session9/test_lambda_lab.py new file mode 100644 index 0000000..dd77847 --- /dev/null +++ b/students/JohnRudolph/session9/test_lambda_lab.py @@ -0,0 +1,10 @@ +import lambda_lab as ll + +def test_function_builder(): + ''' + Test that items in function list + are returning correct values + ''' + the_list = ll.function_builder(4) + assert the_list[0](2) == 2 + assert the_list[1](2) == 3 \ No newline at end of file