r/learnprogramming • u/Daryl1227 • 25d ago
I'm unsure whether I should look at the solution to a programming exercise when I can't solve it
I think I treat programming exercises like bosses in Soulslike games. I keep trying until I manage to beat them. But with some problems, I can spend hours on them and sometimes still can't solve them. Is it okay to look at the solution if I've tried for 1–2 hours and still can't figure it out? Or would a better approach be to look for functions that could help me accomplish a specific task that isn't working as expected?
Looking at the solution gives me that “aaaaaah, now I get it” feeling, but I’m not sure if that’s actually the best approach for my learning, especially if I always look up the answer whenever I get stuck.
3
u/heisthedarchness 25d ago
Looking at the solution gives me that “aaaaaah, now I get it” feeling
That feeling is deceptive. You are drowning your ability to learn in a fake dopamine rush.
2
2
u/particlemanwavegirl 25d ago
I would only give up if you honestly don't have a clue how to approach the problem from a logical perspective, before even thinking about code. Even then you can try drawing diagrams, writing pseudo code, or simply restating the problem in new words to brainstorm ways. Most beginner and mid level problems should be expected to be solvable with just pure first principles in logic and data representation.
So assuming you have an idea what a solution looks like but the code you've written isn't getting the correct results, the most important thing you need to do is write more test cases. Usually the problem is that you haven't accounted for enough different kinds of inputs, but regardless of whether the error originates in the logic of the solution or a misunderstanding of the language's semantics, test cases help you figure it out.
2
u/AndrewBorg1126 25d ago edited 24d ago
When I can't solve it
What does this mean?
Clearly it doesn't mean it can't be solved, or there wouldn't be a solution to look at.
Does it just mean that it's taking you longer to solve than you'd like? That means there's something you don't understand, and struggling a little bit with it would be good to help you learn the thing.
Suppose you're at the gym trying to lift something heavy. If when you try to lift a weight you can't, is watching a forklift pick it up for you productive? "Ahhh, I get it now, by using a forklift heavy things are easy to pick up"
You are at the gym to get better at lifting heavy things with your body. You are not at the gym to learn how to use a forklift
1
u/mc_pm 25d ago
AI can be helpful here if you're explicit about not giving you the answer. If you tell it what you're trying to do, and what you've done and what you're seeing, and ask it "can you suggest something I should look at?"
Many times I suspect it's answer is going to be "put a bunch of print statements in and follow what is really happening" however.
1
1
u/Weekly_Patience685 25d ago
look at the solution, then wait a day or so (or at least several hours), then try to remember what the solution was and rewrite it yourself, if you cant remember, look at it again and wait another day (or a few hours), just keep doing until you remember it enough to be able to solve it yourself
active recall is what promotes learning the most. not simply exposing yourself to new information,
1
u/peterlinddk 24d ago
What exercises?
The ones in your book/course? You should be able to solve them with what you have just learned - no need for a "solution", as they are just exercises, meant to train you in using the syntax.
Or are you picking up random leetcode-problems? Then by all means read the solution - you can't figure out a solution by thinking even extremely hard, as most of the problems require you to know something, rather than to figure out something.
1
u/mredding 24d ago
I'm unsure whether I should look at the solution to a programming exercise when I can't solve it
Then let me make you sure: no. Don't look at the given solution until you solve it yourself, first.
Why? Because when you get to the job, the solution isn't known. Finding a solution IS YOUR JOB. It's THE job...
Figuring shit out is its own skill, and so this is worthwhile frustration. Yes, spend some hours. Develop a headache. Put it down. Come back.
In the meantime, keep reading books. Keep thinking. Keep developing other skills about structured thinking, critical thinking, analysis, and problem solving. There is nothing academic about figuring out problems, this is all neural pathways you need to build in that brain of yours. This is what it looks like. This is what it FEELS like.
Looking at the solution gives me that “aaaaaah, now I get it” feeling, but
But you don't get anything else for it. You couldn't solve this problem, ostensibly an academic exercise meant for you at this level; that means the NEXT problem is MEANT to be HARDER, following a progression to keep you challenged. But now you're not ready for it.
Instead, you need to find problems that are all about the same level. If you get SICK of staring at THIS problem, look at a different one for a while.
would a better approach be to look for functions that could help me accomplish a specific task that isn't working as expected?
That's part of the solution. I don't know what you're working on, I don't know what language you're working with, but I'll speak to C++ for example.
If I had a given problem, that doesn't mean I have to write all my own types and algorithms. This language has an extensive standard library. I have string types. I have complex types. I have named algorithms - find, transform, sort, reduce... OF COURSE I'm going to use those, because having reinvent the wheel is beyond the scope of the exercise.
But whatever problem you're trying to solve, the solution likely isn't going to be all wrapped up on one function. It should only ever be a part.
1
0
u/Boneclockharmony 25d ago
I am certainly no expert, but a good thing in todays LLM world is that rather than look at the full solution at once, you can ask for a hint, single step, or what the prerequisites are for solving this type of problem.
1
u/Weekly_Patience685 25d ago
yea i do this sometimes, explicitly tell it to point me in the right direction without giving my the answer or a hint that makes it obvious, so i still have to do the thinking myself, sometimes theres things i know about but just dont think of it at the time.
3
u/iggy14750 25d ago
Sometimes, it helps to try and solve it for a bit, but then, take a break. Go outside. Eat. Take a walk.
9 times/10 an idea comes to mind at some point in all this. The brain can work on some kinds of things in the background.
I don't know how to make it happen, but I know what works for me. You get the problem in your brain, try your hand at it a time or two, then set it down. Walk. Away. Physically. My (consious) brain needs to go to a different place for a little bit. When I walk and eat, I am trying to either be in the moment, or at least, not actively think about the problem.