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);
|
||||
}
|
||||
@@ -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");
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user