memoization

memoization library for python 2.7

孤人 提交于 2019-12-17 04:24:04
问题 I see that python 3.2 has memoization as a decorator in functools library. http://docs.python.org/py3k/library/functools.html#functools.lru_cache Unfortunately it is not yet backported to 2.7. Is there any specific reason as why it is not available in 2.7? Is there any 3rd party library providing the same feature or should I write my own? 回答1: Is there any specific reason as why it is not available in 2.7? @Nirk has already provided the reason: unfortunately, the 2.x line only receive

memoization library for python 2.7

谁说胖子不能爱 提交于 2019-12-17 04:24:02
问题 I see that python 3.2 has memoization as a decorator in functools library. http://docs.python.org/py3k/library/functools.html#functools.lru_cache Unfortunately it is not yet backported to 2.7. Is there any specific reason as why it is not available in 2.7? Is there any 3rd party library providing the same feature or should I write my own? 回答1: Is there any specific reason as why it is not available in 2.7? @Nirk has already provided the reason: unfortunately, the 2.x line only receive

Partial vs function literal when memoize

淺唱寂寞╮ 提交于 2019-12-14 03:45:58
问题 This is giving me a bit of brain thump: user> (repeatedly 10 #((memoize rand-int) 10)) (7 0 4 8 1 2 2 1 6 9) user> (repeatedly 10 (partial (memoize rand-int) 10)) (8 8 8 8 8 8 8 8 8 8) I would like to know if the reason for this is because the function literal (first version) is called/evaluated every time, thus the memoize is "recreated" (re-memorized) each time, therefore not really having any true meaningful "memorization" at all, while the second one with partial is actually returning a

Speeding up solution to algorithm

随声附和 提交于 2019-12-14 03:26:46
问题 Working on the following algorithm: Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. For example: A = [2,3,1,1,4] , return true . A = [3,2,1,0,4] , return false . Below is my solution. It tries every single potential step, and then memoizes accordingly. So if the first element is three, the code takes three

Why need apply in memoization function?

≡放荡痞女 提交于 2019-12-13 16:23:09
问题 I've been toyin with concept of memoization and there are obviously different implementations of it. I put this together and it seems to work fine: Function.prototype.memoized = function(a) { debugger if (typeof cache === "undefined") cache = []; if (cache[a]) { return cache[a]; } else { cache[a] = this(a); return cache[a]; } } Function.prototype.memoize=function() { t=this; return function() { // Resig seems to use this: return t.memoized.apply(t,arguments); // why not simple this: //return

What kind of table structure should be used to store memoized function parameters and results in a relational database?

不羁的心 提交于 2019-12-13 06:06:31
问题 Given an expensive function of n variables that returns a scalar value: f(x1, x2, ..., xn) = y If I wished to memoize this function in a relational database, what kind of table structure should I use, and what data modelling methodologies apply? (Related but from a different angle: What kind of data model models function parameters and results?) 回答1: Depending somewhat on the value of 'n', you can probably model it like this. Assume that the value of 'n' is 137. create table expensive

Memoization when arguments can be very large

余生长醉 提交于 2019-12-12 10:02:40
问题 Let's say I have a referentially transparent function. It is very easy to memoize it; for example: def memoize(obj): memo = {} @functools.wraps(obj) def memoizer(*args, **kwargs): combined_args = args + (kwd_mark,) + tuple(sorted(kwargs.items())) if combined_args not in memo: memo[combined_args] = obj(*args, **kwargs) return cache[combined_args] return memoizer @memoize def my_function(data, alpha, beta): # ... Now suppose that the data argument to my_function is huge; say, it's a frozenset

How should I avoid memoization causing bugs in Ruby?

大憨熊 提交于 2019-12-12 08:34:53
问题 Is there a consensus on how to avoid memoization causing bugs due to mutable state? In this example, a cached result had its state mutated, and therefore gave the wrong result the second time it was called. class Greeter def initialize @greeting_cache = {} end def expensive_greeting_calculation(formality) case formality when :casual then "Hi" when :formal then "Hello" end end def greeting(formality) unless @greeting_cache.has_key?(formality) @greeting_cache[formality] = expensive_greeting

Memoizing SQL queries

谁说胖子不能爱 提交于 2019-12-12 07:59:04
问题 Say I have a function that runs a SQL query and returns a dataframe: import pandas.io.sql as psql import sqlalchemy query_string = "select a from table;" def run_my_query(my_query): # username, host, port and database are hard-coded here engine = sqlalchemy.create_engine('postgresql://{username}@{host}:{port}/{database}'.format(username=username, host=host, port=port, database=database)) df = psql.read_sql(my_query, engine) return df # Run the query (this is what I want to memoize) df = run

calculating catalan numbers using memoization

馋奶兔 提交于 2019-12-12 06:57:44
问题 I am tring to use memoization in order to calculate catalan numbers, but it just does not seem to work, what do I need to change? def catalan_mem(n, memo = None): if n==0: return 1 if memo == None: memo = {} b=0 if n not in memo: for i in range (n): b+=((catalan_mem(i),memo)[0])*((catalan_mem(n-1-i),memo)[0]) memo[n]=b return memo[n] thank you! 回答1: If you call catalan_mem on an n in memo then you replace memo[n] with 0. This is probably not what you intend. Without parsing the logic further,