Skip to content

Instantly share code, notes, and snippets.

@jatinsharrma
Created April 30, 2020 11:09
Show Gist options
  • Save jatinsharrma/215fc4f7fe2258445adc76906fab36f4 to your computer and use it in GitHub Desktop.
Save jatinsharrma/215fc4f7fe2258445adc76906fab36f4 to your computer and use it in GitHub Desktop.
Permutation Function
#///////////////////////////////////////////////////////////
#-------------------- Permutation -------------------------
#/////////////////////////////////////////////////////////
'''
For logic : https://gist.github.com/jatinsharrma/88345fea62cb088dfab149d81ded49b5
'''
def permutation(given):
if type(given) == type(2):
return ([int(x) for x in permutationHelper(list(str(given)),0,len(str(given)))])
else:
return permutationHelper(list(given),0,len(given))
def permutationHelper(array,l,h):
result = []
if (h-l) == 1 :
result.append("".join(array))
else:
for i in range(l,h):
array[l],array[i] = array[i], array[l]
result.extend(permutationHelper(array,l+1,h))
array[l],array[i] = array[i], array[l]
return result
print(permutation('abcde'))
print('---------------------------------------------------------------------------')
print(permutation(12345))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment