From 28e28c2ee477e0b88caa3a8e156b59b4fa693b1e Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Tue, 29 Jun 2021 15:59:22 -0400 Subject: [PATCH] Added functions to check for palindromes and convert nums to bin strings --- Algorithms.ts | 35 +++++++++++++++++++++++++++ TestAlgorithms.ts | 60 ++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 94 insertions(+), 1 deletion(-) diff --git a/Algorithms.ts b/Algorithms.ts index 7c47211..8353b07 100644 --- a/Algorithms.ts +++ b/Algorithms.ts @@ -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; +} diff --git a/TestAlgorithms.ts b/TestAlgorithms.ts index d8970eb..f5d093d 100644 --- a/TestAlgorithms.ts +++ b/TestAlgorithms.ts @@ -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();