diff --git a/StringAlgorithms.ts b/StringAlgorithms.ts index 7654edf..5590ba9 100644 --- a/StringAlgorithms.ts +++ b/StringAlgorithms.ts @@ -1,7 +1,7 @@ //typescriptClasses/StringAlgorithms.ts //Matthew Ellison // Created: 07-13-21 -//Modified: 07-13-21 +//Modified: 10-11-21 //Algorithms for strings /* Copyright (C) 2021 Matthew Ellison @@ -75,7 +75,7 @@ export function isPalindrome(str: string): boolean{ } //This function returns the number of times the character occurs in the string -export function findNumOccurrence(str: string, ch: string){ +export function findNumOccurrence(str: string, ch: string): number{ let num: number = 0; //Set the number of occurrences to 0 to start //Loop through every character in the string and compare it to the character passed in for(let strCh of str){ @@ -86,4 +86,31 @@ export function findNumOccurrence(str: string, ch: string){ } //Return the number of times the character appeared in the string return num; +} + +//Returns true if the string passed to it is a pandigital +export function isPandigitalFull(str: string, bottom: number, top: number): boolean{ + //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(let cnt = bottom;cnt <= top;++cnt){ + //Make sure there is exactly one of this number contained in the string + if(findNumOccurrence(str, cnt.toString()) != 1){ + return false; + } + } + + //If the function has reached this part is has passed all of the falsifying tests + return true; +} +export function isPandigital(str: string): boolean{ + return isPandigitalFull(str, 1, 9); } \ No newline at end of file diff --git a/TestAlgorithms.ts b/TestAlgorithms.ts index 59a0a48..ae39b7b 100644 --- a/TestAlgorithms.ts +++ b/TestAlgorithms.ts @@ -1,7 +1,7 @@ //typescriptClasses/TestAlgorithms.ts //Matthew Ellison // Created: 10-19-20 -//Modified: 07-13-21 +//Modified: 10-11-21 //This class holds many algorithms that I have found it useful to keep around /* Copyright (C) 2021 Matthew Ellison @@ -23,7 +23,7 @@ Copyright (C) 2021 Matthew Ellison import { testArrayEquals, testGetSum, testGetProd } from "./TestArrayAlgorithms"; import { testFactorial, testGCD, testGetAllFib, testGetDivisors, testGetFactors, testGetFib, testGetNumPrimes, testGetPrimes, testIsPrime, testSieveOfEratosthenes, testSqrtBig, testToBin } from "./TestNumberAlgorithms"; -import { testFindNumOccurrences, testGetPermutations, testIsPalindrome } from "./TestStringAlgorithms"; +import { testFindNumOccurrences, testGetPermutations, testIsPalindrome, testIsPandigital } from "./TestStringAlgorithms"; //Run the array tests @@ -49,5 +49,6 @@ testToBin(); testGetPermutations(); testIsPalindrome(); testFindNumOccurrences(); +testIsPandigital(); console.log("All tests passed"); diff --git a/TestStringAlgorithms.ts b/TestStringAlgorithms.ts index f789323..90d8238 100644 --- a/TestStringAlgorithms.ts +++ b/TestStringAlgorithms.ts @@ -1,7 +1,7 @@ //typescriptClasses/TestStringAlgorithms.ts //Matthew Ellison // Created: 07-13-21 -//Modified: 07-13-21 +//Modified: 10-11-21 //Tests for the string algorithms /* Copyright (C) 2021 Matthew Ellison @@ -23,7 +23,7 @@ Copyright (C) 2021 Matthew Ellison import assert = require("assert"); import { arrayEquals } from "./ArrayAlgorithms"; -import { getPermutations, isPalindrome } from "./StringAlgorithms"; +import { getPermutations, isPalindrome, isPandigital, isPandigitalFull } from "./StringAlgorithms"; export function testGetPermutations(){ @@ -75,3 +75,43 @@ export function testFindNumOccurrences(){ console.log("findNumOccurrences passed"); } + +export function testIsPandigital(){ + //Test 1 + let str: string = "123456789"; + let correctAnswer: boolean = true; + let answer = isPandigital(str); + assert.ok((answer == correctAnswer), "isPandigital 1 failed"); + + //Test 2 + str = "123"; + correctAnswer = true; + answer = isPandigitalFull(str, 1, 3); + assert.ok((answer == correctAnswer), "isPandigital 2 failed"); + + //Test 3 + str = "123"; + correctAnswer = false; + answer = isPandigital(str); + assert.ok((answer == correctAnswer), "isPandigital 3 failed"); + + //Test 4 + str = "123"; + correctAnswer = false; + answer = isPandigitalFull(str, 3, 1); + assert.ok((answer == correctAnswer), "isPandigital 4 failed"); + + //Test 5 + str = "1"; + correctAnswer = true; + answer = isPandigitalFull(str, 1, 1); + assert.ok((answer == correctAnswer), "isPandigital 5 failed"); + + //Test 6 + str = "112"; + correctAnswer = false; + answer = isPandigitalFull(str, 1, 3); + assert.ok((answer == correctAnswer), "isPandigital 6 failed"); + + console.log("isPandigital passed"); +}