The best advice I had (from a theoretician) was that I should not try to randomly fix my code but I should understand why it does not work. How to do that?
Can you prove (in the mathematical sense) that it always does what you want?
Can you prove (in the mathematical sense) that it always does what you want?
The advice is to look at the issue as you would a mathematical problem, critically and analytically. Don't use shotgun debugging or quick fixes, but instead try to understand the code thoroughly, what its goal is and how it accomplishes it. Instead of testing with random data, mentally walk through all the possibilities and branches.
This is akin to making a mental model of what you are trying to do, and then verifying that the model is correct, and that your code matches the model.