From 8dbb52db85fc8f87d22dff224cf0a2f5fbc27368 Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Mon, 11 Oct 2021 12:57:43 -0400 Subject: [PATCH] Added isPandigital function --- StringAlgorithms.py | 23 ++++++++++++++++++++- test/TestStringAlgorithms.py | 39 +++++++++++++++++++++++++++++++++++- 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/StringAlgorithms.py b/StringAlgorithms.py index 6f20a6f..58388e5 100644 --- a/StringAlgorithms.py +++ b/StringAlgorithms.py @@ -1,7 +1,7 @@ #Python/pyClasses/StringAlgorithms.py #Matthew Ellison # Created: 07-21-21 -#Modified: 07-21-21 +#Modified: 10-11-21 #This file contains my library of string functions """ Copyright (C) 2021 Matthew Ellison @@ -59,3 +59,24 @@ def isPalindrome(str: str) -> bool: return True else: return False + +#Returns true if the string passed to it is a pandigital +def isPandigitalFull(numStr: str, bottom: int, top: int) -> bool: + #Return false if top < bottom + if(top < bottom): + return False + + #Return false if the wrong number of characters are in the string + if(len(numStr) != (top - bottom + 1)): + return False + + #Make sure that all of the needed characters are in the string exactly one time + for cnt in range(bottom, top): + #Make sure there is exactly one of this number contained in the string + if(numStr.count(str(cnt)) != 1): + return False + + #If the function has reached this part it has passed all of the falsifying tests + return True +def isPandigital(str: str) -> bool: + return isPandigitalFull(str, 1, 9) \ No newline at end of file diff --git a/test/TestStringAlgorithms.py b/test/TestStringAlgorithms.py index 5a3550a..94c8339 100644 --- a/test/TestStringAlgorithms.py +++ b/test/TestStringAlgorithms.py @@ -1,7 +1,7 @@ #Python/pyClasses/TestStringAlgorithms.py #Matthew Ellison # Created: 07-21-21 -#Modified: 07-21-21 +#Modified: 10-11-21 #Tests for my library of number algorithms """ Copyright (C) 2021 Matthew Ellison @@ -52,6 +52,43 @@ class TestStringAlgorithms(unittest.TestCase): answer = StringAlgorithms.isPalindrome(str) self.assertEqual(correctAnswer, answer, "isPalindrome failed the third test") + def testIsPandigital(self): + #Test 1 + str = "123456789" + correctAnswer = True + answer = StringAlgorithms.isPandigital(str) + self.assertEqual(correctAnswer, answer, "isPandigital failed the first test") + + #Test 2 + str = "123" + correctAnswer = True + answer = StringAlgorithms.isPandigitalFull(str, 1, 3) + self.assertEqual(correctAnswer, answer, "isPandigital failed the second test") + + #Test 3 + str = "123" + correctAnswer = False + answer = StringAlgorithms.isPandigital(str) + self.assertEqual(correctAnswer, answer, "isPandigital failed the third test") + + #Test 4 + str = "123" + correctAnswer = False + answer = StringAlgorithms.isPandigitalFull(str, 3, 1) + self.assertEqual(correctAnswer, answer, "isPandigital failed the fourth test") + + #Test 5 + str = "1" + correctAnswer = True + answer = StringAlgorithms.isPandigitalFull(str, 1, 1) + self.assertEqual(correctAnswer, answer, "isPandigital failed the fifth test") + + #Test 6 + str = "112" + correctAnswer = False + answer = StringAlgorithms.isPandigitalFull(str, 1, 3) + self.assertEqual(correctAnswer, answer, "isPandigital failed the sixth test") + #Run the unit test if the script is called if __name__ == "__main__":