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

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 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...
Write a Python function that takes as input parameters base_cost (a float) and customer_type and prints...
Write a Python function that takes as input parameters base_cost (a float) and customer_type and prints a message with information about the total amount owed and how much the tip was. As a reminder, the tip amounts are 10%, 15% and 20% for stingy, regular, and generous customers. And the tax amount should be 7%. The total amount is calculated as the sum of two amounts: check_amount = base_cost*1.07 tip_amount = tip_percentage*check_amount To receive full credit, you must use string...
(Python) a) Using the the code below write a function that takes the list xs as...
(Python) a) Using the the code below write a function that takes the list xs as input, divides it into nss = ns/nrs chunks (where nrs is an integer input parameter), computes the mean and standard deviation s (square root of the variance) of the numbers in each chunk and saves them in two lists of length nss and return these upon finishing. Hint: list slicing capabilities can be useful in implementing this function. from random import random as rnd...
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT