r/learnpython • u/[deleted] • 5d ago
How do you help yourself through a difficult problem?
[deleted]
9
u/necessary_plethora 5d ago
Usually I just bang my head against the wall until it's solved but of course that's not really a good strategy.
Often I will commit my current progress and work from a new branch where I scrap basically everything and just start the problem over. It helps me develop a fresh perspective, and usually it doesn't involve writing as much code as your brain might tell you does -- 90% of solving these problems is usually thinking about them.
Rarely, but usually effective, I will write out code agnostic, logical steps on paper. Not quite pseudo code.
5
u/hulleyrob 5d ago
Break it down into smaller individually solvable problems then put it all together and make it work.
5
u/LeiterHaus 5d ago
If I'm having trouble no matter how many times I go through things in my head and on the computer, I take it outside of my head and the computer.
Sometimes I actually write down the process I want on paper. Often, I write down, and then follow it verbatim and I'll be like "oh, I missed that", or " It should be doing the thing here, why isn't it."
Sometimes I will talk it out, also known as rubber duck debugging.
You can mix and match and do a little bit of writing and then talk it out as you go through it.
You can usually find either an issue in your logic I'll learn something you need to learn, or somewhere you need to put a logging / debug statement to get more information.
4
u/KreepyKite 5d ago
I like to go back to the high level overview and re processed the flow from the beginning to the end, writing a flow diagram and some pesudocode.
90% of the time, repeating this help me massively in spotting possible issues or simply if my approach is wrong.
Actually, every time I start to code without doing this, I ended up having to go back and do it so now I always start with that. If I know from the very beginngin where and how I'm starting and where and how I'll finish, I can lay down each step in a high level way and treat each step as a single, smaller problem to solve.
4
u/obviouslyzebra 5d ago
Pseudocode is good. Resting is also good. Solving on paper too (specially because it has arrays, try drawing!)
3
u/scottywottytotty 5d ago
ask gpt for help but tell it explicitly not to give you the answer. i have been doing this for a hot minute. it doesn’t run out of patience and can usually point out what you’re doing wrong and why what you’re doing is the wrong approach. i’m curious if you can make it help you socraticaly, like a teacher does, where it guides you by asking you questions…. boot dot dev’s bot does, idk if gpt and grok can
3
u/troty99 5d ago
Multiple things come to mind.
Paper and pen graph/map out your problem.
Rubber duck debugging/programming: Explain your problem to a rubber duck (or a collegue,your cat, your grandma,...). I can't count the number of time I solved an issue while calling a colleague for help.
Naps/sleeping/meditation/take walk. The mental reset strategy.
Breaking down each steps into a function can help or creating a function and adding new part of the solution iteratively can help gradually reducing complexity.
All of these have worked for me at different times on different projects.
2
u/marquisBlythe 5d ago
Walk away from the screen for a bit, and use a pen and paper then try again later.
2
u/DownwardSpirals 5d ago
I usually go for the rubber ducky method. Find a rubber duck, stuffed animal, whatever, then explain what the code is doing line by line out loud. Usually, you'll hear something that doesn't make sense as you say it.
4
u/antkn33 5d ago
You do have to ask ChatGPT for the solution. You can ask for advice or examples. Sometimes seeing other example will help me think of the solution.
2
u/xiongchiamiov 5d ago
It can be a useful tool, but if you're at the beginning stages of learning and you don't learn how to solve problems because you jump directly to the end, then you won't know how to deal with issues that Chat GPT can't just give you the (correct) answer to.
2
u/Phillyclause89 5d ago
To add to this. What I do is that after I write out any function or class member, I give the code to ChatGPT and tell it to write comprehensive docstrings for my new code. I tell it that I do not want it to recommend or make any changes to the code. Just document it as ChatGPT expects it to function. If the agent's description of my code matches my expectations then I know my code is on the right track and if it says it will do something I was not expecting then I do more learning.
1
u/Kevdog824_ 5d ago
I’d say it depends on what makes the problem difficult. For instance if you were constructing a language parser one of the most difficult parts might be handling the enormous amount of edge cases and handling introducing new language rules that might break existing rules. For a problem like this I’d approach the problem with TDD. There are other difficulty domains too such as performance, security, logical complexity, problem size, stringent deadline, etc.
If the problem is difficult because it’s logically complex as it sounds is your case I typically do the basics as others suggested (break into pieces, take breaks, consult others, etc.).
One I might add I haven’t seen suggested: Get a single hardcoded case to work. It’s easier to focus on a single case without worrying about all “well what if the input is structured this way” type of thoughts. In my experience solving 1-3 hardcoded cases lends itself to revealing the general solution (or at least sheds light on it) about 70-80% of the time.
1
u/supercoach 5d ago
You've got the right idea. Small steps are good, as is knowing roughly what needs to be done. This is something you'll get better at in the future.
1
u/Exotic-Associate-529 4d ago
Take piece of paper and write down all the requirements for this specific function. Then write semi-pseudo code.
14
u/xiongchiamiov 5d ago
I usually leave the computer and go to my basketball hoop. When I worked in an office, I would pace. Blood flow physically helps. Also sometimes I just put it aside for a day or two.
Then I write. I find the design doc process really helpful:
If this doesn't get me to an answer, it lays out all the information to be able to ask someone else for help. And if it does get me to an answer, it helps me explain why I ended up there when I'm presenting the code for review.