memoization

Memoisation – Bernoulli numbers

Memoisation – Bernoulli numbers Question: I am trying to compute some Bernoulli numbers and am trying to use memoisation. In the example below, I can get the Bernoulli number for 8 when I run 1-7 before it, but not if the cache is clear. What changes would I need to make to run it when …

Total answers: 1

Returning value in a nested function when using memoization

Returning value in a nested function when using memoization Question: I am trying to implement a count variable in the function below using dynamic programming specifically memoization. The method calculates the value in a Fibonacci sequence at a given index. I cannot figure out why the count (in this case the number of times this …

Total answers: 1

Dynamic Programming: Smallest cost path through matrix — memoization?

Dynamic Programming: Smallest cost path through matrix — memoization? Question: Is there a simple way to memoize results? my dynamic programming solution is potentially calling the same function, with the same parameters, multiple times. I think memoizing would add speed. However, I’m not sure what the best approach is. Here’s the original function, which works, …

Total answers: 3

Distinct way to reach top of Stairs | Climbing Stairs Top-Down Approach

Distinct way to reach top of Stairs | Climbing Stairs Top-Down Approach Question: Problem : You are climbing a staircase. It takes n steps to reach the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? Input: n = 3 Output: 3 …

Total answers: 2

why doesn't memoization work in this Python code?

why doesn't memoization work in this Python code? Question: i am following a dynamic programming video. However the memoization for my code is not working. It is not storing any True/False when i print(memo) it is blank. Please advise def cansum(targetsum,numbers): memo = dict() print(memo) # check in memory if targetsum in memo: return memo[targetsum] …

Total answers: 1

Fibonacci Function Memoization in Python

Fibonacci Function Memoization in Python Question: I’m working on a problem in codewars that wants you to memoize The Fibonacci sequence. My solution so far has been: def fibonacci(n): return fibonacci_helper(n, dict()) def fibonacci_helper(n, fib_nums): if n in [0, 1]: return fib_nums.setdefault(n, n) fib1 = fib_nums.setdefault(n – 1, fibonacci_helper(n – 1, fib_nums)) fib2 = fib_nums.setdefault(n …

Total answers: 2

Prevent calling a function more than once if the parameters have been used before

Prevent calling a function more than once if the parameters have been used before Question: I would like a way to limit the calling of a function to once per values of parameters. For example def unique_func(x): return x >>> unique_func([1]) [1] >>> unique_func([1]) *** wont return anything *** >>> unique_func([2]) [2] Any suggestions? I’ve …

Total answers: 3

Best way to 'intelligently' reset memoized property values in Python when dependencies change

Best way to 'intelligently' reset memoized property values in Python when dependencies change Question: I’m writing a class with various attributes that I only want to calculate when necessary (lazy evaluation). However, more importantly, I want to make sure that ‘stale’ values are not returned if any of the attributes that their calculation depended on …

Total answers: 5

memoize to disk – python – persistent memoization

memoize to disk – python – persistent memoization Question: Is there a way to memoize the output of a function to disk? I have a function def getHtmlOfUrl(url): … # expensive computation and would like to do something like: def getHtmlMemoized(url) = memoizeToFile(getHtmlOfUrl, “file.dat”) and then call getHtmlMemoized(url), so as to do the expensive computation …

Total answers: 11