diff --git a/CSClasses/ArrayAlgorithms.cs b/CSClasses/ArrayAlgorithms.cs index 2e4337a..4432224 100644 --- a/CSClasses/ArrayAlgorithms.cs +++ b/CSClasses/ArrayAlgorithms.cs @@ -78,5 +78,29 @@ namespace mee{ } return prod; } + //Print a list + public static string PrintList(List list){ + string listString = "["; + for(int cnt = 0;cnt < list.Count;++cnt){ + listString += list[cnt]; + if(cnt < list.Count - 1){ + listString += ", "; + } + } + listString += "]"; + return listString; + } + //Print an array + public static string PrintArray(T[] array){ + string arrayString = "["; + for(int cnt = 0;cnt < array.Length;++cnt){ + arrayString += array[cnt]; + if(cnt < array.Length - 1){ + arrayString += ", "; + } + } + arrayString += "]"; + return arrayString; + } } } \ No newline at end of file diff --git a/CSClasses/StringAlgorithms.cs b/CSClasses/StringAlgorithms.cs index acaf256..5e31c2e 100644 --- a/CSClasses/StringAlgorithms.cs +++ b/CSClasses/StringAlgorithms.cs @@ -1,7 +1,7 @@ //C#/CSClasses/StringAlgorithms.cs //Matthew Ellison // Created: 07-04-21 -//Modified: 07-04-21 +//Modified: 10-11-21 //This class contains functions that perform algorithms on numbers /* Copyright (C) 2021 Matthew Ellison @@ -85,29 +85,31 @@ namespace mee{ return false; } } - //Print a list - public static string PrintList(List list){ - string listString = "["; - for(int cnt = 0;cnt < list.Count;++cnt){ - listString += list[cnt]; - if(cnt < list.Count - 1){ - listString += ", "; + //This function returns true if the string passed to it is a pandigital + public static bool IsPandigital(string str, char bottom, char top){ + //Return false if top < bottom + if(top < bottom){ + return false; + } + + //Return false if the wrong number of characters are in the string + if(str.Length != (top - bottom + 1)){ + return false; + } + + //Make sure that all of the needed characters are in the string exactly one time + for(char cnt = bottom;cnt <= top;++cnt){ + //If a single character is found more than once then the string is not pandigital + if(FindNumOccurrence(str, cnt) != 1){ + return false; } } - listString += "]"; - return listString; + + //If the function has reached this part it has passed all of the falsifying tests + return true; } - //Print an array - public static string PrintArray(T[] array){ - string arrayString = "["; - for(int cnt = 0;cnt < array.Length;++cnt){ - arrayString += array[cnt]; - if(cnt < array.Length - 1){ - arrayString += ", "; - } - } - arrayString += "]"; - return arrayString; + public static bool IsPandigital(string str){ + return IsPandigital(str, '1', '9'); } } } \ No newline at end of file diff --git a/TestCSClasses/TestArrayAlgorithms.cs b/TestCSClasses/TestArrayAlgorithms.cs index 734e7b3..159d950 100644 --- a/TestCSClasses/TestArrayAlgorithms.cs +++ b/TestCSClasses/TestArrayAlgorithms.cs @@ -79,5 +79,71 @@ namespace TestCSClasses{ BigInteger bigAnswer = mee.ArrayAlgorithms.GetProd(bigNumbers); Assert.AreEqual(bigCorrectAnswer, bigAnswer, "GetProd BigInteger failed"); } + [TestMethod] + public void TestPrintList(){ + //Test 1 + List nums = new List(); + string correctAnswer = "[]"; + string answer = mee.ArrayAlgorithms.PrintList(nums); + Assert.AreEqual(correctAnswer, answer, "PrintList 1 failed"); + //Test 2 + nums = new List(){1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + correctAnswer = "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"; + answer = mee.ArrayAlgorithms.PrintList(nums); + Assert.AreEqual(correctAnswer, answer, "PrintList 2 failed"); + //Test 3 + nums = new List(){-3, -2, -1, 0, 1, 2, 3}; + correctAnswer = "[-3, -2, -1, 0, 1, 2, 3]"; + answer = mee.ArrayAlgorithms.PrintList(nums); + Assert.AreEqual(correctAnswer, answer, "PrintList 3 failed"); + + //Test 4 + List strings = new List(){"A", "B", "C"}; + correctAnswer = "[A, B, C]"; + answer = mee.ArrayAlgorithms.PrintList(strings); + Assert.AreEqual(correctAnswer, answer, "PrintList 1 failed"); + //Test 5 + strings = new List(){"abc", "def", "ghi"}; + correctAnswer = "[abc, def, ghi]"; + answer = mee.ArrayAlgorithms.PrintList(strings); + Assert.AreEqual(correctAnswer, answer, "PrintList 2 failed"); + } + [TestMethod] + public void TestPrintArray(){ + //Test 1 + int[] nums = new int[]{}; + string correctAnswer = "[]"; + string answer = mee.ArrayAlgorithms.PrintArray(nums); + Assert.AreEqual(correctAnswer, answer, "PrintArray 1 failed"); + //Test 2 + nums = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + correctAnswer = "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"; + answer = mee.ArrayAlgorithms.PrintArray(nums); + Assert.AreEqual(correctAnswer, answer, "PrintArray 2 failed"); + //Test 3 + nums = new int[]{-3, -2, -1, 0, 1, 2, 3}; + correctAnswer = "[-3, -2, -1, 0, 1, 2, 3]"; + answer = mee.ArrayAlgorithms.PrintArray(nums); + Assert.AreEqual(correctAnswer, answer, "PrintArray 3 failed"); + + //Test 4 + string[] strings = new string[]{"A", "B", "C"}; + correctAnswer = "[A, B, C]"; + answer = mee.ArrayAlgorithms.PrintArray(strings); + Assert.AreEqual(correctAnswer, answer, "PrintArray 1 failed"); + //Test 5 + strings = new string[]{"abc", "def", "ghi"}; + correctAnswer = "[abc, def, ghi]"; + answer = mee.ArrayAlgorithms.PrintArray(strings); + Assert.AreEqual(correctAnswer, answer, "PrintArray 2 failed"); + } + [TestMethod] + public void TestIsPandigital(){ + //Test 1 + + //Test 2 + + //Test 3 + } } } \ No newline at end of file diff --git a/TestCSClasses/TestStringAlgorithms.cs b/TestCSClasses/TestStringAlgorithms.cs index f0b342b..5524503 100644 --- a/TestCSClasses/TestStringAlgorithms.cs +++ b/TestCSClasses/TestStringAlgorithms.cs @@ -1,7 +1,7 @@ //C#/CSClasses/TestStringAlgorithms.cs //Matthew Ellison // Created: 07-04-21 -//Modified: 07-04-21 +//Modified: 10-11-21 //This class contains tests for my string algorithms library /* Copyright (C) 2021 Matthew Ellison @@ -76,62 +76,42 @@ namespace TestStringAlgorithms{ Assert.AreEqual(correctAnswer, answer, "IsPalindrome 3 failed"); } [TestMethod] - public void TestPrintList(){ + public void TestIsPandigital(){ //Test 1 - List nums = new List(); - string correctAnswer = "[]"; - string answer = mee.StringAlgorithms.PrintList(nums); - Assert.AreEqual(correctAnswer, answer, "PrintList 1 failed"); + string num = "123456789"; + bool correctAnswer = true; + bool answer = mee.StringAlgorithms.IsPandigital(num); + Assert.AreEqual(correctAnswer, answer, "IsPandigital 1 failed"); + //Test 2 - nums = new List(){1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; - correctAnswer = "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"; - answer = mee.StringAlgorithms.PrintList(nums); - Assert.AreEqual(correctAnswer, answer, "PrintList 2 failed"); + num = "123"; + correctAnswer = true; + answer = mee.StringAlgorithms.IsPandigital(num, '1', '3'); + Assert.AreEqual(correctAnswer, answer, "IsPandigital 2 failed"); + //Test 3 - nums = new List(){-3, -2, -1, 0, 1, 2, 3}; - correctAnswer = "[-3, -2, -1, 0, 1, 2, 3]"; - answer = mee.StringAlgorithms.PrintList(nums); - Assert.AreEqual(correctAnswer, answer, "PrintList 3 failed"); + num = "123"; + correctAnswer = false; + answer = mee.StringAlgorithms.IsPandigital(num); + Assert.AreEqual(correctAnswer, answer, "IsPandigital 3 failed"); //Test 4 - List strings = new List(){"A", "B", "C"}; - correctAnswer = "[A, B, C]"; - answer = mee.StringAlgorithms.PrintList(strings); - Assert.AreEqual(correctAnswer, answer, "PrintList 1 failed"); - //Test 5 - strings = new List(){"abc", "def", "ghi"}; - correctAnswer = "[abc, def, ghi]"; - answer = mee.StringAlgorithms.PrintList(strings); - Assert.AreEqual(correctAnswer, answer, "PrintList 2 failed"); - } - [TestMethod] - public void TestPrintArray(){ - //Test 1 - int[] nums = new int[]{}; - string correctAnswer = "[]"; - string answer = mee.StringAlgorithms.PrintArray(nums); - Assert.AreEqual(correctAnswer, answer, "PrintArray 1 failed"); - //Test 2 - nums = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; - correctAnswer = "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"; - answer = mee.StringAlgorithms.PrintArray(nums); - Assert.AreEqual(correctAnswer, answer, "PrintArray 2 failed"); - //Test 3 - nums = new int[]{-3, -2, -1, 0, 1, 2, 3}; - correctAnswer = "[-3, -2, -1, 0, 1, 2, 3]"; - answer = mee.StringAlgorithms.PrintArray(nums); - Assert.AreEqual(correctAnswer, answer, "PrintArray 3 failed"); + num = "123"; + correctAnswer = false; + answer = mee.StringAlgorithms.IsPandigital(num, '3', '1'); + Assert.AreEqual(correctAnswer, answer, "IsPandigital 4 failed"); - //Test 4 - string[] strings = new string[]{"A", "B", "C"}; - correctAnswer = "[A, B, C]"; - answer = mee.StringAlgorithms.PrintArray(strings); - Assert.AreEqual(correctAnswer, answer, "PrintArray 1 failed"); //Test 5 - strings = new string[]{"abc", "def", "ghi"}; - correctAnswer = "[abc, def, ghi]"; - answer = mee.StringAlgorithms.PrintArray(strings); - Assert.AreEqual(correctAnswer, answer, "PrintArray 2 failed"); + num = "1"; + correctAnswer = true; + answer = mee.StringAlgorithms.IsPandigital(num, '1', '1'); + Assert.AreEqual(correctAnswer, answer, "IsPandigitial 5 failed"); + + //Test 6 + num = "112"; + correctAnswer = false; + answer = mee.StringAlgorithms.IsPandigital(num, '1', '3'); + Assert.AreEqual(correctAnswer, answer, "IsPandigitial 6 failed"); } } } \ No newline at end of file