Python Recursion vs Iteration
<p>I can't find the error in my code. It runs just fine on my cmd but won't pass the lab platform tests. Here is the question:</p>

<blockquote>
  <p>You need to design an iterative and a recursive function called replicate_iter and replicate_recur respectively 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.</p>
</blockquote>

<p>The function should return an array containing repetitions of the data argument. For instance, <code>replicate_recur(3, 5)</code> or <code>replicate_iter(3,5)</code> should return <code>[5,5,5]</code>. If the times argument is negative or zero, return an empty array. If the argument is invalid, raise a <code>ValueError</code>."</p>

<p>Now here is my code:</p>

<pre><code>my_list1 = []
my_list2 = []
   def replicate_iter(times,data):
      try:
        if type(data) != int and type(data) != str:
          raise ValueError('Invalid Argument')
        times += 0
          if times &gt; 0:
            x = 0
            while x &lt; times:
            my_list2.append(data)
            x = x+1
           return my_list2
         else:
          return []
    except (ValueError,AttributeError,TypeError):
        raise ValueError('Invalid Argument')

def replicate_recur(times,data):
    try:
        if type(data) != int and type(data) != str:
          raise ValueError('Invalid Argument')
        times += 0
        if times &lt;= 0:
          return my_list1
        else:
          my_list1.append(data)
          return replicate_recur(times-1,data)
    except(AttributeError,TypeError):
        raise ValueError('Invalid Argument')
</code></pre>

<p>for the recursive version I recommend <a href="https://en.wikipedia.org/wiki/Tail_call" rel="nofollow">tail recursion</a> as you are already doing, the other examples even if simplest use list concatenation (<code>+</code>) which produce a new list with a copy of the elements of each, that is too much waste of time and space when you can pass around a single list where you add elements to it as you do with a iterative version</p>

<p>here is a example </p>

<pre><code>def replicate_recur(times, data, result=None):
    if result is None:
        result = []
    if times &lt;= 0 :
        return result
    else:
        result.append(data)
        return replicate_recur(times-1,data,result)
</code></pre>

<p>Additionally, you can avoid repeat the same check over and over again by using an auxiliary function</p>

<pre><code>def replicate_recur_aux(times, data, result):
    if times &lt;= 0 :
        return result
    else:
        result.append(data)
        return replicate_recur_aux(times-1,data,result)  

def replicate_recur(times, data):
    if not isinstance(data, int) and not isinstance(data, str):
        raise ValueError('Invalid Argument')
    return replicate_recur_aux(times,data,[])
</code></pre>

<p>this way the main function do all the check that needs to be done and all the necessary setup just once so the auxiliary function do its job without any worry or unnecessary checks of the same thing.</p>

<p>For the iterative version, any of the other examples are good, but maybe your teacher want something like this</p>

<pre><code>def replicate_iter(times, data):
    if not isinstance(data, int) and not isinstance(data, str):
        raise ValueError('Invalid Argument')
    result = []
    if times &lt;=0:
        return result
    for i in range(times):
        result.append(data)
    return result
</code></pre>

<p>now notice the similarity between both version, you can easy transform one into the other </p>

python recursion iteration