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(astring)): newStr
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 '+' 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). (3 marks)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started