100k views
1 vote
write a function copy(s, n) that takes as inputs a string s and an integer n, and that uses recursion to create and return a string in which n copies of s have been concatenated together. python

User Evenodd
by
4.7k points

1 Answer

5 votes

Answer:

Following are the method to the given question:

def copy(s, n):#defining a method copy that takes two parameters

if n <= 0:#definig if to compare n value is less than equal to 0

return ''#return space

else:#definig else block

return s + copy(s, n-1)#use recursive method that return value

print(copy("by",2))#calling method and print value

print(copy("ta",2))#calling method and print value

print(copy("good by ",2))#calling method and print value

Output:

byby

tata

good by good by

Explanation:

In this code, a method "copy" is declared that takes two parameters that are "s and n" inside the method a conditional statement is used which can be defined as follows.

In the if block is used "n" variable that checks n value which is less than equal to 0 if it is true it will return a space value.

In the else block it use the recursive method that returns a value which is a copy of s that is concatenated together.

User Riyad Kalla
by
4.3k points