From 4a79343766a63a49afddf9fa53aa1f5a42932cbc Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Fri, 23 Jul 2021 18:20:26 -0400 Subject: [PATCH] Split single large library into multiple smaller libraries --- .gitignore | 2 +- ArrayAlgorithms.py | 38 +++++++ Algorithms.py => NumberAlgorithms.py | 67 +---------- StringAlgorithms.py | 61 ++++++++++ test/TestAlgorithms.py | 45 ++++++++ test/TestArrayAlgorithms.py | 50 ++++++++ .../TestNumberAlgorithms.py | 107 +++++------------- test/TestStringAlgorithms.py | 62 ++++++++++ testStopwatch.py => test/testStopwatch.py | 0 todo.txt | 1 - 10 files changed, 294 insertions(+), 139 deletions(-) create mode 100644 ArrayAlgorithms.py rename Algorithms.py => NumberAlgorithms.py (75%) create mode 100644 StringAlgorithms.py create mode 100644 test/TestAlgorithms.py create mode 100644 test/TestArrayAlgorithms.py rename TestAlgorithms.py => test/TestNumberAlgorithms.py (68%) create mode 100644 test/TestStringAlgorithms.py rename testStopwatch.py => test/testStopwatch.py (100%) diff --git a/.gitignore b/.gitignore index 319c92a..ed98aa4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ #Ignore anything that Visual Studio Code produces .vscode/* #Ignore anything that python caches -__pycache__/* \ No newline at end of file +__pycache__ diff --git a/ArrayAlgorithms.py b/ArrayAlgorithms.py new file mode 100644 index 0000000..a0f5d73 --- /dev/null +++ b/ArrayAlgorithms.py @@ -0,0 +1,38 @@ +#Python/pyClasses/ArrayAlgorithms.py +#Matthew Ellison +# Created: 07-21-21 +#Modified: 07-21-21 +#This file contains my library of array functions +""" + Copyright (C) 2021 Matthew Ellison + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . +""" + + +#This function returns the product of all elements in the list +def prod(nums: list) -> int: + #If a blank list was sent to the function return 0 as the product + if(len(nums) == 0): + return 0 + + #Setup the variables + product = 1 #Start at 1 because of working with multiplication + + #Loop through every element in a list and multiply them together + for num in nums: + product *= num + + #Return the product of all elements + return product diff --git a/Algorithms.py b/NumberAlgorithms.py similarity index 75% rename from Algorithms.py rename to NumberAlgorithms.py index 90f6431..73f3480 100644 --- a/Algorithms.py +++ b/NumberAlgorithms.py @@ -1,10 +1,10 @@ -#Python/pyClasses/Algorithms.py +#Python/pyClasses/NumberAlgorithms.py #Matthew Ellison -# Created: 01-27-19 -#Modified: 03-11-21 -#This is a file that contains a few algorithms that I have used several times +# Created: 07-21-21 +#Modified: 07-21-21 +#This file contains my library of number functions """ -Copyright (C) 2021 Matthew Ellison + Copyright (C) 2021 Matthew Ellison This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by @@ -21,8 +21,8 @@ Copyright (C) 2021 Matthew Ellison """ -import math import itertools +import math #Generate an infinite sequence of prime numbers using the Sieve of Eratosthenes @@ -185,53 +185,6 @@ def getAllFib(goalNumber: int) -> list: fibNums.pop() return fibNums -#This function returns the product of all elements in the list -def prod(nums: list) -> int: - #If a blank list was sent to the function return 0 as the product - if(len(nums) == 0): - return 0 - - #Setup the variables - product = 1 #Start at 1 because of working with multiplication - - #Loop through every element in a list and multiply them together - for num in nums: - product *= num - - #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: int = 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 - 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:] - - #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 - #This function returns the GCD of the two numbers sent to it def gcd(num1: int, num2: int) -> int: while((num1 != 0) and (num2 != 0)): @@ -248,14 +201,6 @@ def factorial(num: int) -> int: fact *= cnt return fact -#Returns true if the string passed in is a palindrome -def isPalindrome(str: str) -> bool: - rev = str[::-1] - if(str == rev): - return True - else: - return False - #Converts a number to its binary equivalent def toBin(num: int) -> str: #Convert the number to a binary string diff --git a/StringAlgorithms.py b/StringAlgorithms.py new file mode 100644 index 0000000..6f20a6f --- /dev/null +++ b/StringAlgorithms.py @@ -0,0 +1,61 @@ +#Python/pyClasses/StringAlgorithms.py +#Matthew Ellison +# Created: 07-21-21 +#Modified: 07-21-21 +#This file contains my library of string functions +""" + Copyright (C) 2021 Matthew Ellison + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . +""" + + +#This is a function that creates all permutations of a string and returns a vector of those permutations. +def getPermutations(master: str, num: int = 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 + 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:] + + #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 + +#Returns true if the string passed in is a palindrome +def isPalindrome(str: str) -> bool: + rev = str[::-1] + if(str == rev): + return True + else: + return False diff --git a/test/TestAlgorithms.py b/test/TestAlgorithms.py new file mode 100644 index 0000000..b4e238e --- /dev/null +++ b/test/TestAlgorithms.py @@ -0,0 +1,45 @@ +#Python/pyClasses/TestAlgorithms.py +#Matthew Ellison +# Created: 07-21-21 +#Modified: 07-23-21 +#This script runs the tests for all of my algorithm libraries +""" + Copyright (C) 2021 Matthew Ellison + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . +""" + + +import unittest + +import TestArrayAlgorithms +import TestNumberAlgorithms +import TestStringAlgorithms + + +#Run the unit test if the script is called +if __name__ == "__main__": + suites2 = [unittest.defaultTestLoader.loadTestsFromModule(TestArrayAlgorithms), unittest.defaultTestLoader.loadTestsFromModule(TestNumberAlgorithms), + unittest.defaultTestLoader.loadTestsFromModule(TestStringAlgorithms)] + test_suite = unittest.TestSuite(suites2) + test_runner = unittest.TextTestRunner().run(test_suite) + + +"""Results: +............. +---------------------------------------------------------------------- +Ran 13 tests in 0.001s + +OK +""" diff --git a/test/TestArrayAlgorithms.py b/test/TestArrayAlgorithms.py new file mode 100644 index 0000000..c00bdf8 --- /dev/null +++ b/test/TestArrayAlgorithms.py @@ -0,0 +1,50 @@ +#Python/pyClasses/TestArrayAlgorithms.py +#Matthew Ellison +# Created: 07-21-21 +#Modified: 07-21-21 +#Tests for my library of array algorithms +""" + Copyright (C) 2021 Matthew Ellison + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . +""" + + +import unittest +import ArrayAlgorithms + + +class TestArrayAlgorithms(unittest.TestCase): + #This function tests the prod function + def testProd(self): + #Test 1 + correctAnswer = 0 + numbers = [] + answer = ArrayAlgorithms.prod(numbers) + self.assertEqual(correctAnswer, answer, "prod failed the first test") + + #Test 2 + correctAnswer = 57600 + numbers = [2, 2, 3, 3, 4, 4, 100] + answer = ArrayAlgorithms.prod(numbers) + self.assertEqual(correctAnswer, answer, "prod failed the second test") + + +#Run the unit test if the script is called +if __name__ == "__main__": + unittest.main() + + +"""Results: +""" diff --git a/TestAlgorithms.py b/test/TestNumberAlgorithms.py similarity index 68% rename from TestAlgorithms.py rename to test/TestNumberAlgorithms.py index 6ce7ad0..6ebd4d6 100644 --- a/TestAlgorithms.py +++ b/test/TestNumberAlgorithms.py @@ -1,10 +1,10 @@ -#Python/pyClasses/TestAlgorithms.py +#Python/pyClasses/TestNumberAlgorithms.py #Matthew Ellison -# Created: 01-27-19 -#Modified: 06-01-21 -#This is a file that contains a few algorithms that I have used several times +# Created: 07-21-21 +#Modified: 07-21-21 +#Tests for my library of number algorithms """ -Copyright (C) 2021 Matthew Ellison + Copyright (C) 2021 Matthew Ellison This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by @@ -22,16 +22,16 @@ Copyright (C) 2021 Matthew Ellison import unittest -import Algorithms +import NumberAlgorithms -class TestAlgorithms(unittest.TestCase): +class TestNumberAlgorithms(unittest.TestCase): #This function tests the getPrimes function def testGetPrimes(self): #Test 1 correctAnswer = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97] topNum = 100 - answer = Algorithms.getPrimes(topNum) #Get the answer from the function + answer = NumberAlgorithms.getPrimes(topNum) #Get the answer from the function self.assertEqual(correctAnswer, answer, "getPrimes failed the first test") #This function tests the getNumPrimes function @@ -39,7 +39,7 @@ class TestAlgorithms(unittest.TestCase): #Test 1 correctAnswer = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97] numPrimes = 25 - answer = Algorithms.getNumPrimes(numPrimes) #Get the answer from the function + answer = NumberAlgorithms.getNumPrimes(numPrimes) #Get the answer from the function self.assertEqual(correctAnswer, answer, "getNumPrimes failed the first test") #This function tests the isPrime function @@ -47,22 +47,22 @@ class TestAlgorithms(unittest.TestCase): #Test 1 num = 2 correctAnswer = True - answer = Algorithms.isPrime(num) + answer = NumberAlgorithms.isPrime(num) self.assertEqual(correctAnswer, answer, "isPrime failed the first test") #Test 2 num = 97 correctAnswer = True - answer = Algorithms.isPrime(num) + answer = NumberAlgorithms.isPrime(num) self.assertEqual(correctAnswer, answer, "isPrime failed the second test") #Test 3 num = 1000 correctAnswer = False - answer = Algorithms.isPrime(num) + answer = NumberAlgorithms.isPrime(num) self.assertEqual(correctAnswer, answer, "isPrime failed the third test") #Test 4 num = 1 correctAnswer = False - answer = Algorithms.isPrime(num) + answer = NumberAlgorithms.isPrime(num) self.assertEqual(correctAnswer, answer, "isPrime failed the fourth test") #This function tests the getFactors function @@ -70,13 +70,13 @@ class TestAlgorithms(unittest.TestCase): #Test 1 correctAnswer = [2, 2, 5, 5] number = 100 - answer = Algorithms.getFactors(number) + answer = NumberAlgorithms.getFactors(number) self.assertEqual(correctAnswer, answer, "getFactors failed the first test") #Test 2 correctAnswer = [2, 7, 7] number = 98 - answer = Algorithms.getFactors(number) + answer = NumberAlgorithms.getFactors(number) self.assertEqual(correctAnswer, answer, "getFactors failed the second test") #This function tests the getDivisors function @@ -84,7 +84,7 @@ class TestAlgorithms(unittest.TestCase): #Test 1 correctAnswer = [1, 2, 4, 5, 10, 20, 25, 50, 100] topNum = 100 - answer = Algorithms.getDivisors(topNum) + answer = NumberAlgorithms.getDivisors(topNum) self.assertEqual(correctAnswer, answer, "getDivisors failed the first test") #This function tests the getFib function @@ -92,19 +92,19 @@ class TestAlgorithms(unittest.TestCase): #Test 1 correctAnswer = 144 number = 12 - answer = Algorithms.getFib(number) + answer = NumberAlgorithms.getFib(number) self.assertEqual(correctAnswer, answer, "getFib failed the first test") #Test 2 correctAnswer = 6765 number = 20 - answer = Algorithms.getFib(number) + answer = NumberAlgorithms.getFib(number) self.assertEqual(correctAnswer, answer, "getFib failed the second test") #Test 3 correctAnswer = 1070066266382758936764980584457396885083683896632151665013235203375314520604694040621889147582489792657804694888177591957484336466672569959512996030461262748092482186144069433051234774442750273781753087579391666192149259186759553966422837148943113074699503439547001985432609723067290192870526447243726117715821825548491120525013201478612965931381792235559657452039506137551467837543229119602129934048260706175397706847068202895486902666185435124521900369480641357447470911707619766945691070098024393439617474103736912503231365532164773697023167755051595173518460579954919410967778373229665796581646513903488154256310184224190259846088000110186255550245493937113651657039447629584714548523425950428582425306083544435428212611008992863795048006894330309773217834864543113205765659868456288616808718693835297350643986297640660000723562917905207051164077614812491885830945940566688339109350944456576357666151619317753792891661581327159616877487983821820492520348473874384736771934512787029218636250627816 number = 4782 - answer = Algorithms.getFib(number) + answer = NumberAlgorithms.getFib(number) self.assertEqual(correctAnswer, answer, "getFib failed the third test") #This function tests the getAllFib function @@ -112,56 +112,34 @@ class TestAlgorithms(unittest.TestCase): #Test 1 correctAnswer = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] highestNumber = 100 - answer = Algorithms.getAllFib(highestNumber) + answer = NumberAlgorithms.getAllFib(highestNumber) self.assertEqual(correctAnswer, answer, "getAllFib failed the first test") #Test 2 correctAnswer = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987] highestNumber = 1000 - answer = Algorithms.getAllFib(highestNumber) + answer = NumberAlgorithms.getAllFib(highestNumber) self.assertEqual(correctAnswer, answer, "getAllFib failed the second test") - #This function tests the prod function - def testProd(self): - #Test 1 - correctAnswer = 0 - numbers = [] - answer = Algorithms.prod(numbers) - self.assertEqual(correctAnswer, answer, "prod failed the first test") - - #Test 2 - correctAnswer = 57600 - numbers = [2, 2, 3, 3, 4, 4, 100] - answer = Algorithms.prod(numbers) - self.assertEqual(correctAnswer, answer, "prod failed the second test") - - #This function tests the getPermutations function - def testGetPermutations(self): - #Test 1 - permString = "012" - correctAnswer = ["012", "021", "102", "120", "201", "210"] - answer = Algorithms.getPermutations(permString) - self.assertEqual(correctAnswer, answer, "getPermutations failed the first test") - #This function tests the gcd function def testGCD(self): #Test 1 num1 = 2 num2 = 3 correctAnswer = 1 - answer = Algorithms.gcd(num1, num2) + answer = NumberAlgorithms.gcd(num1, num2) self.assertEqual(correctAnswer, answer, "getGCD failed the first test") #Test 2 num1 = 1000 num2 = 575 correctAnswer = 25 - answer = Algorithms.gcd(num1, num2) + answer = NumberAlgorithms.gcd(num1, num2) self.assertEqual(correctAnswer, answer, "getGCD failed the second test") #Test 3 num1 = 1000 num2 = 1000 correctAnswer = 1000 - answer = Algorithms.gcd(num1, num2) + answer = NumberAlgorithms.gcd(num1, num2) self.assertEqual(correctAnswer, answer, "getGCD failed the third test") #This functions tests the factorial function @@ -169,66 +147,43 @@ class TestAlgorithms(unittest.TestCase): #Test 1 num = 1 correctAnswer = 1 - answer = Algorithms.factorial(num) + answer = NumberAlgorithms.factorial(num) self.assertEqual(correctAnswer, answer, "getFactorial failed the first test") #Test 2 num = 10 correctAnswer = 3628800 - answer = Algorithms.factorial(num) + answer = NumberAlgorithms.factorial(num) self.assertEqual(correctAnswer, answer, "getFactorial failed the second test") #Test 3 num = -5 correctAnswer = 1 - answer = Algorithms.factorial(num) + answer = NumberAlgorithms.factorial(num) self.assertEqual(correctAnswer, answer, "getFactorial failed the third test") - #This function tests the isPalindrome function - def testIsPalindrome(self): - #Test 1 - str = "101" - correctAnswer = True - answer = Algorithms.isPalindrome(str) - self.assertEqual(correctAnswer, answer, "isPalindrome failed the first test") - #Test 2 - str = "100" - correctAnswer = False - answer = Algorithms.isPalindrome(str) - self.assertEqual(correctAnswer, answer, "isPalindrome failed the second test") - #Test 3 - str = "" - correctAnswer = True - answer = Algorithms.isPalindrome(str) - self.assertEqual(correctAnswer, answer, "isPalindrome failed the third test") - #This function tests the toBin function def testToBin(self): #Test 1 num = 7 correctAnswer = "111" - answer = Algorithms.toBin(num) + answer = NumberAlgorithms.toBin(num) self.assertEqual(correctAnswer, answer, "toBin failed the first test") #Test 2 num = 0 correctAnswer = "0" - answer = Algorithms.toBin(num) + answer = NumberAlgorithms.toBin(num) self.assertEqual(correctAnswer, answer, "toBin failed the second test") #Test 3 num = 1000000 correctAnswer = "11110100001001000000" - answer = Algorithms.toBin(num) + answer = NumberAlgorithms.toBin(num) self.assertEqual(correctAnswer, answer, "toBin failed the third test") - #Run the unit test if the script is called if __name__ == "__main__": unittest.main() -"""Results: -............. ----------------------------------------------------------------------- -Ran 13 tests in 0.002s -OK +"""Results: """ diff --git a/test/TestStringAlgorithms.py b/test/TestStringAlgorithms.py new file mode 100644 index 0000000..5a3550a --- /dev/null +++ b/test/TestStringAlgorithms.py @@ -0,0 +1,62 @@ +#Python/pyClasses/TestStringAlgorithms.py +#Matthew Ellison +# Created: 07-21-21 +#Modified: 07-21-21 +#Tests for my library of number algorithms +""" + Copyright (C) 2021 Matthew Ellison + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . +""" + + +import unittest +import StringAlgorithms + + +class TestStringAlgorithms(unittest.TestCase): + #This function tests the getPermutations function + def testGetPermutations(self): + #Test 1 + permString = "012" + correctAnswer = ["012", "021", "102", "120", "201", "210"] + answer = StringAlgorithms.getPermutations(permString) + self.assertEqual(correctAnswer, answer, "getPermutations failed the first test") + + #This function tests the isPalindrome function + def testIsPalindrome(self): + #Test 1 + str = "101" + correctAnswer = True + answer = StringAlgorithms.isPalindrome(str) + self.assertEqual(correctAnswer, answer, "isPalindrome failed the first test") + #Test 2 + str = "100" + correctAnswer = False + answer = StringAlgorithms.isPalindrome(str) + self.assertEqual(correctAnswer, answer, "isPalindrome failed the second test") + #Test 3 + str = "" + correctAnswer = True + answer = StringAlgorithms.isPalindrome(str) + self.assertEqual(correctAnswer, answer, "isPalindrome failed the third test") + + +#Run the unit test if the script is called +if __name__ == "__main__": + unittest.main() + + +"""Results: +""" diff --git a/testStopwatch.py b/test/testStopwatch.py similarity index 100% rename from testStopwatch.py rename to test/testStopwatch.py diff --git a/todo.txt b/todo.txt index c747a54..e69de29 100644 --- a/todo.txt +++ b/todo.txt @@ -1 +0,0 @@ -Split the Algorithms library into 3 different libraries \ No newline at end of file