Skip to content

Instantly share code, notes, and snippets.

@ababycat
Created July 16, 2019 02:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ababycat/e6592349c7af6f8baa6f13f3f569251d to your computer and use it in GitHub Desktop.
Save ababycat/e6592349c7af6f8baa6f13f3f569251d to your computer and use it in GitHub Desktop.
Generate permutaion and combination
def permutation(array):
if len(array) == 1:
return [array]
output = []
for i in range(len(array)):
if i == len(array) - 1:
remain = array[0:i]
else:
remain = array[0:i] + array[i+1:]
for back in permutation(remain):
output.append([array[i]] + back)
return output
def combine(array, n):
if len(array) == n:
return [array]
elif n == 0:
return []
output = []
for i in range(len(array) - n + 1):
remain = array[i+1:]
if n - 1 == 0:
output.append([array[i]])
else:
for r in combine(remain, n-1):
output.append([array[i]] + r)
return output
array = [1, 2, 3, 4]
for line in permutation(array):
print(line)
A = [2, 3, 5, 2]
for line in combine(A, 2):
print(line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment