Question: Fix the code snippet below by changing only the code inside the function in order to produce the correct output. Given a number sequence seq

Fix the code snippet below by changing only the code inside the function in order to produce the correct output.

Given a number sequence seq as input, the function consecutive_elem_ratio() attempts to compute the ratio of each element to that of the previous element and change seq to contain the new computed values. Since the first element has no previous element, the resulting list must be shortened by one element for this operation to make sense.

The function satisfies the following equality:

consecutive_elem_ratio([1, 1, 2, 3, 5, 8, 13, 21]) == [1.0, 2.0, 1.5, 1.6666666666666667, 1.6, 1.625, 1.6153846153846154] 

Note: It is important that you change only the code in the function body. The autograder will rerun your function to verify that this is the case, so it is possible that the fibonnaci_seq you obtain as an output is correct but the autograder will still assign you zero points because your function is not correct. In particular, it would be meaningless to add a return statement to the end of your function in this problem because the rest of the code would ignore any returned value.

Python code (user_code.py)

def consecutive_elem_ratio(seq): temp = [] for i,v in enumerate(seq): temp.append(v/seq[i-1]) seq = temp seq.pop(0)

fibonnaci_seq = [1, 1, 2, 3, 5, 8, 13, 21] consecutive_elem_ratio(fibonnaci_seq) print(fibonnaci_seq)

Fix the code snippet below by changing only the code inside the

Fix the code snippet below by changing only the code inside the function in order to produce the correct output. Given a number sequence seq as input, the function consecutive_elem_ratio() attempts to compute the ratio of each element to that of the previous element and change seq to contain the new computed values Since the first element has no previous element, the resulting list must be shortened by one element for this operation to make sense The function satisfies the following equality: consecutive-elem-ratio( [1, 1, 2, 3, 5, 8, 13, 21]) [1.0, 2.0, 1.5, 1.6666666666666667, 1. Note: It is important that you change only the code in the function body. The autograder will rerun your function to verify that this is the case, so it is possible that the fibonnaci_seq you obtain as an output is correct but the autograder will still assign you zero points because your function is not correct. In particular, it would be meaningless to add a return statement to the end of your function in this problem because the rest of the code would ignore any returned value. user_code.py 1 def consecutive_elem_ratio(sea): 2 temp C] 3for i,v in enumerate(seq): 4 temp.append(v/seq[i-1]) seq-temp 6 seq.popC0) 7 8 fibonnaci-seq = [1, 1, 2, 3, consecutive_elem_ratio(fibonnaci_sea) 5, 8, 13, 21] 10 print(fibonnaci_sea)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!