mirror of
https://bitbucket.org/Mattrixwv/typescriptclasses.git
synced 2025-12-06 18:33:59 -05:00
Added isPandigital function
This commit is contained in:
@@ -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);
|
||||
}
|
||||
Reference in New Issue
Block a user