first, you spend a few hours thinking about the problem, not in terms of java or C++ or Ada, but in whatever natural language you choose.
You write down on paper or in a text document the steps you would personally take if you had to do this.
You revise those steps several times, making them clearer and more detailed.
Once that is done, you simply convert it into code, since each line should be simple enough it translates almost directly into whatever language you want to use.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Joined: Jun 06, 2012
Bear Bibeault wrote:Where's your code? What problems are you having with it?
Please follow the link in my signature below or the one in Bear's signature above for some guidelines on how to ask questions in these forums so that you can increase the likelihood of getting an answer.
Thanks and we hope you have a wonderful experience in our community.
Rahul Abrol wrote:I found this program on net but unable to understand...
I wouldn't bother trying...that not the best way to learn.
A permutation means a re-arrangement of the 'things'. if you have a number like 123, you have three things: the digit '1', the digit '2', and the digit '3'. and you have correctly identified all the possible permutations of that in your prior post.
How does one do this? As I explained above, you start by thinking about it and writing it down in English (or whatever). There is no code at first. I would start like this:
1) get the number
2) pick it apart into pieces
3) print all permutations.
So now I have three slightly simpler problems. All three need to be solved, but also note that all three are independent of one another. So, I'd focus on part 1, and refine it into steps:
1a) prompt the user for input
1b) read value in
1c) validate data (i.e. error if they input something with non-digits).
Ok..so 1a and 1b are pretty straightforward (well, 1b may be a little tricky if you've never done any code, but 1a is covered in just about every book on page 1). So i'd now revise 1c - or possibly decide that I don't care about validation and cross it off.
you do this for each and every step. break down part 2 into pieces, then revise again. Then start on part 3.
Work on that, post what you get, and we'll be happy to help some more.
I bet this could be solved without resorting to String manipulation; just using plain arithmetic operations and recursion. Probably not for beginners but surely not that difficult of a challenge... any takers?
Junilu Lacar wrote: I bet this could be solved without resorting to String manipulation; just using plain arithmetic operations and recursion. Probably not for beginners but surely not that difficult of a challenge... any takers?
It might be possible, but it would also be bad-faith code. The task is to rearrange the digits, and our code should express that with maximum clarity. Using arithmetic to accomplish the rearrangement would be adding an unnecessary and confusing layer that would serve to conceal the actual intent.
Dennis Deems wrote:...our code should express that with maximum clarity. Using arithmetic to accomplish the rearrangement would be adding an unnecessary and confusing layer that would serve to conceal the actual intent.
Maybe code like this to reveal intent?
subject: print out all possible permutations of the number?