Ad
  • Default User Avatar

    Heck I missed the isdigit() utility, it might have made my solution much simpler...

    The only improvement for your solution: rather than using magic numbers to convert char to int, You can literally do ISBN[i] - '0'.

  • Default User Avatar

    p is declared and initialized in the initialization statement of the for loop:

    for (char i = 0, *p = y; ...; ...) {
    
  • Custom User Avatar

    C language: my code is running smoothly on the terminal, giving right results, but on kata, right on the second sample test I get the following error:

    Test Crashed Caught unexpected signal: SIGSEGV (11). Invalid memory access.

    Another interesting behaviour is that I passed 108 tests when I attempt, being 100/100 from "random tests" and 7/7 from "futher", and 1 sample.

    I'm probably messing up with dynamic memory allocation. But since it works on terminal, I don't know what is it.

  • Custom User Avatar

    Question: why don't you have to call variable p before using it?

  • Custom User Avatar

    Thank you very much, that solved the problem.

  • Default User Avatar

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

  • Default User Avatar

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

  • Custom User Avatar

    C language: I don't have much experience in C, so I feel the code is badly written and I'm messing with the dynamic memory allocation.

    Tried, and passed the 4 tests. But, when I attempt, I pass 3 tests and then get the error message "Test Crashed
    Caught unexpected signal: SIGSEGV (11). Invalid memory access."

    Observation: the only dynamic memory that is not freed is the return varible, since they tell that the function result will be freed. All the other dynamic memory allocated are freed in each iteration.

    Can anybody give me a hint on where I'm messing this up? In the mean time, I'll try to do a non-iterative version.

  • Default User Avatar

    this fixes the symptoms, not the condition. your code would still fail for very large ints. UINT_MAX is just twice as large as INT_MAX, not 10 times as large. if you want to count the number of digits of a number, you should rather divide than multiply

  • Custom User Avatar

    Thats great! I solved the problem turning 'i' into unsigned int. Thank you very much =)

  • Default User Avatar

    for (int i = 10; i <= n; i *= 10, digits++)

    if n is very large, there is no power of 10 larger than n that can fit inside i, so this loops forever.
    for example, if n is 2,000,000,000, the next largest power of 10 is 10,000,000,000, but INT_MAX aka 2 ** 31 - 1 is 2,147,483,647

  • Custom User Avatar

    Hi. The description may be unclear about that, but there is no obligation to use recursion to solve this problem. However, it's possible to use recursion to solve this problem. You should try to print stuff from your code to try to catch when your code doesn't act as it should (use printf).

  • Custom User Avatar

    C language: passing tests, but when attempting I get the "Execution Time Out", and only around 10 tests passed. Using recursive function as requested, any hypothesis as why thats happening?