Блог пользователя Ahmad_OS

Автор Ahmad_OS, история, 5 месяцев назад, По-английски

Hi codeforces! If you stuck on a problem that you are sure your solution is correct, but in somehow it gets WA, and then you opened the tutorial, so your solution is the same, but you don't know what is the bug, what do you do for this situation? I tried AI agents but most of them stuck also. thx already :)

  • Проголосовать: нравится
  • +22
  • Проголосовать: не нравится

»
5 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by Ahmad_OS (previous revision, new revision, compare).

»
5 месяцев назад, скрыть # |
 
Проголосовать: нравится +21 Проголосовать: не нравится

Think very hard. Or stress test.

»
5 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Some useful things to do are creating many assertions throughout your code (to ensure your assumptions are correct along with ensuring no UB is occurring), running your code with sanitization flags, running your code with and without optimization flags, and finding a reproducible break case. Taking a break then logically stepping through your code can also help.

»
5 месяцев назад, скрыть # |
 
Проголосовать: нравится +32 Проголосовать: не нравится

I stare at the code until the bug gets embarrassed and leaves.

»
5 месяцев назад, скрыть # |
← Rev. 2  
Проголосовать: нравится +23 Проголосовать: не нравится

Depends on the circumstances honestly. If I'm solving outside of a live contest (which I assume is what you are referring to), I usually write a brute force to find a reasonably-sized testcase where my code has incorrect output. Then I debug my code with breakpoints (you can do this with gdb via terminal or using your code editor's native debugging tools) and compare the variable values to what they are supposed to be. This solves ~60-70% of my problems, and if you write a clean enough code then it's not a problem to navigate through it, even if it's pretty long. This tactic also works on OI-style contests, and it's something that I used at IOI to debug a 400 line solution within reasonable time.

  • »
    »
    5 месяцев назад, скрыть # ^ |
     
    Проголосовать: нравится +3 Проголосовать: не нравится

    I second this. If you can find a case that you solve wrong, either by trying a few by hand (works surprisingly well) or with a stresstesting script, stepping through your logic can really help.

    If you don't have a case, like today when I got WA on D (it was because I looped until log(n) instead of log(max a_i)) and that problem is not stresstestable, I tend to just try to read the code carefully and try to find the problem. Unfortunately, this is not very fun and doesn't always work lol

»
5 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

If even the AI is getting stuck, you have to accept the truth. Ask for help from the god

»
5 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

by using a debugger

by putting a bunch of print statements and dry running the code to catch the place where it doesn't behave as intended

»
5 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I have met problems like that many times.In my perspective,the best way to solve them is to rewrite the code.

»
5 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

send it to the community group and beg for help :)

»
5 месяцев назад, скрыть # |
← Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

1 ) Stress test to find a test that has a different answer from the correct solution.

2 ) After you find a test , try to find why is your solution giving that wrong answer (read it word by word) , maybe that would help a bit.

»
5 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

we don't debug, we are the bug xD

»
5 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

When I am doing a problem that is at least partly constructive, I usually try to use the test cases that are provided to "generate" even more test cases to test my code on.

For example, today I practised on the problem "Frequencies" from JBOI 2023. I used the first test case of the example tests and wrote down the arrangements after each step of the construction. I then then tried each of these steps and used it to work out exactly where my error occurred.

It can be harder to make your own test cases for other types of question, but it is still usually the best "first stop" for debugging. However, it can also be good to carefully trace though the algorithm on paper.

If all else fails, you could try to explain your algorithm to someone else. This is helpful as often-times when you talk aloud about the problem, it can help you with spotting errors.

»
5 месяцев назад, скрыть # |
← Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

I hope it somehow pops up in my brain

»
5 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

What agents do you use? Maybe that's the key point.

»
5 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I set my compiler to dark theme because bugs love light.

»
4 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

first by adding many print statements , if not solve then

find the test case on which my code fails (by printing it on codeforces)

»
4 месяца назад, скрыть # |
 
Проголосовать: нравится -10 Проголосовать: не нравится

On Codeforces, you can usually print the wrong testcase you are failing on.

Spoiler
  1. Instead of debugging later, while writing each thing, separate the things you are doing. Suppose you are doing a divide and do it as x, y, z separate things. Use functions. It becomes much easier to verify your assumptions.

  2. Even if somehow things went wrong, think if some condition might fail for a group of tests. Sometimes you solve it for that specific failing testcase say you put extra condition or solve it seperatly and leave it, but it might actually be because of some group of testcases like that. So instead of fixing just that testcase, try to generalize it.

  3. If nothing like the above exists, then once you find a failing testcase, trace it. Print the values and compare them with your assumed values. Since you have the editorial or a similar implementation, compare the prints. If both blocks are supposed to be doing the same thing, you'll usually find where they first differ.

  4. If you couldn't find any failing testcase:

  • If you don't have the editorial, then come from the first, separate your code into blocks and ask:

    • Is this block doing what I wanted?
    • What is this variable used for?
    • What assumptions am I making?
    • Is my assumption always true?

These questions will usually find the bug much faster than changing i to i+1 and hoping it ACs.

»
4 месяца назад, скрыть # |
 
Проголосовать: нравится -10 Проголосовать: не нравится

by debugging it.