memoization

Recursion, memoization and mutable default arguments in Python

人走茶凉 提交于 2021-02-19 02:48:52
问题 "Base" meaning without just using lru_cache. All of these are "fast enough" -- I'm not looking for the fastest algorithm -- but the timings surprised me so I was hoping I could learn something about how Python "works". Simple loop (/tail recursion): def fibonacci(n): a, b = 0, 1 if n in (a, b): return n for _ in range(n - 1): a, b = b, a + b return b Simple memoized: def fibonacci(n, memo={0:0, 1:1}): if len(memo) <= n: memo[n] = fibonacci(n - 1) + fibonacci(n - 2) return memo[n] Using a

Is there an easy way to memoize (and flush) properties on Python at object level?

两盒软妹~` 提交于 2021-02-08 10:20:54
问题 I'm looking for a way to cache properties of an object. In my case, I suppose the object can change over the time, so the memoized value for the property should be flushable. In pure python, I want to have a behaviour like: class Foo: def __init__(self, text: str): self._text = text self._bar = None def flush(self): self._bar = None def update_text(self, text: str): self._text = text self.flush() @property def bar(self): if self._bar is None: print('Computing bar...') self._bar = f

Is there an easy way to memoize (and flush) properties on Python at object level?

巧了我就是萌 提交于 2021-02-08 10:20:15
问题 I'm looking for a way to cache properties of an object. In my case, I suppose the object can change over the time, so the memoized value for the property should be flushable. In pure python, I want to have a behaviour like: class Foo: def __init__(self, text: str): self._text = text self._bar = None def flush(self): self._bar = None def update_text(self, text: str): self._text = text self.flush() @property def bar(self): if self._bar is None: print('Computing bar...') self._bar = f

Is the pickling process deterministic?

一个人想着一个人 提交于 2021-02-07 12:26:42
问题 Does Pickle always produce the same output for a certain input value? I suppose there could be a gotcha when pickling dictionaries that have the same contents but different insert/delete histories. My goal is to create a "signature" of function arguments, using Pickle and SHA1, for a memoize implementation. 回答1: I suppose there could be a gotcha when pickling dictionaries that have the same contents but different insert/delete histories. Right: >>> pickle.dumps({1: 0, 9: 0}) == pickle.dumps(

Memoizing decorator keeping stored values

烈酒焚心 提交于 2021-02-07 04:21:13
问题 I have a memoization decorator that looks like this: def memoize(obj): from functools import wraps cache = {} @wraps(obj) def memoizer(*args, **kwargs): if args not in cache: cache[args] = obj(*args, **kwargs) return cache[args] return memoizer However, I'm not sure this function works properly because it seems like to me it recreates cache as an empty dictionary every time the decorated function is called. When I test it with a simple fibonacci function, it does appear to memoize correctly.

Memoizing decorator keeping stored values

孤者浪人 提交于 2021-02-07 04:21:05
问题 I have a memoization decorator that looks like this: def memoize(obj): from functools import wraps cache = {} @wraps(obj) def memoizer(*args, **kwargs): if args not in cache: cache[args] = obj(*args, **kwargs) return cache[args] return memoizer However, I'm not sure this function works properly because it seems like to me it recreates cache as an empty dictionary every time the decorated function is called. When I test it with a simple fibonacci function, it does appear to memoize correctly.

Is there an object-identity-based, thread-safe memoization library somewhere?

一个人想着一个人 提交于 2021-02-06 10:14:45
问题 I know that memoization seems to be a perennial topic here on the haskell tag on stack overflow, but I think this question has not been asked before. I'm aware of several different 'off the shelf' memoization libraries for Haskell: The memo-combinators and memotrie packages, which make use of a beautiful trick involving lazy infinite data structures to achieve memoization in a purely functional way. (As I understand it, the former is slightly more flexible, while the latter is easier to use

Can I memoize a Python generator?

时间秒杀一切 提交于 2021-02-05 20:01:12
问题 I have a function called runquery that makes calls to a database and then yields the rows, one by one. I wrote a memoize decorator (or more accurately, I just stole one from this stackoverflow question) but on subsequent calls it just yields an empty sequence, presumably because a generator's values can only be yielded once. How could I modify the memoization decorator that works for Python generators? I realise I will need to store it in memory at some point but I'd like to handle this

Can I memoize a Python generator?

浪子不回头ぞ 提交于 2021-02-05 19:58:22
问题 I have a function called runquery that makes calls to a database and then yields the rows, one by one. I wrote a memoize decorator (or more accurately, I just stole one from this stackoverflow question) but on subsequent calls it just yields an empty sequence, presumably because a generator's values can only be yielded once. How could I modify the memoization decorator that works for Python generators? I realise I will need to store it in memory at some point but I'd like to handle this

Primitive Calculator - Dynamic Approach

依然范特西╮ 提交于 2021-01-21 05:07:29
问题 I'm having some trouble getting the correct solution for the following problem: Your goal is given a positive integer n, find the minimum number of operations needed to obtain the number n starting from the number 1. More specifically the test case I have in the comments below. # Failed case #3/16: (Wrong answer) # got: 15 expected: 14 # Input: # 96234 # # Your output: # 15 # 1 2 4 5 10 11 22 66 198 594 1782 5346 16038 16039 32078 96234 # Correct output: # 14 # 1 3 9 10 11 22 66 198 594 1782