PyCon2012 Intermediate Python Solutions

functional.py

import types
import unittest

class TestFunctional(unittest.TestCase):
    def test_functional(self):
        # Lambda
        # create a lambda statement that adds 2 to it's input
        # assign the statement to a variable named ``add_2``
        # ================================
        add_2=lambda a: a+2
        self.assert_(isinstance(add_2, types.LambdaType))
        self.assertEquals(add_2(4), 6)

        # Lamda 2
        # Create an ``is_odd`` lambda statement,
        # that returns True if the input is odd
        # ================================
        is_odd=lambda a: a%2 != 0
        self.assert_(isinstance(is_odd, types.LambdaType))
        self.assertEquals(is_odd(5), True)
        self.assertEquals(is_odd(4), False)


        # Map
        # Create a list ``digits`` with the numbers from 0 to 9
        # Create a new list ``two_more`` from digits by
        # mapping ``add_2`` to the elements of digits
        # ================================
        digits = range(0,10)
        two_more= map (add_2, digits)
        self.assertEquals(digits, range(0, 10))
        self.assertEquals(two_more, range(2,12))

        # Reduce
        # Add the values of digits by using
        # ``reduce`` with the ``operator.add``
        # function, store the result in ``digit_sum``
        # ================================
        import operator
        digit_sum = reduce(operator.add, digits)
        self.assertEquals(digit_sum, 45)

        # Filter
        # use ``filter`` to get the odd digits of
        # ``two_more``, store results in ``two_odd``
        # ================================
        two_odd=filter(lambda two_more: two_more%2!=0, two_more)
        self.assertEquals(two_odd, [3, 5, 7, 9, 11])

if __name__ == '__main__':
    unittest.main()

funcargs.py


closures.py

import types
import unittest

class TestClosures(unittest.TestCase):
    def test_closure(self):
        # Closure
        #
        # Create a function ``echo_creator`` that returns a function that returns what was passed into it
        # ================================
        def echo_creator():
            def echo_in(innum):
                return innum
            return echo_in
        
        echo = echo_creator()
        self.assert_(isinstance(echo, types.FunctionType))
        self.assertEquals(echo(5), 5)
        self.assertEquals(echo("foo"), "foo")

        # Closure 2
        # Create a function ``mult_factory`` that accepts
        # a number and returns a function that multiples its
        # input by that number
        # ================================
        def mult_factory(num):
            def mul_num(mulnum):
                return num * mulnum
            return mul_num
        mult5 = mult_factory(5)
        self.assert_(isinstance(mult5, types.FunctionType))
        self.assertEquals(mult5(5), 25)
        self.assertEquals(mult5("f"), "fffff")

if __name__ == '__main__':
    unittest.main()

iterators.py

import unittest
import types

class TestIterators(unittest.TestCase):
    def test_iterators(self):
        # Iterators
        #
        # Create a list ``a_list`` with [0,1,2] in it.
        # Get an iterator ``an_iter`` for ``a_list`` by calling
        # the ``iter`` function on it
        # ================================
        a_list=[0,1,2]
        an_iter = iter(a_list)
        self.assertEquals(next(an_iter), 0)
        self.assertEquals(next(an_iter), 1)
        self.assertEquals(next(an_iter), 2)
        self.assertRaises(StopIteration, next, an_iter)

        # Iterators
        #
        # Create a list, ``b_list`` with [4, 3, 2] in it.
        # Store an iterator for ``b_list`` in ``b_iter``.
        # Take 2 items off the iterator by calling ``next()`` on it.
        # ================================
        b_list = [4,3,2]
        b_iter = iter(b_list)
        b_iter.next()
        b_iter.next()
        self.assertEquals(next(b_iter), 2)
        self.assertRaises(StopIteration, next, b_iter)

if __name__ == '__main__':
    unittest.main()

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: