aspose file tools*
The moose likes Beginning Java and the fly likes  print out all possible permutations of the number? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark " print out all possible permutations of the number?" Watch " print out all possible permutations of the number?" New topic
Author

print out all possible permutations of the number?

Rahul Abrol
Greenhorn

Joined: Jun 06, 2012
Posts: 4
How to write a program that accepts a natural no. of 4 digit as input and print out all possible permutations of the number. ?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61768
    
  67

Where's your code? What problems are you having with it?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

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
Rahul Abrol
Greenhorn

Joined: Jun 06, 2012
Posts: 4
Bear Bibeault wrote:Where's your code? What problems are you having with it?


I don't know from Where to start?
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5288
    
  10

Hi Rahul,

Welcome to the Ranch!

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.

Junilu - [How to Ask Questions] [How to Answer Questions]
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5288
    
  10

An even better way to make this challenging is to write a solution that doesn't use Strings or arrays.
Rahul Abrol
Greenhorn

Joined: Jun 06, 2012
Posts: 4
first of all I don't understand what is purmutation its (4!=4*3*2*1 or 123 would give

231
321
312
132
213
123)
Rahul Abrol
Greenhorn

Joined: Jun 06, 2012
Posts: 4
I found this program on net but unable to understand...
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Rahul Abrol wrote:I found this program on net but unable to understand...
. . .
I’m not surprised! I can’t understand it. It looks pretty dreadful to me, I shan’t say much more, because Fred has given you such good advice already.
Abdulla S Mamuwala
Greenhorn

Joined: Jul 27, 2009
Posts: 21
Hello Rahul,

How about starting with a 2 digit permutation to write your code, that I think would be easy and then you can add more digits and see how that works.

So if you have two digits 12 then your permutations would be "12","21".

Just out of curiosity and not depriving you of the pleasure of your homework, I came across this URL - List Permuation Algorithm

You could possibly use the weaving concept they are talking about.

To give a better understanding of permutation, I recommend Khan Academy. The stuff on wiki is freaky.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Abdulla S Mamuwala wrote: . . .
How about starting with a 2 digit permutation to write your code, . . . then you can add more digits and see how that works. . . .
Good suggestion.

And welcome to the Ranch
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5288
    
  10

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?
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
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.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5288
    
  10

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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: print out all possible permutations of the number?