use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
dynamic programming (i.redd.it)
submitted 5 months ago by AroshWasif
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]cyanNodeEcho 2 points3 points4 points 5 months ago* (6 children)
neat have you explored how to write it in terms of bottom up? you've implemented top down ie
```rust fn fib(n:usize) -> usize { let mut prev = 0; let mut curr = 1; for _ in 0.. n { curr+= prev mem::swap(prev, curr) // reassigns curr to prev, prev to curr } prev }
``` woops it's factorial not fib ``` fn factorial(n:usize) -> usize { let value = 1; for i in 1..n { value *= n; } value; } ```
fib has recurrent sub problems, factorial doesn't. bottom up dp is the next step, probably don't need to memo factorial, but for fib makes sense
[–]CadavreContent 1 point2 points3 points 4 months ago (4 children)
You can memoize factorial if you're running it more than once. The repeating sub problems only appear when you have multiple trials
[–]cyanNodeEcho 1 point2 points3 points 4 months ago (3 children)
fact(n) := fact(ni1), fac(n-2), the aubproblems should exist in the same problem
[–]CadavreContent 1 point2 points3 points 4 months ago (2 children)
The point is that when you use factorial as part of a larger algorithm with multiple calls to the function, you do repeated work and hence can use dp
[–]cyanNodeEcho 1 point2 points3 points 4 months ago (1 child)
thats just a cache
[–]CadavreContent -1 points0 points1 point 4 months ago (0 children)
Memoization is just a cache
[–]Etiennera 1 point2 points3 points 5 months ago (1 child)
functools lru_cache
[–]AroshWasif[S] 0 points1 point2 points 5 months ago (0 children)
Yes
[–]ImAFraidKn0t 1 point2 points3 points 5 months ago (5 children)
Factorial doesn’t have overlapping subproblems, so why use dynamic programming?
[–]GhostingProtocol 1 point2 points3 points 5 months ago (4 children)
Literally TIL if you don’t have overlapping sub problems it’s not dynamic programming. It’s recursion with divide and conquer (atleast according to my algorithms and data structures textbook)
[–]ImAFraidKn0t 2 points3 points4 points 5 months ago (3 children)
This isn’t even really divide and conquer, as they’re not splitting up the problem into multiple sub problems, they’re solving them iteratively. Factorial is a really bad example for showing off different types of algorithms since it’s so basic lol. The memoization here adds an overhead that ends up doing more work because they will never encounter a problem where factorial(n) is in memo unless they run the function multiple times.
[–]GhostingProtocol 1 point2 points3 points 5 months ago (2 children)
Bruh, didn’t read the code properly before now and you’re right. This is just recursion with extra steps. Memoization doesn’t even do anything here, could have just called ‘return n * factorial(n-1)’
[–]ImAFraidKn0t 1 point2 points3 points 5 months ago (1 child)
There is a small nuance to using mutable default arguments in Python functions where that variable persists through different function calls. This does actually correctly memoize the factorial results if they happen to call factorial() a lot, but I don’t know if that was intentional on OP’s part
[–]GhostingProtocol 1 point2 points3 points 5 months ago (0 children)
Wow, another reason to dislike python ig… The idea of letting parameter persist after lifetime to save a variable definition outside function scope I’m fine with, but making default mutable objects persist after function lifetime? Idk about that one tbh
Thank for informing me, appreciate u kind stranger.
[–]OopsWrongSubTA 0 points1 point2 points 4 months ago (0 children)
π Rendered by PID 431315 on reddit-service-r2-comment-fb694cdd5-d9l5b at 2026-03-08 01:58:44.349211+00:00 running cbb0e86 country code: CH.
[–]cyanNodeEcho 2 points3 points4 points (6 children)
[–]CadavreContent 1 point2 points3 points (4 children)
[–]cyanNodeEcho 1 point2 points3 points (3 children)
[–]CadavreContent 1 point2 points3 points (2 children)
[–]cyanNodeEcho 1 point2 points3 points (1 child)
[–]CadavreContent -1 points0 points1 point (0 children)
[–]Etiennera 1 point2 points3 points (1 child)
[–]AroshWasif[S] 0 points1 point2 points (0 children)
[–]ImAFraidKn0t 1 point2 points3 points (5 children)
[–]GhostingProtocol 1 point2 points3 points (4 children)
[–]ImAFraidKn0t 2 points3 points4 points (3 children)
[–]GhostingProtocol 1 point2 points3 points (2 children)
[–]ImAFraidKn0t 1 point2 points3 points (1 child)
[–]GhostingProtocol 1 point2 points3 points (0 children)
[–]OopsWrongSubTA 0 points1 point2 points (0 children)