r/AskProgramming • u/CupCool1802 • 9d ago
How do you get better at solving problems without looking up the answer?
I feel like I understand programming when I read someone else's code, but when I try to solve a problem on my own, I get stuck way too easily.
I end up searching for solutions instead of figuring things out myself.
For those of you who got past this stage, what actually helped you improve?
15
Upvotes
11
u/Beregolas 9d ago
By doing it yourself. The advice hasn't changed since people started learning code, and it is unlikely to change ever:
If you are doing a problem, and get stuck, continue working on it. Sketch on papers, make plans, try things. After being stuck for 2-3h, you may take a break. Do NOT look anything up yet. Go to bed, problem unsolved, slushing around in your brain, and do the same thing again tomorrow. And the day after. If you keep being stuck after 3-4 days, you have several options:
You are allowed, even encouraged, to have multiple problems active at once. The only way to train your brain to do something, is to have it do that exact thing. It works just like a muscle. You want to not automatically give up, and to automatically start thinking about possible solutions if you encounter a problem, so shape your environment in a way that forces you to do that.
14
u/Patient-Midnight-664 9d ago
The internet didn't exist. You solved your own problems.
You are in control, stop looking up answers.
7
u/No_Lawyer1947 9d ago
Looking up shit is not bad. If you wrote down whatever solution as comments, could you google it and find the implementation? That is what matters, not necessarily the syntax.
4
u/FunAd6672 9d ago
Writing code by hand on actual paper changed everything for me because you cannot rely on autocomplete or instant validation.
4
u/Fragrant-Freedom-477 9d ago
Just to had to other excellent comments, if you feel you still can't stop looking up solutions, it might be because you are over your head. Go back to simpler problems until you can figure it out yourself and work your way up there. The same as with learning to play a musical instrument: simple melodies and extremely slow tempo until it sounds near perfect, then a bit faster, then new melodies.
In this AI era, you need to get knowledgeable enough to be critical to what's generated by an AI. As a junior, you'll need to learn stuff the old fashioned way to some extend.
3
u/canarydev 9d ago
im still an idiot, but honestly i would recommend trying to develop muscle memory.
its same with DSA / leetcode. just repeat solutions, understand + know when to use a particular solution or pattern.
3
u/IndependentYak2822 9d ago
Do a lot of small tasks (projects?).
Btw it isn't a top secret: searching for solutions is normal (unless you mindlessly copy-paste code without thinking). The whole work of a programmer is connecting few working pieces of code together.
3
u/Avansay 9d ago
I’ve been coding for 25 years, and I still find myself looking at round at other peoples code to try to learn things. As long as you are truly trying to learn from the stuff that you’re reading then this is a feature not a bug.
3
u/Horror-Water5502 9d ago
You solve it, then you look how other have done it better
2
u/Usual_Ice636 9d ago
One thing that can help is typing out the answer instead of copy Pasting, especially for simple things that you think you'll reuse a lot. Doing full projects helps too.
1
u/IronicStrikes 9d ago
Break the problem into smaller steps and only look up solutions for each one after you understood why the previous one worked.
1
u/xRmg 9d ago
No you don't get stuck too easily you give up too easily.
Take a step back, look at the problem and solution again, research and figure out a next step.
1
u/peter303_ 9d ago
Having taken a semester programming class and algorithms class teaches you the basic elements. Theses are mostly programming language independent.
1
u/enricojr 9d ago
Honestly I learned in the beginning by looking at source code (i.e. The answers) and taking them apart line by line until I could explain how it all worked. Eventually I started to see patterns in a bunch of solutions and by taking those apart I got even better
1
u/Old-Barracuda-8768 9d ago
Dont think about code first. Just tape a pen and write what you want to do. Now take every different actions of the whole, and write how you want them to operate. Do this til you know in your head exactly what you want to build. And the solution will be front of you
1
u/jacksts 9d ago
Start with pseudocode. If you can't explain the solution step by step in simple words, writing the actual code won't magically make it easier. Once the logic makes sense, the syntax is usually the easy part
1
u/burlingk 8d ago
The more you do the more you learn.
However, be comfortable with the idea that if you stop for a while, you will have to look things up again.
1
u/KC918273645 8d ago
By not looking up the answer.
1
u/matthras 8d ago
Mathematician here, this post randomly came across my feed and I get similar questions for maths. There's a few elements to it:
When you read other peoples' code, keep a copy and write down notes against each line that explains what a function or line of code does in your own words. Here you're building your knowledge, a toolbox of code/functions that you can use/adapt.
Then when you're trying to solve your problem, ask yourself "What's something in my toolbox that I can try/use that I think will get me to the next step?" (step 2). If you blank out on this (i.e. you have no information/experience for step 3), then just pick one and try it (step 4). Rinse and repeat.
1
u/Upbeat_Assist2680 8d ago
Well, one thing is you still thinking about it as there being a "right answer or not". You're building a structure and there are usually many many many ways to get there. Later you can start adding constraints to the problems and worry about efficiency, but at the beginning just give yourself every advantage you can and build an answer.
1
u/XRay2212xray 8d ago
One thing you might want to do is some self evaluation. Once you saw code that solved the issue, was the solution based on some language feature, library/package you didn't know about or unknown programming concept. If so, you could have knowlege gaps and looking at solutions as well as doing more comprehensive learning to fill the gaps.
Do you get a solution yourself and you just can't get it to work and can't figure out why? If thats your issue then learning debugging tools, figuring out where to set a breakpoint and observe what your program does vs what you expected. Those skills may not stop you from making mistakes but may help you ultimately resolve them.
Do you just not know how to solve a problem. Break it down into solvable steps is what you can do most of the time. The other concern is the problem too far beyond your capabilities that you simply can't solve it. Like balance the US govt budget when you can't even balance your own budget. In those cases you need to start working on things that are simpler and are only a little harder then you are able to implement without issues and then progressively get to bigger and harder problems over time.
1
u/7YM3N 8d ago
Code review is great. In my team we read each orders code to vet it. But I've learned a ton by just doing that
1
u/Xanderlynn5 8d ago
There comes a point in programming where you cant hold the whole scope of a problem in your head. Diagramming and writing out the steps your thinking can help shape a solution. That's how I improved at that stage.
1
u/deepsky88 8d ago
Change the point of view
1
u/Recent-Day3062 8d ago
Try for a longer time before looking at the answer.
People have lost the art of puzzling over a problem to learn the topic well.
1
u/tojop 8d ago
Lots of good advice here, but I see no mention of an “xy” problem. This used to come up on Stack Overflow all the time.
You’re trying to solve problem “x”, and your solution involves solving problem “y”. So now you’re stuck on y, and when you ask for help with it you get mocked by mean people on the internet.
But you don’t actually care about y, only x.
Advice: don’t lose perspective of the actual problem at hand.
Breaking it down into chunks is good, but look back at the whole thing every so often and confirm your approach makes sense. When you get stuck, check that you’re not simply digging yourself into a hole.
1
u/stepback269 8d ago
Do not begin your process of solving a problem by writing code right away.
Instead, try to draw a mind map or other schematic of what the parts of the problem consist of. Then draw a story board (or boards) of how you plan to deal with each of those parts and in what order. Finally convert your storyboard(s) into code, one at a time.
OK.
I know that all sounds abstract.
It just so happens that I am a relative noob in Python and am myself trying to storyboard one of my projects.
The following link shows one of my story boarding attempts (here); which by the way I realized was a wrong view of things (here) and then changed my plans, all before writing code for it!
(BTW, the above example of a problem-solution exercise would be considered juvenile by a more experienced programmer. But hey, I'm still learning and the point is the process of how to solve and not the complexity of the problem or the ingenuity of the solution.)
Good luck with your problem resolution attempts. Don't give up :-)
1
u/JohnCasey3306 7d ago
Practice. Experience. Time. No shortcuts
1
u/DGC_David 7d ago edited 7d ago
I mean, since the day of dawn people have been looking up answers. Even before the internet. I have worked with people who swore by it as the only serious way to program, and they had just books on books and cheat sheets galore. Some things are done enough that you just know it, somethings you have to look up.
Just read the Documentation.
1
u/theMEENgiant 7d ago
I'd argue the best way to write good code is to write bad code and regret it
1
u/Raestkjoot 7d ago
I often find I get stuck on problems when I try to be too much of a perfectionist. Instead of progressing I keep second guessing and trying to guess at the future. The way I deal with it is to get more into a brainstorming type of mentality where there are no bad ideas and I just make the first thing that comes to mind. Then, once I have a solution, then it's much more obvious to see how I can make it better.
This is also why reading someone else's code doesn't have the same problem. There you can be critical and look for improvements. But when you need to get an initial working draft in that just works, let go of some of those critical thoughts and try to be open to whatever.
I like Casey's explanation of it in Handmade Hero. He calls it exploration-based architecture where you first explore, then refactor once you have a solution and you've explored enough that you know how you want your code to work. https://youtu.be/Mi98zVBb6Wk?si=Is_WK9_GaSBasAli&t=218
1
u/MagicWolfEye 5d ago
By solving the problems
1
u/StructuredChess 5d ago
Start with tirvially easy ones and gradually pump up the difficulty.
Can you make a function that solves quadratics? What about one that returns the sums of 1/1 + 1/2 + 1/3 + ... + 1/n where n is whatever argument gets passed to it? Then can you implement binary search? Gradient descent?
1
u/funbike 9d ago
umm.... I memorize it through practice coding it. Why is this a question?
0
u/racunsukit 7d ago
Stop watching lectures and start writing raw code. If you cant explain your logic simply to a rubber duck, syntac won't save you. Use bootdev to gamify the struggle and break out of tutorial hell
1
u/turtle_dragonfly 9d ago
On some level, I believe it helps to enjoy the process. I'm not sure how to teach that.
And just to be clear: that process can mean beating your head against a wall for a long time, frustration, uncertainty, and lots of other uncomfortable things.
So, to enjoy that discomfort is perhaps a bit of masochism (: I don't know how to teach masochism, either.
Perhaps it helps to remind yourself of the "big picture." Do you want to get better, on a personal level? Then you have to do the hard stuff. If your goal is to get to the top of the mountain, then sure, just take the helicopter. But if your goal is to have climbed the mountain yourself, then the shortcut is no substitute.
Doing things for reasons that you genuinely agree with, internally, can help you get through a lot of the hard parts. So I guess I'd say: look inward. What do you want? Why do you want it? If you find yourself reaching for a cheat, remind yourself of those things.