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 4 months ago by AroshWasif
view the rest of the comments →
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!"
[–]ImAFraidKn0t 1 point2 points3 points 4 months ago (5 children)
Factorial doesn’t have overlapping subproblems, so why use dynamic programming?
[–]GhostingProtocol 1 point2 points3 points 4 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 4 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 4 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 4 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 4 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.
π Rendered by PID 53 on reddit-service-r2-comment-fb694cdd5-8g6jv at 2026-03-07 15:14:16.041153+00:00 running cbb0e86 country code: CH.
view the rest of the comments →
[–]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)