# List Comprehensions & Testing

These exercises review material on list comprehensions and testing 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`.
2. Create a test case using the manager's `case` method.
3. Call the `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.

``````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:

``````>>> 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:

``````# Establishes a test manager called testIE
testIE = optimism.testFunction(incrementEach)``````
``````# 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])``````
``````# 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.

``````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:

``````>>> capitalizeEachLC(['hi', 'hello'])
['HI', 'HELLO']``````

### Testing

Here's the code for a single test of the `capitalizeEach` function:

``````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:

``````def addLists(nums1, nums2):
"""
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:

``````>>> 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:

``````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:

``````>>> 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.