Recursion: design a recursive function called replicate_recur which will receive two arguments:
<p>The below code is a recursive function which takes two arguments and return something  like <code>[5,5,5]</code>.</p>

<pre><code>def recursive(times, data):  
    if not isinstance(times,int):  
         raise ValueError("times must be an int")  
    if not (isinstance(data,int) or isinstance(data, str)):  
        raise ValueError("data must be an int or a string")  
    if times &lt;= 0:  
        return []  
    return [data] + recursive(times, data - 1)  

print(recursive(3, 5))
</code></pre>

<p>Why is the code throwing a recursive error?</p>

<p>Let's try to think how we would repeat any data item N times recursively:</p>

<ul>
<li>If <code>times</code> is 0 or less, we return an empty list, as per the requirements.</li>
<li>If <code>times</code> is greater than 0, we return list that has <code>data</code> ones and another <code>times - 1</code> repetitions of data, recursively. </li>
</ul>

<p>Another requirement is to check the validity of the arguments and raise a <code>ValueError</code> if they are invalid. While this can be done in the same recursive function, this carries a performance hit, as we'll do the same validation <code>times</code> times. The textbook solution for this is to separate the function to two - an "outer" function that handles the validations and and "inner" function that handles the recursive logic. </p>

<p>Put it all together, and you'll get something like this:</p>

<pre><code>def replicate_recur(times, data):
    if not isinstance(times, int):
        raise ValueError("times must be an int")

    return real_replicate_recur(times, data)

def real_replicate_recur(times, data):
    if times &lt;= 0:
        return []

    return [data] + real_replicate_recur(times - 1, data)
</code></pre>

python recursion