Created
April 30, 2020 11:09
-
-
Save jatinsharrma/215fc4f7fe2258445adc76906fab36f4 to your computer and use it in GitHub Desktop.
Permutation Function
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
#/////////////////////////////////////////////////////////// | |
#-------------------- 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