7 kyu

Compare 2 digit numbers

328 of 1,686mbicc
Description
Loading description...
Fundamentals
Mathematics
  • Please sign in or sign up to leave a comment.
  • bobi3f1 Avatar

    Language: C++ Can someone explain why the result is 50% on the first test and not 25%? tester(13,14,"50%");

  • lakshdeep-s Avatar

    The Kata Description can be changed to provide better understanding of the problem

    • Comparison to be done on the basis of digits of the number rather than the Value (A clearer description)
    • This way the Difficulty level of 7 kyu can be justified
  • YinzPixel Avatar

    This should at least be lvl 6 if not 5, its not that its super hard, but to me lvl 7 is supposed to be very easy, where this one needs a little bit of thinking.

  • yLaWy Avatar
  • 66  Avatar

    Language: C++
    Test suit missing the required header std::string

  • akar-0 Avatar

    This comment has been hidden.

  • nomennescio Avatar

    As for the C translation advice from trashy_incel, I strongly disagree with some of his points. See also the discussion on Discord.

    The advice:

    "It is best to write a wrapper function that takes care of calling the user function and performing the assertions (+ memory management & other setup if needed), that way you can re-use it for random tests as well, and you do not need to hardcode the messages"

    is actually what's NOT best to do, as was discussed on Discord. Such a wrapper function hides both the call to the user function and hides how memory is handled from the user, but is required for the user to deduct essential information while solving the kata.

    Having explicit test code also allows the user to modify the test code to add extra debugging in the sample tests.

    It's OK to have preloaded helper functions that allocate or convert complex types.

  • Unnamed Avatar

    With only 2 languages and 1 day of age, the kata already is inconsistent between languages. Either it should be an enum everywhere (in the languages where they are normally used, and C and C++ are similar here) or a string everywhere.

  • trashy_incel Avatar

    several issues with the C setup:

    • error messages are not helpful. they should show the input, expected and actual values. Criterion assertions take an extra printf format string parameter:

      cr_assert_eq(actual, expected, "expected %d, but got %d", expected, actual);
      

      It is best to write a wrapper function that takes care of calling the user function and performing the assertions (+ memory management & other setup if needed), that way you can re-use it for random tests as well, and you do not need to hardcode the messages:

      void do_test (int input, int expected)
      {
      int actual = user_function(input);
      cr_assert_eq(actual, expected, "for input %d, expected %d, got %d", input, expected, actual);
      }
      
    • you should not compare strings with == in C, it just compares their addresses. It works here because of implementation details that are not true in general. Use the strcmp() function to compare strings, but Criterion already has a macro for that : cr_assert_str_eq(actual_str, expected_str)

    • sample/fixed tests should be hardcoded, and not call your solution:

      do_test(21, 22, "50%");
      // instead of:
      do_test(21, 22, solution(a, b));
      
    • your solution should NOT be visible to the user ! it should be marked static, so that the user cannot call it; otherwise, I can cheat the tests like this

    • using strings as constants is not very idiomatic in C, because they cannot be handled with operators and because they are pointers types for which you have to define an allocation scheme. If there are only 3 possible values, it is better to use an enumerated type, for example:

      enum { PERCENT_100, PERCENT_50, PERCENT_0 } ;
      
  • Unnamed Avatar

    Memory management isn't specified in C. The return type is non-const char *, so a static buffer doesn't make much sense, but the sample tests don't free the result, which suggests it shouldn't be mallocced.

  • Unnamed Avatar

    Why short? I'd expect either char (because it's enough) or int (because it's the default integer type), possibly unsigned. But I don't see any reason to use short.

  • 4500zenja1 Avatar

    This comment has been hidden.