mirror of
https://bitbucket.org/Mattrixwv/pyclasses.git
synced 2025-12-06 18:33:58 -05:00
Added a function that gets all permutations of a
string
This commit is contained in:
@@ -219,3 +219,39 @@ def prod(nums: list) -> int:
|
||||
|
||||
#Return the product of all elements
|
||||
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:
|
||||
perms = []
|
||||
#Check if the number is out of bounds
|
||||
if((num >= len(master)) or (num < 0)):
|
||||
#Do nothing and return and empty list
|
||||
perms
|
||||
#If this is the last possible recurse just return the current string
|
||||
elif(num == (len(master) - 1)):
|
||||
perms.append(master)
|
||||
#If there are more possible recurses, recurse with the current permutations
|
||||
else:
|
||||
temp = getPermutations(master, num + 1)
|
||||
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:]
|
||||
#Get the permutations after swapping two elements
|
||||
temp = getPermutations(master, num + 1)
|
||||
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
|
||||
|
||||
@@ -189,6 +189,22 @@ def testProd():
|
||||
if(not failed):
|
||||
print("prod passed all tests")
|
||||
|
||||
#This function tests the getPermutations function
|
||||
def testGetPermutations():
|
||||
failed = False #Holds whether a test was failed
|
||||
|
||||
#Test 1
|
||||
permString = "012"
|
||||
correctAnswer = ["012", "021", "102", "120", "201", "210"]
|
||||
answer = Algorithms.getPermutations(permString)
|
||||
if(answer != correctAnswer):
|
||||
print("getPermutations failed the first test")
|
||||
failed = True
|
||||
|
||||
#If the false was not triggered it must have passed all tests
|
||||
if(not failed):
|
||||
print("getPermutations passed all tests")
|
||||
|
||||
#This runs all of the tests and times them if this script is implicitly called
|
||||
if __name__ == "__main__":
|
||||
timer = Stopwatch()
|
||||
@@ -233,27 +249,36 @@ if __name__ == "__main__":
|
||||
timer.start()
|
||||
testProd()
|
||||
timer.stop()
|
||||
print("testProd took " + timer.getString() + " to run")
|
||||
print("testProd took " + timer.getString() + " to run\n")
|
||||
|
||||
#Test getPermutations
|
||||
timer.start()
|
||||
testGetPermutations()
|
||||
timer.stop()
|
||||
print("testGetPermutations took " + timer.getString() + " to run")
|
||||
|
||||
"""Results:
|
||||
getPrimes passed all tests
|
||||
testGetPrimes took 130.448 microseconds to run
|
||||
testGetPrimes took 429.000 microseconds to run
|
||||
|
||||
getNumPrimes passed all tests
|
||||
testGetNumPrimes took 112.202 microseconds to run
|
||||
testGetNumPrimes took 324.100 microseconds to run
|
||||
|
||||
getFactors passed all tests
|
||||
testGetFactors took 41.105 microseconds to run
|
||||
testGetFactors took 202.700 microseconds to run
|
||||
|
||||
getDivisors passed all tests
|
||||
testGetDivisors took 15.416 microseconds to run
|
||||
testGetDivisors took 205.900 microseconds to run
|
||||
|
||||
getFib passed all tests
|
||||
testGetFib took 3.185 milliseconds to run
|
||||
testGetFib took 1.738 milliseconds to run
|
||||
|
||||
getAllFib passed all tests
|
||||
testGetAllFib took 25.959 microseconds to run
|
||||
testGetAllFib took 215.100 microseconds to run
|
||||
|
||||
prod passed all tests
|
||||
testProd took 9.553 microseconds to run
|
||||
testProd took 207.000 microseconds to run
|
||||
|
||||
getPermutations passed all tests
|
||||
testGetPermutations took 212.800 microseconds to run
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user