Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

BlockPy: #33.3) Cube Elements Use the Map pattern to create a function cube_elem

ID: 3728489 • Letter: B

Question

BlockPy: #33.3) Cube Elements Use the Map pattern to create a function cube_elements that consumes a list of numbers and returns a new list of each number cubed (to the third power). Try running this function on the list [1, 2, 3] This problem has a worked example. Feedback: Algorithm Error Unused Variable The variable a_list was given a value, but was never used after that. Printer "Blocks -Split | / Text ~ Reset O Upload , E History a-list [1,2,3] 2def cube_elements() for item in list: 4. alist = alist**3 - - 5print (a_list) 6 7

Explanation / Answer

#first method

a_list = [1,2,3]
result_list = []
def cube_element():
     for item in a_list:
         ans = item ** 3; ## calculating the answer
         result_list.append(ans)    ## appending the answer in to the another list
print a_list
cube_element()
a_list = result_list    ## if you want anser in same list ,then copy your data
print a_list

'''
OUTPUT:
[1, 2, 3]
[1, 8, 27]
'''


## second method

a_list = [1,2,3]
result_list = []
def cube_element():
     a_list[:] = [x ** 3 for x in a_list]
   
print "Input   ",a_list
cube_element()
print "OUTPUT   ",a_list

'''
Input    [1, 2, 3]
OUTPUT    [1, 8, 27]
'''

## PLease feel free to ask any doubt related to this question