I am working on a problem in which I am given a number and need to
find every possible permutation of the digits in that number. For
example, if I am given 20, the answer would be: 20 and 02. I know
that there are n! possible permutations, and I have divided up the
numbers so that each digit is an element in an array. My question is:
How can I loop through this array to generate every possible
combination of a number that is at least 2 digits long but no more
than 6.
I am working on a problem in which I am given a number and
Share
Say the
nindividual digits are in an array of lengthn. Then the problem of generating the permutations boils down to:ndigits as the first digit to print.n-1digits.A recursion.
The pseudocode for such a recursive function
permutewould be something like:Then simply call
permuteand print out the list, or do whatever else with it.EDIT: You also need to handle the edge case of
permuteing 1 digit.I think this is already too much information for ‘homework’ 🙂