Removed some unnecesary code

This commit is contained in:
2019-03-25 12:29:07 -04:00
parent fd858c5249
commit 0ea837d86c

View File

@@ -221,7 +221,7 @@ def prod(nums: list) -> int:
return product return product
#This is a function that creates all permutations of a string and returns a vector of those permutations. #This is a function that creates all permutations of a string and returns a vector of those permutations.
def getPermutations(master: str, num = 0) -> list: def getPermutations(master: str, num: int = 0) -> list:
perms = [] perms = []
#Check if the number is out of bounds #Check if the number is out of bounds
if((num >= len(master)) or (num < 0)): if((num >= len(master)) or (num < 0)):
@@ -236,8 +236,6 @@ def getPermutations(master: str, num = 0) -> list:
perms += temp perms += temp
#You need to swap the current letter with every possible letter after it #You need to swap the current letter with every possible letter after it
#The ones needed to swap before will happen automatically when the function recurses #The ones needed to swap before will happen automatically when the function recurses
cnt = 1
#while((num + cnt) < len(master)):
for cnt in range(1, len(master) - num): for cnt in range(1, len(master) - num):
#Swap two elements #Swap two elements
master = master[0:num] + master[num + cnt] + master[num + 1 : num + cnt] + master[num] + master[num + cnt + 1:] master = master[0:num] + master[num + cnt] + master[num + 1 : num + cnt] + master[num] + master[num + cnt + 1:]
@@ -246,12 +244,9 @@ def getPermutations(master: str, num = 0) -> list:
perms += temp perms += temp
#Swap the elements back #Swap the elements back
master = master[0:num] + master[num + cnt] + master[num + 1 : num + cnt] + master[num] + master[num + cnt + 1:] master = master[0:num] + master[num + cnt] + master[num + 1 : num + cnt] + master[num] + master[num + cnt + 1:]
#Advance the counter
cnt += 1
#The array is not necessarily in alpha-numeric order. So if this is the full array sort it before returning #The array is not necessarily in alpha-numeric order. So if this is the full array sort it before returning
if(num == 0): if(num == 0):
perms.sort() perms.sort()
#Return the list #Return the list
return perms return perms