Question

In: Computer Science

All functions are written in python Write a function cube_evens_lc(values) that takes as input a list...

All functions are written in python

  1. Write a function cube_evens_lc(values) that takes as input a list of numbers called values, and that uses a list comprehension to create and return a list containing the cubes of the even numbers in values (i.e., the even numbers raised to the third power). For example:

    >>> cube_evens_lc([2, 5, 6, 4, 1])
    result: [8, 216, 64]
    

    This version of the function may not use recursion.

  2. Write a function cube_evens_rec(values) that takes as input a list of numbers called values, and that uses recursion to create and return a list containing the cubes of the even numbers in values. In other words, this function will do the same thing as the previous function, but it must use recursion instead of a list comprehension. For example:

    >>> cube_evens_rec([2, 5, 6, 4, 1])
    result: [8, 216, 64]
    

    This version of the function may not use a list comprehension.

Solutions

Expert Solution

Source Code:

Output:

Code in text format (See above images of code for indentation):

#import math module
import math

#function using list comprehension
def cube_evens_lc(values):
result=[pow(x,3) for x in values if x%2==0]
return result

#list declaration
rec_res=[]
#function using recursion
def cube_evens_rec(values):
s=len(values)
if(s==0):
return [0]
else:
#check for even
if values[0]%2==0:
#add to list
rec_res.append(pow(values[0],3))
#recursive call
cube_evens_rec(values[1:])
else:
cube_evens_rec(values[1:])
#return result list
return rec_res

#list declaration   
values=[]
#read size from user
s=int(input("Enter the size of the list: "))
print("Enter elements of list:")
#read numbers into list
for i in range(s):
n=int(input())
values.append(n)
#function call
result=cube_evens_lc(values)
#print list
print("Using list comprehension")
print("result: ",result)
#function call
result1=cube_evens_rec(values)
#print list
print("Using recusrsion")
print("result: ",result1)


Related Solutions

Write a function cube_all_lc(values) that takes as input a list of numbers called values, and that...
Write a function cube_all_lc(values) that takes as input a list of numbers called values, and that uses a list comprehension to create and return a list containing the cubes of the numbers in values (i.e., the numbers raised to the third power). This version of the function may not use recursion.
USING PYTHON, write a function that takes a list of integers as input and returns a...
USING PYTHON, write a function that takes a list of integers as input and returns a list with only the even numbers in descending order (Largest to smallest) Example: Input list: [1,6,3,8,2,5] List returned: [8, 6, 2]. DO NOT use any special or built in functions like append, reverse etc.
Write a function that takes a list of integers as input and returns a list with...
Write a function that takes a list of integers as input and returns a list with only the even numbers in descending order (Largest to smallest) Example: Input list: [1,6,3,8,2,5] List returned: [8, 6, 2] Do not use any special or built in functions like append, reverse etc.
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should...
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should assume that the list is already in ascending order. The function should insert the number into the correct position of the list so that the list stays in ascending order. It should modify the list, not build a new list. It does not need to return the list, because it is modifying it.   Hint: Use a whlie loop and list methods lst = [1,3,5,7]...
Write a Python function that takes a list of string as arguments. When the function is...
Write a Python function that takes a list of string as arguments. When the function is called it should ask the user to make a selection from the options listed in the given list. The it should get input from the user. Place " >" in front of user input. if the user doesn't input one of the given choices, then the program should repeatedly ask the user to pick from the list. Finally, the function should return the word...
write a python function that takes a number as input argument, and that tries to determine...
write a python function that takes a number as input argument, and that tries to determine two other integers, root and pwr, such that root**pwr is equal to the integer passed as an argument to the function. Consider that pwr > 1. The function should return the values of root and pwr, as a string in the form root**pwr. For example, when the passed argument is 8, the function should return the string ‘2**3’. When the passed input argument does...
Q1-      Write a program that takes a list of values as an input from the user....
Q1-      Write a program that takes a list of values as an input from the user. The program should further ask the user about sorting the list in ascending or descending order. It is desirable to use Arraylist/Vector in place of simple arrays. (Object Oriented Programming java)
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
This is python: #Write a function called count_positive_evens. This function #should take as input a list...
This is python: #Write a function called count_positive_evens. This function #should take as input a list of integers, and return as #output a single integer. The number the function returns #should be the count of numbers from the list that were both #positive and even. # #For example: # # count_positive_evens([5, 7, 9, 8, -1, -2, -3]) -> 1 # count_positive_evens([2, 4, 6, 8, 10, 12, 15]) -> 6 # count_positive_evens([-2, -4, -6, -8, -10, 1]) -> 0 # #0...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT