mirror of
https://bitbucket.org/Mattrixwv/pyclasses.git
synced 2025-12-06 18:33:58 -05:00
Removed some unnecesary code
This commit is contained in:
@@ -221,7 +221,7 @@ def prod(nums: list) -> int:
|
||||
return product
|
||||
|
||||
#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 = []
|
||||
#Check if the number is out of bounds
|
||||
if((num >= len(master)) or (num < 0)):
|
||||
@@ -236,8 +236,6 @@ def getPermutations(master: str, num = 0) -> list:
|
||||
perms += temp
|
||||
#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
|
||||
cnt = 1
|
||||
#while((num + cnt) < len(master)):
|
||||
for cnt in range(1, len(master) - num):
|
||||
#Swap two elements
|
||||
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
|
||||
#Swap the elements back
|
||||
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
|
||||
if(num == 0):
|
||||
perms.sort()
|
||||
|
||||
#Return the list
|
||||
return perms
|
||||
|
||||
Reference in New Issue
Block a user