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.
4 Antworten
+ 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;
+ 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;
0
someone should please help me with this. It is a question that is very important to me.
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.