@extends('template') @section('title') Lab 8: List Comprehensions & Testing @stop @section('content') # List Comprehensions & Testing These exercises review material on [list comprehensions](/lectures/lec_sorting) and [testing](/lectures/lec_testing_debugging) that we covered in earlier lectures. Define each of the functions described below in **a new file** named `lab08.py`. You'll want to start with the following import: ``` import optimism ``` For each exercise, we've supplied code that uses a loop and your job is to a) convert the code to use a list comprehension instead, and b) write tests for your list comprehension version using `optimism`. Remember that defining a test is a 3-step process: 1. Create a test manager using [`optimism.testFunction`](/reference/quickref#TestManager). 2. Create a test case using the manager's [`case` method](/reference/quickref#TestManager.case). 3. Call the [`checkReturnValue`](/reference/quickref#TestCase.checkReturnValue) method of the test case. It's easiest to start from the examples provided below and modify them to create your test cases. ## `incrementEachLC`
Partner A
Here's code for a function called `incrementEach` which adds one to each number in a list. Your job is to write `incrementEachLC` which does the same thing but using a list comprehension instead of a `for` loop. ```py def incrementEach(nums): """ Returns a new list containing each of the numbers from the given list, incremented by 1. """ result = [] for n in nums: result.append(n + 1) return result ``` Here's an example of how it should work: ```xml >>> incrementEachLC([1, 2, 3]) [2, 3, 4] ``` ### Testing To test the original `incrementEach` function (the one that uses a for loop), you could use this code: ```py # Establishes a test manager called testIE testIE = optimism.testFunction(incrementEach) ``` ```py # Creates a test case for the incrementEach function # with the list [1, 2, 3]. Given [1, 2, 3], incrementEach # is expected to return [2, 3, 4]. testIE.case([1, 2, 3]).checkReturnValue([2, 3, 4]) ``` ```py # Creates another test case for the incrementEach function # with the list [10]. Given [10], incrementEach # is expected to return [11]. testIE.case([10]).checkReturnValue([11]) ``` Add that code to your file, then copy it and modify the copy so that your `incrementEachLC` function will *also* be tested. ## `capitalizeEachLC`
Partner B
Here's code for a function called `capitalizeEach` which makes each word in the list upper case. Your job is to write `capitalizeEachLC` which does the same thing but using a list comprehension instead of a `for` loop. ```py def capitalizeEach(words): """ Given a sequence of words, returns a new list containing each word in upper case. Does not modify the original list. """ result = [] for word in words: result.append(word.upper()) return result ``` Here's an example of how it should work: ```xml >>> capitalizeEachLC(['hi', 'hello']) ['HI', 'HELLO'] ``` ### Testing Here's the code for a single test of the `capitalizeEach` function: ```py testCE = optimism.testFunction(capitalizeEach) testCE.case(['a', 'b']).checkReturnValue(['A', 'B']) ``` Copy and modify this code to add at least **two** tests to your file for your `capitalizeEachLC` function. ## `addListsLC`
Partner A
Convert the following code to use a list comprehension: ```py def addLists(nums1, nums2): """ Adds together two equal-length lists of numbers, adding the numbers at each index to produce a result list with the same length as each of the input lists. """ result = [] for i in range(len(nums1)): result.append(nums1[i] + nums2[i]) return result ``` An example: ```xml >>> addListsLC([1, 2], [3, 4]) [4, 6] ``` ### Testing Using the testing examples for testing other functions given above as a guide, add at least two tests for `addListsLC` to your code. ## `oddWordsLC`
Partner B
One last function to convert: ```py def oddWords(wordList): """ Returns a list containing all of the odd-length words from the given list of words. """ result = [] for word in wordList: if len(word) % 2 == 1: result.append(word) return result ``` An example: ```xml >>> oddWordsLC(['one', 'two', 'three', 'four', 'five']) ['one', 'two', 'three'] ``` ### Testing Using the testing examples for testing other functions given above as a guide, add at least two tests for `oddWordsLC` to your code. @include('/labs/lab08/_toc') @stop