r/learnprogramming • • Apr 05 '22

Name one programming/comp sci concept you never understood and if you understand it, try to explain it to them

Name a programming concept such as virtual functions, pointers, certain algorithms, etc that you always had a hard time understanding and why.

Someone else who reads yours and does understand it, try to explain it to them to help them learn

1.6k Upvotes

738 comments sorted by

View all comments

Show parent comments

292

u/procrastinatingcoder Apr 05 '22

It generally refers to keeping a table of previous results, and re-using those for your next calculations. It's nothing special, and I find the term "dynamic" in dynamic programming to be the most misleading thing ever personally.

1

u/Sexual_tomato Apr 06 '22

Oh, so every time I use python's functools.cache decorator I'm doing "dynamic programming". Neat.

I'm self-taught and employed as a developer, but mapping industry terms to what I already know can be frustrating.

I once had an Interviewer ask me what a closure is. I said I had no idea. They explained that it was a function that defines some variable, then returns a function that uses that variable. That's it. After them telling me that, I told them about instances where I've used the concept and typed up a quick example.

Example:

So, in Python, if you ever see a decorator function that takes parameters, it's actually a function that returns a function of one argument but uses those arguments in the returned function body.

def log_this(log_level):
    def do_log(function):
        def wrapper(*args, **kwargs):
            log = logging.getLogger(function.__name__)
            log.log(log_level, f"Calling {function.__name__}")
            result = function(*args, **kwargs)
            log.log(log_level, f"Called {function.__name__}").   
            return result
    return wrapper
return do_log

1

u/procrastinatingcoder Apr 06 '22

That's not exactly it... you should be wary of thinking "i recognize this X, therefore it must be this Y that I knew but didn't know the name for".

Especially beware "using Python's X". You don't know what Python does, don't try to put names to it unless you explicitly define it or the doc says so. Python does a whole lot of magic behind the scenes, either take what's provided as information (doc says X), or build your own X; but don't try to reinterpret something as X because they look similar, it's a very common problem that most people aren't aware of.

A whole lot of stuff gets done for you in Python, and a whole lot of mechanism are in place, so using something is never as simple as it seems. Python does string interning for instance, but only up to a certain length and depending on the sub-version. In layman's words, it means there's only a single copy of every string in the system, even if completely different part of the code use it, so long as it's identical and below a certain threshold (it used to be very small, now it's 4096 I believe, I'd have to double-check)

so, for example:

s1 = "X"*10
s2 = "X"*10

s1 is s2 --> True


ss1 = "X"*4097
ss2 = "X"*4097

s1 is s2 --> False

It also does the same thing with numbers below a certain value (-something to 256, depends, test on your own system). And it goes more complex than that for many things.

A more relevant example would be the one you gave, read up the first line of the documentation:

@ functools.cache(user_function)
Simple lightweight unbounded function cache. Sometimes called “memoize”.

It's memoization, not dynamic programming. Assuming things and making links where there is a tenuous one at best is one of the biggest tells of a modern self-learned (though many people fall into that self-learned or not).

Also, you shouldn't call things decorator unless that's what they are, and this refers to the "Decorator design pattern".

Same with closure, while it's roughly what you said, you also need to know the implication to the scope, because closure is specifically something that deals with scope at its core. The wikipedia page can give you more information on the subject.

If you want to map "industry" terms to what you know, you should look it up online from mostly reputable sources (there's good blogs out there, or wikipedia articles, etc.) and don't just assume you know or that what someone said in a tutorial (which are usually simplified and where they only tell you half the information that will be relevant to you) was complete.

My little rant about imprecise information and partial knowledge is done, but I do hope you learned a few things.

2

u/Sexual_tomato Apr 06 '22

From my understanding, dynamic programming is the act of caching results of a function call or operation for simple subsets of a problem that overlaps finding an optimal solution to a harder overarching problem, like finding the shortest path between two nodes in a weighted graph. My personal interaction with it comes from the class I took in control theory when I got my engineering degree, but I just called it "don't waste time computing stuff I've already computed otherwise the Matlab code takes all night to run."

That being said, thank you for your really detailed explanation, and I appreciate your willingness to outline all the inaccuracies I glossed over.

I'm aware of all the points you made, and I realize that it painted my understanding in a bit of a dubious light. But, given that I typed all that out on my phone, it was time to get off the toilet, and this is a thread regarding the total lack of basic understanding, I figured the extra nuance would be too much noise. Imo, you adding the extra context in a separate comment is a better way to read it anyway :)

→ More replies (1)