Added functions to check for palindromes and convert nums to bin strings

This commit is contained in:
2021-06-29 15:59:22 -04:00
parent 7ef12495c4
commit 28e28c2ee4
2 changed files with 94 additions and 1 deletions

View File

@@ -23,7 +23,7 @@ Copyright (C) 2021 Matthew Ellison
import assert = require("assert");
import { arrayEquals, getAllFib, getAllFibBig, getFactors, getFactorsBig, getPrimes, getPrimesBig, getNumPrimes, getNumPrimesBig,
sqrtBig, getSum, getSumBig, getProd, getProdBig, getDivisors, getDivisorsBig, factorial, factorialBig } from "./Algorithms";
sqrtBig, getSum, getSumBig, getProd, getProdBig, getDivisors, getDivisorsBig, factorial, factorialBig, isPalindrome, toBin, toBinBig } from "./Algorithms";
function testArrayEquals(){
@@ -253,6 +253,62 @@ function testFactorial(){
console.log("factorial passed");
}
function testIsPalindrome(){
//Test 1
let str: string = "101";
let correctAnswer: boolean = true;
let answer: boolean = isPalindrome(str);
assert.ok((correctAnswer == answer), "isPalindrome number 1 failed");
//Test 2
str = "100";
correctAnswer = false;
answer = isPalindrome(str);
assert.ok((correctAnswer == answer), "isPalindrome number 2 failed");
//Test 3
str = "";
correctAnswer = true;
answer = isPalindrome(str);
assert.ok((correctAnswer == answer), "isPalindrome number 3 failed");
console.log("isPalindrome passed");
}
function testToBin(){
//Test 1
let num: number = 7;
let correctAnswer: string = "111";
let answer: string = toBin(num);
assert.ok((correctAnswer == answer), "toBin number 1 failed");
//Test 2
num = 0;
correctAnswer = "0";
answer = toBin(num);
assert.ok((correctAnswer == answer), "toBin number 2 failed");
//Test 3
num = 1000000;
correctAnswer = "11110100001001000000";
answer = toBin(num);
assert.ok((correctAnswer == answer), "toBin number 3 failed");
//Test 4
let numBig: bigint = 7n;
correctAnswer = "111";
answer = toBinBig(numBig);
assert.ok((correctAnswer == answer), "toBinBig number 1 failed");
//Test 5
numBig = 0n;
correctAnswer = "0";
answer = toBinBig(numBig);
assert.ok((correctAnswer == answer), "toBinBig number 2 failed");
//Test 6
numBig = 1000000n;
correctAnswer = "11110100001001000000";
answer = toBinBig(numBig);
assert.ok((correctAnswer == answer), "toBinBig number 3 failed");
console.log("toBin passed");
}
//Run all of the tests
testArrayEquals();
@@ -265,3 +321,5 @@ testGetDivisors();
testGetSum();
testGetProd();
testFactorial();
testIsPalindrome();
testToBin();