Ad
  • Default User Avatar

    It's perfectly fine to iterate over the characters of a line looking for the first comment marker. It's even much more efficient in terms of runtime, see my other comment! And it can be beautiful too, have a look at my solution.

  • Custom User Avatar

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

  • Default User Avatar

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

  • Default User Avatar

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

  • Default User Avatar

    Hi, actually, function 1 is called when python interpret the line that contain @function1. So it call it like this: function1(function2). This is redefining the function2 to what function1 returns.
    If function1 return function2, later calls to function2 will indeed execute the code of function 2.

    In the case of this solution, as memoized function is returning the wrapped function, later calls to fibonacci actually will call wrapped function (that has inside it the original fibonacci function).

    So, when the interpreter reads the line "@memoized", it calls the memoized function (like this memoized(fibonnacci) ). Then it creates the empty dictionary and return the wrapper function. This way the dictionary is not redefined to empty at every call to fibonacci function.
    The dictionary is still saved at the memory and the wrapper function has a reference to it.
    Later call to fibonacci function will indeed call the wrapper function because the memoized functoin redefined it to wrapper function.

  • Custom User Avatar

    Interesting idea! Probably should be more efficient)