Saturday, August 21, 2010

Dishes

I've been trying to find "homework problems" to test myself. One of the tests in the book I'm reading was to count all the instances of each letter in a string, and then print out how many time each letter showed up.

This kind of stumped me. My puny non-programmer brain immediately thought of the brute force method - scan the entirety of the text 56 times (a-z and A-Z) and test for each instance of a letter and save that to an individual variable (like numberofa, numberofb, numberofc... etc).

While mindlessly doing the dishes this morning my brain zoned out and made some very jarring connections.

So, I've always known that letters on the screen are numbers that the program knows to convert to a character. Each character on the keyboard (plus non-visible things like tabs, newlines, shift, etc) have ASCII character codes, which (to my understanding) is a standard across all modern machines.

Ok so, what if I treated the letters as numbers? I could scan each character and get the numeric result - but would I just have a bunch of individual variables named "numberof1, numberof2, numberof3"? No. I'd use each number as a position on an array. So my array, call it count[], could have each element of number X incremented each time that same number X is found in the text.

Alright (my brain said, as I scrubbed potato off a pan), that would work, but it's really goofy looking because I'd have a big array that would be unfilled up until I get to the ASCII number range of letters and punctuation. Is there a better way to do this? Well, internally the program doesn't care how empty an array is, or how much space is wasted, but I'm trying to learn good habits. What if I was smart about it and made some assumptions?

Well, I can assume that I only care about letters and punctutation - this means the lowest number I'd be interested in is 33, which is an exclamation point. What I could do is turn that 33 into 0, by subtracting 33. Then it would become the first element (count[0]) in my array. The next number, 34 (AKA double quote) would also be subtracted by 33, and become element 1 (count[1]). Each found number would have 33 subtracted from it. Even better, if the program does come across something lower than 33, I could put a line in that rejects negative numbers (same for numbers too high, just in case).

I might be only decreasing my total array by 32, but it seems like an important lesson to learn just in case. The real victory here was figuring out how to do this with an array, instead of a bazillion individual variables.

Well... maybe the real victory is the knowledge that my brain is on my side - as long as I do something mindless from time to time.

No comments:

Post a Comment