I need to design a recursive function called replicate recur which will receive two arguments: times which is the number of times to repeat and data which is the number or string to be repeated. The function should return an array containing repetitions of the data argument. For instance, replicate_recur (3, 5) should return [5,5,5]. If the times argument is negative or zero, return an empty array. If the argument is invalid, raise ValueError. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I need to design a recursive function called replicate recur which will receive two arguments: times which is the number of times to repeat and data which is the number or string to be repeated. The function should return an array containing repetitions of the data argument. For instance, replicate_recur (3, 5) should return [5,5,5]. If the times argument is negative or zero, return an empty array. If the argument is invalid, raise ValueError.

5th Sep 2016, 9:26 PM
Dan-Awoh Emmanuel
Dan-Awoh Emmanuel - avatar
4 Answers
+ 2
def replicate_recur(c,val): if(isinstance(val,(float,int,str))): if(c>0): ls = replicate_recur(c-1,val); ls.append(val); return ls; else: return []; else: raise ValueError;
6th Sep 2016, 12:51 AM
Иван Гусляков
Иван Гусляков - avatar
+ 1
def replicate_iter(c,val): if(isinstance(val,(float,int,str))): if(c>0): ls = [val]; #ls = ls * c; for i in range(1,c): ls.append(val); return ls; else: return []; else: raise ValueError;
6th Sep 2016, 12:38 PM
Иван Гусляков
Иван Гусляков - avatar
0
someone should please help me with this. It is a question that is very important to me.
5th Sep 2016, 9:27 PM
Dan-Awoh Emmanuel
Dan-Awoh Emmanuel - avatar
0
thank you! I really appreciate it. I'm grateful. please can you assist me with this: this time, I need to design a ITERATIVE function called replicate_iter which will receive two arguments: times which is the number of times to repeat and data which is the number or string to be repeated. The function should return an array containing repetitions of the data argument. For instance, replicate_iter (3, 5) should return [5,5,5]. If the times argument is negative or zero, return an empty array. If the argument is invalid, raise ValueError.
6th Sep 2016, 12:22 PM
Dan-Awoh Emmanuel
Dan-Awoh Emmanuel - avatar