Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
Variable n contains the number of digits of the input number, it is incremented in
digits[n++] = number % 10;
.The comparison in the later loop is between the current iteration aka digit with the total digits n (minus 1 because i starts at 0) and divided by two because you are "cutting" the input number in two halfs: the left and the right one, so your sums will be only between the n/2 digits from each side.
The left digits sum is performed from left to right (hence
sumLeft += digits[i]
) and the right digits sum is performed from right to left (hencesumRight += digits[n-i-1]
-- note the n-i subtraction).Hi! Could someone please explain this last loop.
for (int i = 0; i < (n-1)/2; ++i) {
sumLeft += digits[i];
sumRight += digits[n-i-1];
}
there is variable n which equal zero. isn't it? so why was it compared with i and why reduced it by 1 and divided by 2? I will highly appreciate!
Well said yer83. This awnser should be on the top.
I prefer such solution since it's pure arithmetical without any library links that can be useful on some other systems when you can't use specific libraries.
So neat, so clear. Thanks for education.
This is not best practice.
Will you consider the Uppercase letter the same as the lowercase version or a different one?
Still, not the best practice in C. I think is not a big deal just put there a comment above the function declaration.
Well, memory leaks is my headache! You shouldn't have to worry about it. Your job is to give me solution =)
Yes, I know that those functions (array2StringData and sortedList) are not to use in the implementation.
What I'm saying is that is not explicit that the memory allocated by our implementation (cartesian_neighbor) will be freed afterwards.
If I'm allocating memory in my functions, I like to know if that memory is then freed in the program, so to avoid memory leaks.
A simple comment above the cartesian_neighbor declaration should be enough.
See, for example, https://www.codewars.com/kata/directions-reduction/discuss/c test case.
This comment is hidden because it contains spoiler information about the solution
How is this a Best Practice?
For this particular use of the function is not necessary to allocate a global var.
In C, you should state that the returned array is deallocated after is used by the other functions.
This is not clear in the test cases.