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

@@ -21,6 +21,7 @@ Copyright (C) 2021 Matthew Ellison
*/
import { SSL_OP_SSLEAY_080_CLIENT_DH_BUG } from "constants";
import { InvalidResult } from "./InvalidResult";
@@ -658,3 +659,37 @@ export function factorialBig(num: bigint): bigint{
}
return fact;
}
//Returns true if the string passed in is a palindrome
export function isPalindrome(str: string): boolean{
let rev = str.split("").reverse().join("");
if(str == rev){
return true;
}
else{
return false;
}
}
//Converts a number to its binary equivalent
export function toBin(num: number): string{
return (num >>> 0).toString(2);
}
export function toBinBig(num: bigint): string{
let binNum = "";
while(num > 0n){
let rest = num % 2n;
if(rest == 1n){
binNum += "1";
}
else{
binNum += "0";
}
num = (num - rest) / 2n;
}
binNum = binNum.split("").reverse().join("");
if(binNum == ""){
binNum = "0";
}
return binNum;
}

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();