Base conversion program: always a good time

I only have one student taking my Python course this semester. He is doing it pretty much independent study because I teach my senior Stats class at the same time. The format is I lift some assignment from the book or I dream up something that will target what I want him to learn. He is self-driven so it works out pretty well. My latest idea to kill several birds with one stone is a program to convert bases. I only want to convert bases 2, 10 and 16 just to keep it manageable. It would be a two-part assignment. The first part would just take the inputs through the usual Python input statement. The second part would be to build a GUI with tkinter so there would be two sets of three radio buttons to select the conversion. No big deal. Should be doable. There is a learning objective of understanding bases, learning and understanding a new package, the coding aspect and the algorithm development aspect. All reasonable I initially thought.

The student had not dealt with bases in a long time so we did a quick review. While doing the review I noticed a pattern I had noticed before but diligently ignored before. Converting from base 10 to any other base is not a big deal. There is a nice algorithm. Converting any base to base 10 is no problem. There is an algorithm. Converting base 2 to base 16 or the other direction directly is a big problem. What I had noticed before was on any conversion I had always had base 10 involved. If I wanted to convert base 5 to base 8 I always converted 5 to 10 and then 10 to 8. Going directly is not so easy. For example, convert 42 base 5 to base 8. Converting the 2 is no problem, it is the ones place and ones are ones so long as the starting base is less than the destination base. (Going the other direction, bigger base to smaller base is going to require an addition to the algorithm to handle when the ones are greater than the possible values for the destination base. Convert 47 base 8 to base 5 for instance. There are not 7 ones in base 5.) The tricky stuff starts when trying to handle the 5s place. 40 base 5 = what in base 8? 4 * 5 = x * 8. Still kind of using base 10 but not quite so directly.

As I type this I can see an algorithm taking shape. The trouble is I do not want to teach the student my algorithm. What I have to figure out is not the algorithm, but how to teach the student to develop the algorithm. I am trying to avoid the standard math class teaching process; here is the algorithm as handed down from on high by the mathematics gods, memorize it and use it over and over until you have it ingrained in your memory. I have a friend that teaches CS at the local university. He states that his classes are full of kids that can program but almost none of them can solve coding problems. Thinking and algorithm development are CS requirements. Two things that kids hate to do and teachers hate to teach. Both are very hard to learn and teach.

Well back to thinking. And on a Friday at that. I am a glutton for punishment.

2 Responses to “Base conversion program: always a good time”

A big question is whether there are limits on the sizes of the numbers being converted. Of course, with Python, integers by default are arbitrarily large precision (rather than mod 2^32 or whatever the machine word size is), so mathematical algorithms can be applied without worrying about word size—you can convert from the external string representation to the internal integer representation, then convert the internal integer to the output string representation. The base only matters in the string representations—the internal representation can be thought of as a pure number.

If you are working in a language in which the internal representation is limited by the word size, then you either have to put restrictions on the size of the numbers you convert or build an arbitrary-precision integer data structure on top of your built-in limited-range representation.

Luckily we are keeping this easy. At least as easy as converting a pencil algorithm to code can be. Even if he does not come up with a good programming solution he will understand base conversions and what base 16 is all about.