Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

Say 4b question

Ranch Hand
Posts: 76
This is a bit premature, as I'm still getting Leap nitpicked, but I've been looking at the discussions about 4b, and a certain pattern about how to chop it down to size seemed to emerge: find out how big the number is, and have procedures in place for saying it. But what if one as it were didn't know in advance how big the number could be? We could put in more procedures as we found out how big it could be, but in English, we just say what we usually say and use different words, like "trillion" and "quadrillion". So I'm wondering if having separate methods (or parts of methods) for saying gazillions is really a good idea.
I've got my own views on this; any thoughts?

Johannes de Jong
tumbleweed
Bartender
Posts: 5089
Adam you are free to write a class that can handle these large numbers but for the assignment the maximum number you must handle is : 999 999 999 999.

Marilyn de Queiroz
Sheriff
Posts: 9067
12
So I'm wondering if having separate methods (or parts of methods) for saying gazillions is really a good idea.

Sounds like you're considering a different method for each group of _illion's. Why not make one method they can all share?

Ranch Hand
Posts: 76
Both your points are well taken, but I think I've got something else in mind. Let me try to explain myself a little better.
I'm not trying to write a program that can say quadrillions if someone chooses to enter a quadrillion number. Also, it's not that I have separate 'illion methods--I can see that one can write a single method for that. Rather, it's that writing a single method that can detect 'illions up to gazillion still requires writing the math for 'illion detecting and inserting that into the method.
For example, if I wanted to chop 123456789 into three-digit chunks, I'd need to write the math to isolate "123", "456", and "789", and doing that typically involves using math: checking for how big the number is, then doing a bunch of division. If I wanted to expand the number the program could say, I'd typically incorporate a little more math. (Actually, in thinking about this, I discovered a general algorithm for doing this that uses some of the java math package, so we wouldn't really need to do more math; but the math involved is, well, a little involved.)
But I suspect that the way I've been thinking about doing Say--finding out how big the number is in advance then using a method for chopping it into appropriate chunks--is inefficient.
What I was thinking was the following. (I got this idea from talking to another programmer--not a Javarancher--and it struck me as pretty nifty.) Consider that 123456789, say, gets said as:
one hundred twenty-three million, four hundred fifty-six thousand, seven hundred eighty-nine.
There's a pattern for saying this: we say some (at most) three-digit number followed by "million", then "thousand", then nothing. Let's suppose we had a method for saying (at most) three-digit numbers, and a method for adding gazillion-type words to them when they're part of larger numbers. When the number is 4 digits long, we'd add "thousand" to the first three-digit sequence said then run our method for the thousands digit; when the number is 7 digits long, we'd add "thousand" to the first three-digit sequence said, "million" to the second sequence said, then run our method for the millions digit; and so on. The idea is that we would only need to add appropriate words to an array when we needed them, and not alter our methods in any way to accommodate larger numbers. Also, I don't think we'd need to do any involved math except the math to say (at most) three-digit numbers.
Anyway, that's the gist.

Marilyn de Queiroz
Sheriff
Posts: 9067
12
Sounds like a good place to start.

Ranch Hand
Posts: 76
Just finished a running version of Say(b). It's longer than the instructor's solution--139 lines, including 22 comment lines and 17 blank lines--and has only three (not four) methods--one for saying up-to-three-digit chunks, one for saying one-or-two-digit chunks (called in the previous method), and the main method. Even so, I'm pretty pleased at having finished the thing at all, and feel good about the basic structure of the program.
My Say(a) is on the to-be-nitpicked stack, and I know that both it and my Say(b) will get plenty of well-deserved nitpicks, but I feel like shouting my own little accomplishment from the rooftops:
I GOT SAY(B) TO RUN PROPERLY!!!
Btw, there should be a stock phrase for passing Say(b), such as the Zorkish:
YOU HAVE SLAIN THE DRAGON SAY
(preceded by a brief tinny fanfare of trumpets), but other suggestions may work better.

Johannes de Jong
tumbleweed
Bartender
Posts: 5089
Well done Adam, the Dragon is down, now Marilyn only has to check if it's really dead

Carol Murphy
village idiot
Bartender
Posts: 1203
I still feel good about getting Sayb to run at all, and I'm on my 9th (count 'em, 9th) submission! If you get it on the first go, you must be some kind of supernatural being.

Ranch Hand
Posts: 76
Thanks, Johannes, for your support. I still haven't even drawn blood by passing Say(a); still, I'm optimistic. I sense that my Say(a) is a much more efficient program than my Say(b).
Carol, the ninth is the charm! It's funny that you should mention supernaturalness. I often stay up past eleven working on code, which with two young kids is, well, kind of dumb. When I go to sleep my wife growls that I'd better be super-human, because the kids will wake me up at a quarter to six.
More seriously, I'd be astonished if my Say(b) were to pass on the first go. I don't see anything specifically wrong with it, but I'm a programming newbie, so the story of my programming life is as yet mostly the story of someone pointing out something wrong I haven't seen in my code.
Still, I feel pretty good.

Johannes de Jong
tumbleweed
Bartender
Posts: 5089
Wonder if anybody did manage to pass Say 1b on the first attempt?.
Marilyn ?
[This message has been edited by Johannes de Jong (edited May 27, 2001).]

Marilyn de Queiroz
Sheriff
Posts: 9067
12
Nobody has, at least not so far.

Johannes de Jong
tumbleweed
Bartender
Posts: 5089
You mean there is a bright star working his/her way up to Say ?