2

I have task to make function to recursive. I read about it, but I m still not sure.

I have function:

arr = [1,2,3,4,5]

def cube(arr):
    cube_arr = []
    for el in arr:
        cube_arr.append(el ** 3)
    return cube_arr

So this method should go to recursive. I'm not sure what they mean by that, I just did

arr = [1,2,3,4]

def cube(arr):
    cube_arr = []
    if len(arr) == 0
        return None
    else:
        for el in arr:
            cube_arr.append(el ** 3)
        return cube_arr

It was said it has to be Tail-recursive. Anyone have any ideas where should I look? I read about it but I can't quite get it

Pleats
  • 59
  • 9
  • 1
    To be using recursion, your cube function needs to call itself. You're not doing that yet, so you're not yet using recursion – CryptoFool Feb 24 '19 at 20:51
  • Your loop would be replaced with recursion and it's a requirement that you call `cube` from `cube` for recursion. – ChiefTwoPencils Feb 24 '19 at 20:52
  • What's your desired output? – Tom Wojcik Feb 24 '19 at 20:53
  • What you want to do is process what's at either the front or back of your input array, remove that item from the array, and then call cube again from inside cube, passing it the shortened array. Eventually, your array will be empty. Your code has to check for that and not call itself in that case. That's what will stop the recursion. – CryptoFool Feb 24 '19 at 20:53
  • Desired output is to have array with cube values inside it. So basically it needs to convert arr values to cube values. – Pleats Feb 24 '19 at 20:55
  • 1
    You can write the code in tail-recursive **style**, but vanilla CPython itself [does **not** support tail-recursion](https://stackoverflow.com/questions/13591970/does-python-optimize-tail-recursion). – meowgoesthedog Feb 24 '19 at 20:59
  • You appear to be a new programmer. What meowgoesthedog is talking about is somewhat advanced. His point is that @Ankit's answer could cause python to "blow up" if the input array was fairly large. For 10 or even probably 100 input values, you don't need to concern yourself with that. I think it's more important to understand the basics of recursion at this point. - you wouldn't actually want to use recursion to solve the problem you have here. your original code block is what you'd normally do. If you're just studying the idea of recursion, Ankit's solution is fine. – CryptoFool Feb 24 '19 at 21:11
  • @Steve the concept of tail-recursion is itself an "advanced" topic as it concerns low-level optimization by the compiler / interpreter. – meowgoesthedog Feb 24 '19 at 21:14
  • But @meow, this is a new programmer who is just trying to understand the basic concept of recursion. He has to walk before he can run. He might be in a different programming language before he actually uses recursion in real life. It's more important here to understand that you wouldn't use recursion for a simple problem like this, or for any problem that requires many recursive iterations for no particular reason. The answer to the problem you're bringing up, in this case, is not "do recursion differently"...it's "don't use recursion". – CryptoFool Feb 24 '19 at 21:16
  • @Steve while you are correct that recursion is unnecessary in this situation, it is probably a requirement of a homework question. In this instance I believe it would be fruitless to argue against it. – meowgoesthedog Feb 24 '19 at 21:28
  • @meow, you're making my point. I agree that this is a homework question. I'm also pretty sure that the instructor is not going to expect that the student understands the subtleties of tail-recursion at this point in his education. The instructor is teaching programming in general, and recursion in general as a major tool that can be used. The instructor is, I doubt, trying to teach interpreter optimization and the subtleties of structuring code to allow for that. What you're talking about simply doesn't likely matter to Pleats at this point in his career. – CryptoFool Feb 24 '19 at 22:00
  • Thank you all for your inputs. I understand recursion way better now as I was in completely wrong direction. At yes, it's simple homework task that must be in recursive - just for teaching purposes. – Pleats Feb 25 '19 at 07:53

1 Answers1

1

You can simply do this for this function to be recursive.

arr = [1,2,3,4]

def cube(arr,cube_arr):
    if len(arr)==0:
        return cube_arr
    else:
        cube_arr.append(arr.pop(0)**3)
        return cube(arr,cube_arr)

print(cube(arr,[]))

Not only this, but you can implement in a number of other ways also.

Ankit Agrawal
  • 596
  • 5
  • 12