File APIs for Java Developers Manipulate DOC, XLS, PPT, PDF and many others from your application. http://aspose.com/file-tools
Big Moose Saloon
 Search | Java FAQ | Recent Topics Register / Login

# Say ( b ) Tips, Tricks, Hints, Help?

Ed Byrnes
Ranch Hand

Joined: Feb 04, 2001
Posts: 33
I have a first working version of assignment Say( b ). I use a recursive approach and break up the billions, millions, thousands, and hundreds into three letter strings and then process those strings. I would like to try the interative approach and process integers instead of strings. I'm stuck and quite low on brains. I've stayed away from it for a few days hoping I would think of something...no luck. If there were a way to cheat, I might be tempted. Any help will be greatly appreciated!
~Ed
David Junta
Ranch Hand

Joined: Dec 10, 2000
Posts: 86
Ed,
First off, will integers work for all the possible arguments? Food for thought.
Next, if you have an argument of 12,345,678, for example, how would you process it to get the correct 3-digit numbers? What would happen if you divided it by 1,000,000? And how would you get the rest of the number you want to work with once you've figured out what the "millions" 3-digit number is?
That should be a good start. Best of luck.
Peter Gragert
Ranch Hand

Joined: Jan 16, 2001
Posts: 421
Somewhere else I reported succes of say, mentioning
two possibilities: I split up a (big) number by integer division and modulo computation. Sheriff P dit it by taking substrings
out of the input.
And I think there is at least a third possibility too (though it was not apreciated by nitpicking) looking at the individual
characters.
I hope that you did not deliberately had choosen 1 april as date of your question ?
Johannes de Jong
tumbleweed
Bartender

Joined: Jan 27, 2001
Posts: 5089
Basically Ed you take the number you start of with and decrease it with the amount just printed. Hope that helps.
And Peter I really believe that the nitpicker had considered your option, thats the way they work around here, they are prepared to learn as well. You passed the assignment accept the solution and move on.
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20074
∞

I have seen the code in question.
The thing is this: There are times when recursion is the best solution: traversing a tree is a good example. This is not one of those times.
Ed has a working solution that uses recursion and is being asked to do it iteratively.
While the integer/long based approach ends up cleaner, the string approach is acceptable. (my earliest solutions were string based)
Ed, consider making a method that accepts strings that contain values of 1 to 99 ....

Don Smathers
Ranch Hand

Joined: Mar 04, 2001
Posts: 31
Ed -- I have worked out two different solutions to this Assignment, the first using an approach similar to yours, that is breaking up the input into a series of three character strings. Works great, but not really optimum if you start thinking about really large numbers ( like 30 digits, or so ).
Better idea, at least for me, was to split up the number to be alphaed (?) into a 3 by N array, then follow up much like the three character string approach.
Joseph Russell
Ranch Hand

Joined: Jan 08, 2001
Posts: 290
In my last attempt I used a switch statement by evaluating the length of the number and then having the number "fall" through the switch statements each time being passed to a method that will "say" the first three digits of the number, cut itand then have it fall back down the chain. However, this the nitpick I received:
<quote>Instead of a switch statement, you could have workIt return a number value to be used in the next call to workIt, and put the number %= ... equation in the method itself.</quote>
I have tried to make a recursive method and am failing at it...then I find this thread that says not to solve this with recursion.
I'm lost.
Donald R. Cossitt
buckaroo
Ranch Hand

Joined: Jan 31, 2003
Posts: 401
If you had a satisfactory program for 4a two thirds of the work is allready done for 4b, what is left is adding the hundreds place and the groupNames of 'billion', 'million' & etc.
Take a look at your 4a result and then ask yourself what you need to do to parse the hundreds for each group of three there are in your argument by using modulo & integer division. The rest is efficiency & protocol (usually the hardest part for me)
[ May 24, 2003: Message edited by: Donald R. Cossitt ]

doco
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9035

10
By the way, if you need a copy of your solution (or the instructor's solution) to Java-4a (Say), just let me know and I'll send it to you.

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Joseph Russell
Ranch Hand

Joined: Jan 08, 2001
Posts: 290
My problem is not how to solve 4b, I solved a while ago. Rather it is do I solve it recursively or not. This nitpick seems to indicate that a recursive method is the way to go while this thread says not too. I do not want to proceed coding this problem until I know for sure what is the most efficient or accepted way.
Nitpick:
"Instead of a switch statement, you could have workIt return a number value to be used in the next call to workIt, and put the number %= ... equation in the method itself."
Marilyn if you could send that to me I would appreciate it. Thanks!
[ May 24, 2003: Message edited by: Joseph Russell ]
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9035

10
Originally posted by Joseph Russell:
My problem is not how to solve 4b, I solved a while ago. Rather it is do I solve it recursively or not. This nitpick seems to indicate that a recursive method is the way to go while this thread says not too. I do not want to proceed coding this problem until I know for sure what is the most efficient or accepted way.
Nitpick:
"Instead of a switch statement, you could have workIt return a number value to be used in the next call to workIt, and put the number %= ... equation in the method itself."

In this particular case an iterative approach works just as well as a recursive approach. A method does not need to be recursive to use a value that a previous call to the same method returns.
[ May 24, 2003: Message edited by: Marilyn de Queiroz ]
Joseph Russell
Ranch Hand

Joined: Jan 08, 2001
Posts: 290
In this particular case an iterative approach works just as well as a recursive approach. A method does not need to be recursive to use a value that a previous call to the same method returns.

Thanks, that gives me an idea. After I finally pass this I would like to try a recursive approach but I would like to pass it first...

subject: Say ( b ) Tips, Tricks, Hints, Help?