Hello! I have made a system that attempts to find all patterns in an arrangement of numbers. For example, I could have 12345, 54321, 13542, or 24531. I have also made a code-cracking block that can give you *maybe give you all password from up to 9 digits long. Here is how this system works:
Create arrangement of numbers into a list
Replace each item in the list with a random number
Compress the list into a single number
Check if the number has already been found
(If number has not been found yet) add number to a seperate list
Umm. Random guessing isn't a good way to find all the possibilities. The number of rearrangements of n values is n!, because first you pick one of the values (n possibilities), then you pick one of the remaining n-1 values, then one of the remaining... etc. So in your five-values example, there are only 5! = 120 arrangements, and in 1000 guesses you're likely to find a lot of them even though some will be duplicates. But 10! = 3628800, so it'd take millions of guesses.
The official textbook way to find all the permutations (that's the technical term for "rearrangements") of a set of values is to divide that set of permutations, in your head before writing any code, into those that start with the first value and those that don't start with the first value. Then figure out how to list all the permutations of each kind, using recursion to solve a smaller problem.
Ok, so I read up on it, and using permutations is basically changing the first number to a random number, but we can no longer use that number. So for the second number, we decide a random number without using the number we already used. Then the third, choose random number without using already used numbers, and so on. Am I right?
I think he meant, in a single permutation you can't reuse a number.
I know; I wasn't saying there'd be duplicates in the final result, just that if you make 1000 guesses that doesn't mean you'll get 1000 unique answers.
Finding the number of permutations is easy; it's n factorial. We're talking about finding the actual permutations. What I was trying to say, to take a very simple example, is that if you're looking for permutations of (1, 2, 3) you mentally divide them into the ones that start with 1 (two of them, (1, 2, 3) and (1, 3, 2)) and the ones that don't start with 1 (four of those). Then write a Snap! expression that reports each of those groups, and use APPEND to get the complete result.
I'm trying to explain this without actually writing the code for you, which would kind of ruin the fun.