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

# print out all possible permutations of the number?

Rahul Abrol
Greenhorn
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
Posts: 65278
95
Where's your code? What problems are you having with it?

fred rosenberger
lowercase baba
Bartender
Posts: 12234
36
• 1
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.

Rahul Abrol
Greenhorn
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
Posts: 8091
65
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 Lacar
Bartender
Posts: 8091
65
An even better way to make this challenging is to write a solution that doesn't use Strings or arrays.

Rahul Abrol
Greenhorn
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
Posts: 4
I found this program on net but unable to understand...

fred rosenberger
lowercase baba
Bartender
Posts: 12234
36
• 1
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
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
Posts: 50702
83
• 1
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
Posts: 21
• 1
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
Posts: 50702
83
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
Posts: 8091
65
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
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
Posts: 8091
65
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?