simple programming fun

Mar 21, 2005 22:39

i don't think many of you will care enough to enjoy reading this, but here it goes. i'm explaining to someone how i managed to generalize a single piece of code by introducing only one variable so that i don't have to write the (almost) same thing twice. these are the things that make programming fun; and by the way, hindsight is easy: it's a little harder to come up with something like this than to understand it--it took me about 10-15 minutes to think it up and convince myself it worked, even though it's just algebra stuff.

ipeerazorblades: basically, i need to repeat a process that brings a number, x, closer to a number y, and i need to stop it when it gets past y.
ipeerazorblades: this is done differently depending on whether x>y or xy, i have to tell the computer 'do this while x>y and decrease x (so i can bring it closer to y)'.
ipeerazorblades: see that? well, i found a way of doing those two things with a single instruction.
ipeerazorblades: what i did was create a variable, q, that has the value 1 when xy, and gave the computer the single instruction of 'do this while q*xy', i say instead '... while q*xy. so i can use the same statement for both. tah-dah!
ipeerazorblades: i think goats are great.
Previous post Next post
Up