Created
March 31, 2011 02:23
-
-
Save jamalsa/895716 to your computer and use it in GitHub Desktop.
This is simple python script to display all possible permutation from sequence of number with repeated element
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Problems: Given list of sequence number from 1 to n. Generate lists of all possible permutation with | |
repeated element. For example : | |
[1,2] => [1,1], [1,2], [2,1], [2,2] | |
[1,2,3] => [1,1,1], [1,1,2], [1,1,3], [1,2,1], [1,2,2], [1,3,3], [1,3,1], [1,3,2], [1,3,3], | |
[2,1,1], [2,1,2], [2,1,3], [2,2,1], [2,2,2], [2,3,3], [2,3,1], [2,3,2], [2,3,3], | |
[3,1,1], [3,1,2], [3,1,3], [3,2,1], [3,2,2], [3,3,3], [3,3,1], [3,3,2], [3,3,3], | |
''' | |
def repeated_permutation(number, counter = 1, lst = []): | |
if counter <= number: | |
for i in range(1,number+1): | |
lst.append(i) | |
repeated_permutation(number, counter+1, lst) | |
lst.pop() | |
else: | |
print lst | |
# Example | |
# repeated_permutation(4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment