Question
This lab is broken into multiple parts. Your goal is to complete as many parts as you can by the end of the lab. If
This lab is broken into multiple parts. Your goal is to complete as many parts as you can by the end of the lab. If you do not complete all of them, you can still get full points; show that you have worked hard and put in effort and complete the minimum amount of problems (6 out of 8) and this will be accomplishable.
The purpose of this lab is to practice with Pythons higher-order functions, list and dictionary comprehensions, and ternary operator.
Part 2: Higher-Order Functions (70 minutes)
Let a be the list of values produced by list(range(1, 11)). Problems 1-6 may be done directly inside the main() function.
- Using the map and a lambda argument, write an expression that produces a list of the cubes of the values in a. Assign the result to a variable called m and print m.
- Using the filter function and a lambda argument, write an expression that produces a list of the values in a that are divisible by 3. Assign the result to a variable called f1 and print f1.
- Using the reduce function and a lambda argument, write an expression that returns the result of concatenating all of the digits in a. Assign the result to a variable called r1 and print r1. The output from this step is the string 12345678910
- Use a list comprehension to produce the same list as in question 1 (i.e., the new list will contain the cubes of the values in a.)
- Use a list comprehension to produce the same list as in question 2.
- Use a list comprehension to produce a list containing the cubes of the values in a that are divisible by 3. The output from this step is the list [27, 216, 729]
- Write a function named evenFilter(dict) that takes as an argument a dictionary of elements indexed by integer keys. Using only a list comprehension, return a list of the values of the elements associated with the keys that are evenly divisible by 2. For example:
>>> data = {1: "one", 3: "three", 4: "four", 5: "five", 8: "eight", 10: "ten"} >>> print(evenFilter(data)) ['four', 'eight', 'ten'] |
- Write a function named findMin(x, y) that uses the ternary operator (i.e. conditional expression) to find and return the minimum of its two arguments. Assume that x and y are numbers. Add code to main() to test this function.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started