You do an exercise you've done thousands of times already - curling a dumbbell, but you persist at it - why? Because it still has value.Why is it different with cognitive skills? Why do we think we don't need to repeat past homework problems? Exactly - old assignments and problem sets are a valuable resource for preserving your cognitive ability in a given skill area.
Now that I'm trying to improve my skill at good ol C code (we'll get to why laterπ), I've decided to tap into CodeWars for some daily drills. I asked chatG if that was a good idea - yes!
Here's an excellent one, with so many ways to trip up:
You are given a string containing space separated integers. You are to return (in a string) the max and min.
There's one old fashioned way to do it:
(it goes without saying that your outer loop is going to check if you're at end of the input string)
Loop through the string and capture any non-space character in your "buffer". If you do see a space, then take what you've accumulated in the buffer to atoi (stdlib.h) and then update your min and max.
When you're done with the loop, you just send out what you have in min and max.
How many ways are there to mess up something so simple? More than you think! For a start, if you have a Udacity-style fill-in-the-blanks exercise, it's hard to slip up. But, here, you're doing everything from scratch.
Do you know which you should use?
char *buffer
or
char buffer[]
One is a string and the other is an array of characters. What's the difference? Right? If you go with a string, you can't modify it without the mess of malloc, free. If you go with a char array, you waste some space. Which is better? π
If you're gobbling up characters from the in_string into buffer, did you remember to increment the buffer pointer? Check that you aren't exceeding your allotted storage?
Did you remember to increment the in_string pointer? Where in the loop? Only in one place?
How are you sending min and max to the update helper function? That's right - if you want MORE than one variable updated, you need pointers - and have to send the references to the helper. But, now, what about allocating the memory that these pointers point to. See? Not that trivial - better think twice before going down the "more readable" helper function path.π
You'll get it to work and it builds some confidence. Without calling it that, you're using whitespace to parse - but, what about how you end - are you unintentionally requiring the last character to BE a space so that you get the last integer? See? So many things you don't see the first time - but, chatGPT sees it all!
Then, you look at someone else's solution and - wow - the atoi() in stdlib does way more than you imagined. It's not demanding just "23". You can give it " 23 32 44" and it'll return 23! Use that to your advantage!
So, now, you don't even need a buffer - you just loop through in_string and, each time you're NOT at a space, you gobble up non-space characters (after you've called atoi for a "bite" that is) till you get to a space.
So simple and elegant!