12 Prove : Homework - Data Structures
Lambda Functions
Outcomes
At the end of this study, successful students will be able to:
Articulate the strengths and weaknesses of lambda functions.
Use lambda functions in Python to solve problems.
Overview
Many modern languages make use of anonymous functions (functions that don't actually have a name), and often these are called lambda functions. Python provides for Lambda functions, and makes use of them for functions such as, filter, map, and reduce.
As you will see in the reading, there is some discussion about the need for these, and whether using something else like list comprehensions would be preferred in a given situation. That said, while not a tool for every job, list comprehensions are very useful in many circumstances.
Preparation Material
Please read the following:
Programming Assignment
Practice using lambda functions.
Instructions
Use lambda functions in combination with map, filter, and reduce functions to perform a series of operations on a list of 100 numbers.
Filter the list to return only those that are even numbers and greater than 33.
Map a lambda function to the list that squares each number and then adds one.
Reduce the list to the product of all the numbers (i.e., multiply them all together) using "reduce" and a lambda function.
To accomplish this, begin with the following code:
"""
Purpose: This file is a starting point to help you practice using lambda functions.
"""
import functools
def get_part1_list():
"""
Filters a list to return even numbers greater than 33.
"""
numbers = [x for x in range(100)]
# TODO: Write a line here that uses filter and a lambda function to filter
# the list so that it only contains even numbers greater than 33.
new_numbers = []
return new_numbers
def get_part2_list():
"""
Maps a lambda function to a list to square each number and add one.
"""
numbers = [x for x in range(100)]
# TODO: Write a line here than uses map and a lambda function to go through
# each number in the list, square it and then add one to the result
new_numbers = []
return new_numbers
def get_part3_list():
"""
Reduces a list to its product
"""
numbers = [x for x in range(1, 100)]
# TODO: Write a line here that uses reduce and a lambda function to
# multiply all the numbers in the list together and return the product
product = 0
return product
def main():
"""
This function calls the above functions and displays their result.
"""
print(get_part1_list())
print(get_part2_list())
print(get_part3_list())
if __name__ == "__main__":
main()
Then, your task is to implement the three functions that produce these lists. You should not need to modify main at all.
Testing your program
An automated testBed script is not available for your program. Instead, please manually test your program and ensure that it works correctly.
Submission
You are encouraged to work with others on the programming and the quiz.
When you have a good understanding of this data structure and have completed the programming project, take the accompanying I-Learn Quiz. It has questions about how the data structure works, and also, has places for your to report on the parts of the programming assignment that you completed.