Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

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

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
tumbleweed
Posts: 5089
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Trailboss
Posts: 23183
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ....

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
buckaroo
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 9099
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joseph Russell
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9099
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 290
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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...
 
I'm doing laundry! Look how clean this tiny ad is:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic