kumarpratyush4's blog

By kumarpratyush4, history, 6 years ago, In English
  • Vote: I like it
  • +8
  • Vote: I do not like it

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Line 67, you need to read f only once, not for every test case.

  • »
    »
    6 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    still no avail

    • »
      »
      »
      6 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      There is a logical mistake in your code. On line 111, you checked if(k[i].size()==5) so on line 137, you should check if(k[i].size()==1) instead of ==2.

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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

»
6 years ago, # |
  Vote: I like it +20 Vote: I do not like it

I don't know what the particular bug is, but the code is in a sad state.

  • magic numbers like 5, 6, 10, 23, and 119 scattered throughout the code, instead of using named constants
  • copying a part of the code and modifying a bit, instead of thinking a bit more how to parameterize it and turn into a loop body or a function
  • then making changes to some parts but not others, for example, compare g=g+char(t+'A'); to just g+char(t+'A');
  • consistently using one-letter names when the program is 100+ lines long
  • reusing variables, instead of decreasing variable live time to help decoupling
  • as a fitting final stroke, marking C++ code as Java on IDEOne when asking a question

All that adds up, so that when such program works, it's no less than a miracle, usually with a few hours wasted on debugging it. Try working on your coding practices to make programs work routinely, not miraculously.

  • »
    »
    6 years ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    thank you very much for your advice. couldn't clearly understand your 2nd point-(copying a part of the code and modifying a bit, instead of thinking a bit more how to parameterize it and turn into a loop body or a function)-could you give an example?

    • »
      »
      »
      6 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      OK, you took a block like this:

      long code block

      And then you copied it a few times.

      By hand.

      Changed some values on the way (probability of mistake increases with each such change).

      Changed the header for every block except the first one (clearing the variables for reuse, instead of creating them in a scope every time). Again, bear in mind that, in every place you change, there is a probability that your change is incorrect, so more changes means more bugs, on average.

      Then it happens that each block has a bug which was copied to each of them. Once more, fixing the same bug in five places is more likely to go wrong than fixing it just once in one place, as we can see with the g+char(t+'A'); situation.


      Instead, we can stop and think: what changes between such blocks? Looks like it's just a couple of constants, and they can be used as parameters. So you can declare the constants that change as, well, arrays of constants in your program. And then loop four or five times, with the loop body representing one block. Or maybe declare a function instead of a loop body, and call it five times with different parameters. As a bonus, if you declare local variables like v64 v; inside the loop body, or inside the function body, you don't have to clear them before each such block.

      Why would the programmer do the repetitive work of copying, changing, then fixing the bugs in five places? Repetitive jobs are best delegated to the computer, it's powerful at doing them.

      • »
        »
        »
        »
        6 years ago, # ^ |
          Vote: I like it +8 Vote: I do not like it

        Thanks, the irony is i did the same mistake during the contest just after and lost some rating. learnt a very valuable lesson thanks!!