Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.

  1. 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.
  2. 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.
  3. 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
  4. 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.)
  5. Use a list comprehension to produce the same list as in question 2.
  6. 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]
  7. 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']

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Business Process Driven Database Design With Oracle PL SQL

Authors: Rajeev Kaula

1st Edition

1795532386, 978-1795532389

More Books

Students also viewed these Databases questions

Question

2. Why is the choice of a mix a difficult task for marketers?

Answered: 1 week ago

Question

Know how to use reservations systems to inventory demand.

Answered: 1 week ago