Question: PYTHON Programming Exercise 6.8: Lee has discovered what he thinks is a clever recursive strategy for printing the elements in a sequence (string, tuple, or
PYTHON Programming Exercise 6.8:
Lee has discovered what he thinks is a clever recursive strategy for printing the elements in a sequence (string, tuple, or list). He reasons that he can get at the first element in a sequence using the 0 index, and he can obtain a sequence of the rest of the elements by slicing from index 1. This strategy is realized in a function that expects just the sequence as an argument. If the sequence is not empty, the first element in the sequence is printed and then a recursive call is executed. On each recursive call, the sequence argument is sliced using the range 1:. Here is Lees function definition:
def printAll(seq):
if seq:
print(seq[0])
printAll(seq[1:])
Write a script that tests this function and add code to trace the argument on each call. Does this function work as expected? If so, explain how it actually works, and describe any hidden costs in running it.
I have included my current code and the assertion error message that the module is raising. Please help!
!
Programming Exercise 6.8 Tasks testprintlist.py+ Test Output 1 # Put your code here 2 def printAll (seq): 3 if seq: print (seq[0]) printAll(seq[1:]) 7 printAll("Hello World! ") 8 printAL((1,2,3,4)) 9 printAll([1,2,3,4]) 10 Traceback (most recent call last): File "codevolve-test ad26dd7c", line 11, in cmodule assert ("[e, 1, 2, 3, 4, 5, 6, 7, 8, 9]" in contents) AssertionErro Run Checks Submit 0% Programming Exercise 6.8 Tasks testprintlist.py+ Test Output 1 # Put your code here 2 def printAll (seq): 3 if seq: print (seq[0]) printAll(seq[1:]) 7 printAll("Hello World! ") 8 printAL((1,2,3,4)) 9 printAll([1,2,3,4]) 10 Traceback (most recent call last): File "codevolve-test ad26dd7c", line 11, in cmodule assert ("[e, 1, 2, 3, 4, 5, 6, 7, 8, 9]" in contents) AssertionErro Run Checks Submit 0%
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
