Question: The following function reverses a string. Trace through the function and answer the questions below. def reverse (astring): newStr = for i in range (len

 The following function reverses a string. Trace through the function and

The following function reverses a string. Trace through the function and answer the questions below. def reverse (astring): newStr = for i in range (len (astring)): newStr astring[i] + newStr return newStr print (reverse ('quiz 3')) 1) What is the purpose of the loop inside the function's scope and how many iterations does it run for a string of size n? (2 marks) 2) Explain how the 't' operator is used in reversing the string? (1 mark) 3) Explain how the order of the terms on the right hand side of the assignment statement is important in reversing the string: newStr = astring[i] + newStr. Why would the string not be reversed if the order of the terms was newStr + astring[i]? (2 marks) 4) How would you change the code if you were to use the append builtin function? Only explain the steps (you don't have to rewrite the code)

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!