Yeah, I’m a pretty weird dude. I have an entire list of various sayings, quotes, and aphorisms from different people and languages. I'm pretty sure that particular one comes from a Latin book I have called Sententiae by Publilius Syrus. Most of the Latin ones I use are from him. He has a ton of good ones. You should check it out.
Yes, I’ve learned quite a few tricks from Code Golf on Stack Exchange. That said, I don’t post there like I do on Codewars. I just really enjoy trying to find the shortest C solution possible for each kata. It’s not just about code golf though, sometimes it’s about finding something equally as clever, like using shellcode tricks or inline assembly.
It feels like I’m always learning something new from someone’s unique solution. That’s really what I love about Codewars. But you’re totally right, I should visit Code Golf SE more often.
I think you miscounted, moving the body into the for statement doesn't save any characters.
As for K&R style, I don't like it, and it is removed from C since C23.
As for the header, size_t is compiler dependent, so there's no portable way to drop the header. I usually don't care too much about the includes.
I'm pretty sure we can drop the header if we use int instead of size_t especially if negative lengths are disallowed anyway. Hold on let me see if i can come up with a unique idea..
As for your remark on branchless; you need to check your code in godbolt to see what it does; have a look at https://godbolt.org/z/TK1xMj66d
Furthermore, almost all current CPUs have branch prediction, so if you measure performance, it might be different from what you would expect.
I'm pretty sure that my code is shorter even with the include (54 characters), and if we ignore whitespace (71). However, you're right that without the K&R style (no return type) implicit conversion, it would make my code 77 characters compared to yours which was 75. So in a fair competition according to your standards and without all the silly tricks you are right your code would be the shortest. Ç'est la vie (: it's still fun playing around with the technicalities and seeing what is possible! :D
For golfing, I ignore whitespace, and with the new C versions, K&R C isn't supported, so I prefer still rather formatted code.
A recursive solution is fine, but a simple for loop is good as well.
If I compare both, we have:
int sum_digits (int n) { int s=0; for (n<0?n=-n:0;n;n/=10) s+=n%10; return s; }
and
int sum_digits (int n) { return n?abs(n%10)+sum_digits(n/10):0; }
but the include for abs isn't used, if it's added we get:
#include <math.h>
int sum_digits (int n) { return n?abs(n%10)+sum_digits(n/10):0; }
which is actually a little bit longer, hence making my code shorter looking from that perspective.
Yeah, I’m a pretty weird dude. I have an entire list of various sayings, quotes, and aphorisms from different people and languages. I'm pretty sure that particular one comes from a Latin book I have called Sententiae by Publilius Syrus. Most of the Latin ones I use are from him. He has a ton of good ones. You should check it out.
Yes, I’ve learned quite a few tricks from Code Golf on Stack Exchange. That said, I don’t post there like I do on Codewars. I just really enjoy trying to find the shortest C solution possible for each kata. It’s not just about code golf though, sometimes it’s about finding something equally as clever, like using shellcode tricks or inline assembly.
It feels like I’m always learning something new from someone’s unique solution. That’s really what I love about Codewars. But you’re totally right, I should visit Code Golf SE more often.
“Aliquid semper addiscendum est.”
Thanks, first time I've encountered someone quoting Latin proverbs.
If you like code golfing, why not try: https://codegolf.stackexchange.com/ ?
I think you miscounted, moving the body into the for statement doesn't save any characters.
As for K&R style, I don't like it, and it is removed from C since C23.
As for the header,
size_t
is compiler dependent, so there's no portable way to drop the header. I usually don't care too much about the includes.thank you for the upvote
I'm pretty sure we can drop the header if we use int instead of size_t especially if negative lengths are disallowed anyway. Hold on let me see if i can come up with a unique idea..
that's super clever
This comment is hidden because it contains spoiler information about the solution
Really nice! Well done!
As for your remark on branchless; you need to check your code in godbolt to see what it does; have a look at https://godbolt.org/z/TK1xMj66d
Furthermore, almost all current CPUs have branch prediction, so if you measure performance, it might be different from what you would expect.
Nice! That third ternary operation indeed was a "code smell".
On this song, it's like cheating.
On Baby shark lyrics generator, it's like PPE.
I thought it should be possible, but I didn't know and I didn't bother to find out that way to avoid using a temporary file. Neat.
I'm pretty sure that my code is shorter even with the include (54 characters), and if we ignore whitespace (71). However, you're right that without the K&R style (no return type) implicit conversion, it would make my code 77 characters compared to yours which was 75. So in a fair competition according to your standards and without all the silly tricks you are right your code would be the shortest. Ç'est la vie (: it's still fun playing around with the technicalities and seeing what is possible! :D
For golfing, I ignore whitespace, and with the new C versions, K&R C isn't supported, so I prefer still rather formatted code.
A recursive solution is fine, but a simple for loop is good as well.
If I compare both, we have:
and
but the include for
abs
isn't used, if it's added we get:which is actually a little bit longer, hence making my code shorter looking from that perspective.
Both are fine solutions however in my opinion.
thanks! :D
fixed there
Tests for C++ should give the same results as the tests for C for the same inputs. It doesn't.
Loading more items...