Ad
  • Custom User Avatar

    I would say yes that pattern recognition is a skill, as well as understanding the problem. Much of the complexity of this problem is in understanding it.

  • Custom User Avatar

    @sweetmercury true, i wished i actually written the resulting output rather than the triangle.

  • Default User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    Please, mark your post as having spoiler content next time, you're disclosing the solution. About your question, yes, that solution is faster and speed is one of the parameters you can measure a solution with.

  • Custom User Avatar

    Read this specially the memory stack part. If you don't return the call, it'll run your code n times, the result will be lost when returning from the stack and the default return value of Python is None. Use Pythontutor to see the difference.

  • Custom User Avatar

    It's not the last line you have to change, only change the line I wrote first there (the line 8 in your posted code), with the other one, do you get why you should do that? Try reading a little more about how recursion works if not. Recursion is not that easy to understand if you're only starting to code, you could have used an iterative solution instead. This will be helpful to you too.

  • Custom User Avatar

    Read my answer again, I told you exactly what was wrong. I wasn't impolite at all.

    persistence(first_number, counter) # call to the function
    return persistence(first_number, counter) # returning the call to the function
    

    I hope this is clear enough.

  • Custom User Avatar

    First, why the downvote? Then I already told you what your problem is, you need to return the recursive call to your function too, not only counter.

    It says it should be getting a 3 and I'm returning a 3.

    Yes, but because you didn't return the function call, that value is lost.

    Are you saying that when I return counter, it's overriding the current value and resetting it back to 0?

    No, I never said that.

  • Custom User Avatar

    As many others, you're doing recursion wrong, not returning the call to the function. Also, read this about using markdown formatting, amongst other things.