Skip to content

Instantly share code, notes, and snippets.

@jamalsa
Created March 31, 2011 02:23
Show Gist options
  • Save jamalsa/895716 to your computer and use it in GitHub Desktop.
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
'''
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