Began to solve problems with Codewars. The problem is the following. A certain friend has from m to n zlotys. If he buys 9 cars worth zł each, he will have 1 zloty left. If he buys seven boats worth b each, he will have 2 zlotys. It is necessary to find all the options that list the amount of money, the cost of cars and the cost of boats in the format of a string of this kind.

 howmuch(1, 100) => [["M: 37", "B: 5", "C: 4"], ["M: 100", "B: 14", "C: 11"]] 

Solution I wrote the following

 char *howmuch(int m, int n) { char promezh[1000]; char var[1000]; char *rez = var; snprintf(var, 1000, "["); for (; m <= n; m++) { for (int c = 1; c <= m; c++) { for (int b = 1; b <= m; b++) { if ((m - 9 * c) == 1 && (m - 7 * b) == 2) { snprintf (promezh, 1000, "[M: %d B: %d C: %d]", m, b, c); strcat (var, promezh); } } } } strcat (var, "]"); return rez; } 

The code that calls this function for tests such

 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <criterion/criterion.h> char* howmuch(int m, int n); void dotest1(int m, int n, char *expr) { char *act = howmuch(m, n); if(strcmp(act, expr) != 0) printf("Error. Expected %s but got %s\n", expr, act); cr_assert_str_eq(act, expr, ""); } Test(howmuch, ShouldPassAllTheTestsProvided) { { char* ans1 = howmuch(1, 100); char* sol1 = "[[M: 37 B: 5 C: 4][M: 100 B: 14 C: 11]]"; dotest1(1, 100, sol1); } { char* ans1 = howmuch(0, 200); char* sol1 = "[[M: 37 B: 5 C: 4][M: 100 B: 14 C: 11][M: 163 B: 23 C: 18]]"; dotest1(0, 200, sol1); } { char* ans1 = howmuch(1000, 1100); char* sol1 = "[[M: 1045 B: 149 C: 116]]"; dotest1(1000, 1100, sol1); } } 

As a result, I get a log

 Time: 694ms Passed: 3 Failed: 1 Test Results: howmuch ShouldPassAllTheTestsProvided Test Passed Test Passed Test Passed Log Error. Expected [[M: 9991 B: 1427 C: 1110]] but got [] The expression (as strings) (act) == (expr) is false. Completed in 687.174805ms 

Please explain where this fourth test is in the code that I cannot pass. Or do I get an error for a different reason? Why I can not pass it, given that when I run this function in Visual Studio Code with the following arguments howmuch (9900, 10000) I get the right answers.

PS Run sample test runs only three options. The link to the task is https://www.codewars.com/kata/how-much/train/c

  • If you get a test report on item 4, and the system does not let you through, then something you do not take into account. Perhaps your algorithm works longer than you can, or it requires more memory than you can. Or there is something you don't know about. - nick_n_a
  • That's just the point, in the task code I don't see the conditions under which I should get the string [[M: 9991 B: 1427 C: 1110]]. And Run Sample Test runs only three described in the code (which can be seen). If the algorithm worked longer than necessary, I would be notified about this (1200ms limit). - Alexey
  • an obvious error - you return a pointer to the data allocated on the stack ... besides this, you use strcat () dirty, and the algorithm itself is inefficient, although it should work for small input values. - Fat-Zer

0