Question

The purpose of this lab is to practice working with Python's higher order functions and ternary...

The purpose of this lab is to practice working with Python's higher order functions and ternary operator. Write all of your code in one file named lab2.py. You can write the code for items 1 through 6 directly inside the main function.

Let a be the list of values produced by list(range(1, 11)).

  1. [1 point] Using the map function 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. [1 point] 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. [1 point] Using the reduce function and a lambda argument, write an expression that returns the result of concatenating all 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. [1 point] 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. [1 point] Use a list comprehension to produce the same list as in question 2.
  6. [1 point] 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. [2 points] Write a function named evenFilter 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']
    add this code to your main function to test the evenFilter function
  8. [2 points] 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.
0 0
Add a comment Improve this question Transcribed image text
Answer #1
 

Explanation::

Code in PYTHON is given below

Output is given at the end of the code

Code in PYTHON:

import functools

def evenFilter(data):

  values = []

  for key in data.keys():

    if key%2==0:

      values.append(data[key])

  return values

def findMin(x,y):

  min = x if x < y else y

  return min

def main():

  a = list(range(1,11))

  print(a)

  #Part 1

  m = list(map(lambda x: x**3 , a))

  print(m)

  #Part 2

  f1 = list(filter(lambda x: (x%3 == 0) , a))

  print(f1)

  #Part 3

  r1 = functools.reduce(lambda x, y: '{0}{1}'.format(x, y),a)

  print(r1)

  #Part 4

  listComp = [x**3 for x in a ]

  print(listComp)

  #Part 5

  divisible = [x for x in a if x%3==0]

  print(divisible)

  #Part 6

  divisibleCube = [x for x in listComp if x%3==0]

  print(divisibleCube)

  

  print("Testing evenFilter method:")

  data = {1:"one",3:"three",4:"four",5:"five",8:"eight",10:"ten"}

  print(evenFilter(data))

  print("\nTest findMin() method on 10 and 45")

  print(findMin(10,45))

main()

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]

[3, 6, 9]

12345678910

[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]

[3, 6, 9]

[27, 216, 729]

Testing evenFilter method:

['four', 'eight', 'ten']

Test findMin() method on 10 and 45

10

Please provide the feedback!!

Thank You!!

Add a comment
Know the answer?
Add Answer to:
The purpose of this lab is to practice working with Python's higher order functions and ternary...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. Write a function called ordinal_sum that accepts a string argument and returns the sum of...

    1. Write a function called ordinal_sum that accepts a string argument and returns the sum of the ordinal values of each character in the string. The ordinal value of a character is its numeric Unicode code point in decimal. 2. Write code that creates a Python set containing each unique character in a string named my_string. For example, if the string is 'hello', the set would be {'h', 'e', 'l', 'o'} (in any order). Assign the set to a variable...

  • In Python 5. Write a function named listComprehensionDivisors that has 3 integer inputs, N, n1, and...

    In Python 5. Write a function named listComprehensionDivisors that has 3 integer inputs, N, n1, and n2. Using a single list comprehension to make a list of all of the numbers in the range 1..N that are either divisible by n1 or n2, where n1 and n2 can be any positive integers. After creating the list, your code should print out the following 2 lines: “We are printing all numbers from 1 to that are divisible by or ” “These...

  • python 3 Lab Activity 4.4: Using Lambda Functions Using Lambda Functions الا </> Write a lambda...

    python 3 Lab Activity 4.4: Using Lambda Functions Using Lambda Functions الا </> Write a lambda function that takes in two numerical values and returns the first value, raised to the power of the second value: Steps for Completion 1. Create a lambda function that takes in number and power and returns the value of the number raised to power. 2. Assign it to a variable called exponential. 3. Print the exponential variable. Using the numberical values 2 and 6,...

  • C++ ONLY Write a function calculator that takes two floating numbers and one operator and prints...

    C++ ONLY Write a function calculator that takes two floating numbers and one operator and prints out the answer based on arithmetic. Assume that there are no overflow, underflow and division by zero cases. Your function should be named calculator Your function takes three input parameter: two double numbers and one char operator Your function does not return anything Your function prints answer in the format specified below Your function should set precision point to 2 Note: You must use...

  • 40pts) Given the following code segment. mSwer= input("Enter age: ") ile answer != 'q' : try:...

    40pts) Given the following code segment. mSwer= input("Enter age: ") ile answer != 'q' : try: print(int(answer) ) answer input ( " Enter age: ") except Value E rror : print ("Invalid answer 'q' age") Show what is printed if the user enters the following values at the prompt, one at a time. Print output User input 20 17 2a => 29 q 9. The following main function works with a class called Student that you will write. def main()...

  • write Java statements that perform the following functions ? 21 If x is greater than or...

    write Java statements that perform the following functions ? 21 If x is greater than or equal zero, then assign the square root of x to variable sqrtx and print out the result Otherwise, print out an error message about the argument of the square root function and set sqrtx to zero (2) 22 A variable fun is calculated by dividing variable numerator by variable denominator If the absolute value of denominator is less than 1, write "Divide by zero...

  • Exercise #1: Write a C program that contains the following steps (make sure all variables are...

    Exercise #1: Write a C program that contains the following steps (make sure all variables are int). Read carefully each step as they are not only programming steps but also learning topics that explain how functions in C really work. Ask the user for a number between 10 and 99. Write an input validation loop to make sure it is within the prescribed range and ask again if not. Commenting out the existing coding, write the code to divide a...

  • Python 3, nbgrader, pandas 0.23.4 Q2 Default Value Functions (1 point) a) Sort Keys Write a function called sort_keys, which will return a sorted version of the keys from an input dictionary Input(s...

    Python 3, nbgrader, pandas 0.23.4 Q2 Default Value Functions (1 point) a) Sort Keys Write a function called sort_keys, which will return a sorted version of the keys from an input dictionary Input(s): dictionary :dictionary reverse boolean, default: False Output(s): .sorted_keys: list Procedure(s) Get the keys from the input dictionary using the keys()method (this will return a list of keys) Use the sorted function to sort the list of keys. Pass in reverse to sorted to set whether to reverse...

  • Question 1 (1 point) Which of the following is a reason to use functions? Question 1...

    Question 1 (1 point) Which of the following is a reason to use functions? Question 1 options: To make it easier to read by keeping sections of code shorter. To avoid duplicating code. all of the above To make the code more organized. Question 2 (1 point) When the execution of a function ends, what Python code is executed next? Question 2 options: The function is automatically executed again. The line of code immediately after the end of the function....

  • Lab 7: Void and Value-Returning Functions Lab 7: Void and Value-returning functions             ...

    Lab 7: Void and Value-Returning Functions Lab 7: Void and Value-returning functions              Due date: 11/6/19 Problem: Write a C++ program that calculates the areas of a rectangle and of an ellipse.                    Area = base * height             Area = π * radius a * radius b Note: all images extracted from http://www.mathsisfun.com/area-calculation-tool.html ------------------------------------------------------------------------------------------------------------------------ Your task: implement in C++ the algorithm solution shown below. ------------------------------------------------------------------------------------------------------------------------ Part A (79...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT