4 ms·
> Revisiting the program a couple of months later (he wanted the grid spacing changed) and dreading to do that work, I was flipping over the manuals, when it su
by therein 8y ago
> Revisiting the program a couple of months later (he wanted the grid spacing changed) and dreading to do that work, I was flipping over the manuals, when it suddently clicked: I could use a variable for the 'line' instructions, and change with this 'for' instruction, which so far had eluded me. A few 'pages' worth of code got translated into 4 lines.
This exact same eureka moment happened to me as a kid when I picked up BASIC in first or second grade.
I was comparing two numbers to figure out which one is the larger one. I put my if statement, and the condition, but then turned out I had another number I had to compare to the larger of the first two. Once I wrote that, I realized I needed another one, and then another one. It struck me that I didn't even know how many numbers I was going to need to compare and it was clear to me that I cannot continue doing it this way.
I started to think what can I do, what if I compared the first one with the numbers after it in the list... Oh what happens on the last number? Do I wrap, no that doesn't make sense, because I did that comparison already.
I know what to do, I should compare each number with all the numbers after it, and pick the largest one from each iteration and place it elsewhere.
This is how I came up with my O(N^2) sorting implementation. Probably the first "general purpose" algorithm I've ever come up with or implemented but I remember the exact feeling of "wow, this changes things".
That realization that I could execute the same subroutine while changing the parameters it is operating on was literally an eye opener at that age and it was so satisfying having come up with it on my own.