The loop # Move every element in the previous generation up one index into the new generation So about 1000 times slower (about 125ms without any lru_cache) rabbit_dynamic_zero_cache.cache_info()ĬacheInfo(hits=0, misses=67185, maxsize=0, currsize=0)īecause of the repeated asking for the same answer (67k function calls instead of 443) %timeit -n 1 -r 1 rabbit_dynamic_zero_cache(45, 27)ġ79 ms ± 0 ns per loop (mean ± std. Return sum(rabbit_dynamic_zero_cache(generation - i, max_age) for i in range(1, max_age)) from functools import rabbit_dynamic(generation, max_age): The generation - 1 is to account for python's 0-indexing dynamic solution:Īnother approach, using recursion and memoization is more in line with dynamic programming. Return nth(rabbits(max_age), generation - 1) Generations.append(sum(generations) - new_borns) Generations = deque(islice(fib(1,1), max_age), maxlen=max_age) The same approach can be used for the second example, but here you can use a double ended queue with a fixed length instead of a tuple. Return next(islice(iterable, n, None), default) I would name them generation and max_age or something generatorsįor the simple fibonacci series, instead of storing everything in lists, only store what you need, and yield the results instead of returning the complete list.Īnd instead of keeping track of how far you are in your list, use an endless generator to yield a stream of fibonacci numbers, and the nth itertools recipe def fib(a=0, b=1):Ĭan be found in almost all python handbooks from itertools import islice Naming the variables clearly and correctly goes a long way in documenting your code. # Put this answer into the list of all answers # Move every element in the previous generation up one index into the new generation # The 0-index of the answer represents newborn rabbits # or the oldest in the previous generation (whichever comes first), produce newborn rabbitsįor j in range(1, min(m, len(previous))): # From age 1 (first reproductive age) to either age at death # Initalize answer as a list of number of rabbits at each age # Each index in the generations will be another array whose indicies represent the ages of the rabbits in that generation I'm not sure how to analyze the time complexity of this problem, but it seems high? So questions are (1) What is the time complexity of my solution? and (2) How can I improve efficiency and readability? def rabbit(n, m): Newborn rabbits keep living, adult rabbits produce themselves and one offspring, and rabbits that are about to die just produce one offspring (see ). In this alteration, rabbits live for a fixed amount of time (input m). If rabbits lived forever, their population sizes would follow a fibonacci sequence. The second snippet tries to use what I learned from the first snippet to solve a related problem. # Fill in the array until you reach the given generationĪllGenerations = allGenerations + allGenerations # First and second generations are trivial Do you have suggestions on how to make my code more readable and more efficient? # Using bottom-up dynamic programming approach, define all fibonacci numbers I think I've done this with O(n) time complexity. The first snippet is just to produce Fibonacci numbers. This post has two code snippets with slightly different but related problems. I'm just moving beyond the standard recursive approach to a lot of simple algorithms. I'm asking because I still don't understand decorators very well and if I knew how to make those 2 versions equivalent that'd help me a lot with them.I would like to improve my code style and its efficiency. But why? Why is simple function composition not working properly and giving a different result? Is there a syntax error in the way I compose those 2 functions? Or maybe the memoize(f) function is just not built correctly for composition? What's even weirder to me is that if you do: fibo(n): Memo = memo_fibo(n - 2) + memo_fibo(n - 1)Īnd this actually works well! Next I wanted to generalize the idea of memoization, I wanted to write a function that basically adds memoization to some other function, so I wrote this: def memoize(f):īut this just doesn't work for some reason, even though I'm just putting one function inside of another. I'm practicing memoization to improve recursive functions, so I wrote this memoized Fibonacci generator: memo =
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |