Ad
  • Custom User Avatar

    I keep doing the same thing if the first thing I would do would be to make an exact copy. The thing is the argument passed by value is just your local copy and you can do whathever you want with it without having it an effect for the caller. For the caller it matters little whether you take const & and do a copy explicitly or take argument by value. No changes are visible outside of a function.

  • Custom User Avatar

    Vector v is being initialized inside function move_zeroes in this case.
    Is it correct to return a reference to an object which will be destroyed after leaving this function?

  • Default User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Default User Avatar

    You also modified the input, which was const to begin with.

  • Default User Avatar

    Modulo by a constant { 2^n } will be optimised by the compiler to (x & (2^n-1)) anyway, and x % 2 makes the intention clearer.

    $ echo "f(unsigned int i) { return i % 16; }" | gcc -O3 -xc - -S -o- 2>/dev/null | sed -n /f:/,/endproc/p
    f:
    .LFB0:
    .cfi_startproc
    movl %edi, %eax
    andl $15, %eax
    ret
    .cfi_endproc

  • Default User Avatar

    Agreed. I think it would help, also, to actually be able to downvote best practice.

    A solution that I would like would be that you'd actually have to write a word or two on why you think it is a good solution. Then whomever reads the comments can also value each vote according to his or hers own values.

    Another solution would be that "best practice"-points are awarded for receiving said points from other people and that for each level achieved, you are awarded a number of "best practice"-points to mete out by default.

  • Default User Avatar

    Nice! Similar to my solution, but I forgot about lambdas in C++11 and did not know about emplace!

  • Default User Avatar

    Man, I thought my solution was smart. But of course, this is better. Nice.

  • Default User Avatar

    To me, this is a good solution, uses available STL functions and is quite readable.
    However, it could be improved by not having so much going on on the return-line. This is not a competition to write the fewest lines.
    If you want to be the envy of your coworkers in the future, write readable code.

    Also, this might be a consequence of the platform, but you should never write code in your class declaration. Always, ALWAYS put the code in the implementation.

  • Default User Avatar

    Oh, I don't really know that I had to close the issue, sorry. But anyway, I think there is no idiots on this site and when they see an error they could easily fix the problem in their code and pass all the tests and it's much easier than to make a note about it (that's a low chance, but who knows? ;) ).

    I fixed problems in my code and passed all tests but still can't understand the task. So please, just explain me why two similar chars like 'kk' after the sorting have to become 'k'.

    P.S. Should I open an issue next time if I have a similar problem or just post the comment?

  • Default User Avatar
  • Default User Avatar

    Nice kata that tests a lot of things that will confuse and cause intermittent memory allocation problems, however the tests do not catch these faults.

    You need to add tests that verify that the solutions:

    1. Do not fail to work if the input string lacks null termination (the description never mentions that the string has this, so this should not be guaranteed).
    2. Do not write beyond allocated memory (a lot of solutions forget to allocate memory for null termination when they use it).
    3. Do not overwrite write protected memory (your example tests use write protected memory but currently just crash if the solutions write in them). This might not be solvable (I haven't myself written any kata yet).
  • Default User Avatar

    You are using calloc, which guarantees zeroed memory, however, you are not leaving any room for the null termination. strcpy will not care that you have allocated 1 byte too little and will overwrite whatever is after that piece of memory you just allocated. This is a serious problem! Always use strncpy. However, in this case, strncpy would have refrained from null terminating your string, instead leaving you with an unterminated string for all your printfs and your zero termination check. That this passes the tests is more because the tests are bad rather than that your code functions correctly.

  • Default User Avatar

    You have a bug in your program. Use strncpy instead of copying the string yourself. You are adding one extra character to the end, I presume to get the null termination. However, "malloc" is not guaranteed to returned zeroed memory. There is very likely garbage in that memory so the last byte in that string may not be 0! If you use strncpy, that function will terminate the string for you.

  • Default User Avatar

    Don't be afraid to add spaces and line breaks to make the code more readable. Name your variables well so that they are called what they are.
    When you're working with other people readability is the number one criteria for well written code.
    This even goes for yourself, don't write code that you understand now, write code that you'll understand in a year, when you have to do maintenance on it. :)

  • Loading more items...