mirror of
https://bitbucket.org/Mattrixwv/csclasses.git
synced 2025-12-06 18:23:58 -05:00
Added functions to check for palindromes and convert nums to bin strings
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
//C#/CSClasses/Algorithms.cs
|
//C#/CSClasses/Algorithms.cs
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 08-23-20
|
// Created: 08-23-20
|
||||||
//Modified: 06-01-21
|
//Modified: 06-29-21
|
||||||
//This file contains a class that is used to time the execution time of other programs
|
//This file contains a class that is used to time the execution time of other programs
|
||||||
/*
|
/*
|
||||||
Copyright (C) 2021 Matthew Ellison
|
Copyright (C) 2021 Matthew Ellison
|
||||||
@@ -797,5 +797,44 @@ namespace mee{
|
|||||||
}
|
}
|
||||||
return fact;
|
return fact;
|
||||||
}
|
}
|
||||||
|
//Returns true if the string passed in is a palindrome
|
||||||
|
public static bool IsPalindrome(string str){
|
||||||
|
string rev = new string(str.ToCharArray().Reverse().ToArray());
|
||||||
|
if(str == rev){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//Converts a number to its binary equivalent
|
||||||
|
public static string ToBin(int num){
|
||||||
|
//Convert the number to a binary string
|
||||||
|
string binaryString = Convert.ToString(num, 2);
|
||||||
|
return binaryString;
|
||||||
|
}
|
||||||
|
public static string ToBin(long num){
|
||||||
|
//Convert the number to a binary string
|
||||||
|
string binaryString = Convert.ToString(num, 2);
|
||||||
|
return binaryString;
|
||||||
|
}
|
||||||
|
public static string ToBin(BigInteger num){
|
||||||
|
//Convert the number to a binary string
|
||||||
|
byte[] bytes = num.ToByteArray();
|
||||||
|
bool[] bits = new bool[8 * bytes.Length];
|
||||||
|
new System.Collections.BitArray(bytes).CopyTo(bits, 0);
|
||||||
|
bits = bits.Reverse().ToArray();
|
||||||
|
System.Text.StringBuilder tempString = new System.Text.StringBuilder();
|
||||||
|
foreach(bool bit in bits){
|
||||||
|
tempString.Append(bit ? '1' : '0');
|
||||||
|
}
|
||||||
|
//Remove any leading 0's
|
||||||
|
string binaryString = System.Text.RegularExpressions.Regex.Replace(tempString.ToString(), @"^0+", "");
|
||||||
|
//If nothing is left make sure there is a 0
|
||||||
|
if(binaryString == ""){
|
||||||
|
binaryString = "0";
|
||||||
|
}
|
||||||
|
return binaryString;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//C#/CSClasses/TestCSClasses/TestAlgorithms.cs
|
//C#/CSClasses/TestCSClasses/TestAlgorithms.cs
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 03-11-21
|
// Created: 03-11-21
|
||||||
//Modified: 06-01-21
|
//Modified: 06-29-21
|
||||||
//This file contains the tests for the Algorithms class
|
//This file contains the tests for the Algorithms class
|
||||||
/*
|
/*
|
||||||
Copyright (C) 2021 Matthew Ellison
|
Copyright (C) 2021 Matthew Ellison
|
||||||
@@ -330,5 +330,75 @@ namespace TestCSClasses{
|
|||||||
BigInteger answerBig = mee.Algorithms.Factorial(numBig);
|
BigInteger answerBig = mee.Algorithms.Factorial(numBig);
|
||||||
Assert.AreEqual(correctAnswerBig, answerBig, "Factorial 5 failed");
|
Assert.AreEqual(correctAnswerBig, answerBig, "Factorial 5 failed");
|
||||||
}
|
}
|
||||||
|
[TestMethod]
|
||||||
|
public void TestIsPalindrome(){
|
||||||
|
//Test 1
|
||||||
|
string str = "101";
|
||||||
|
bool correctAnswer = true;
|
||||||
|
bool answer = mee.Algorithms.IsPalindrome(str);
|
||||||
|
Assert.AreEqual(correctAnswer, answer, "IsPalindrome 1 failed");
|
||||||
|
|
||||||
|
//Test 2
|
||||||
|
str = "100";
|
||||||
|
correctAnswer = false;
|
||||||
|
answer = mee.Algorithms.IsPalindrome(str);
|
||||||
|
Assert.AreEqual(correctAnswer, answer, "IsPalindrome 2 failed");
|
||||||
|
|
||||||
|
//Test 3
|
||||||
|
str = "";
|
||||||
|
correctAnswer = true;
|
||||||
|
answer = mee.Algorithms.IsPalindrome(str);
|
||||||
|
Assert.AreEqual(correctAnswer, answer, "IsPalindrome 3 failed");
|
||||||
|
}
|
||||||
|
[TestMethod]
|
||||||
|
public void TestToBin(){
|
||||||
|
//Test 1
|
||||||
|
int num = 7;
|
||||||
|
string correctAnswer = "111";
|
||||||
|
string answer = mee.Algorithms.ToBin(num);
|
||||||
|
Assert.AreEqual(correctAnswer, answer, "ToBin 1 failed");
|
||||||
|
//Test 2
|
||||||
|
num = 0;
|
||||||
|
correctAnswer = "0";
|
||||||
|
answer = mee.Algorithms.ToBin(num);
|
||||||
|
Assert.AreEqual(correctAnswer, answer, "ToBin 2 failed");
|
||||||
|
//Test 3
|
||||||
|
num = 1000000;
|
||||||
|
correctAnswer = "11110100001001000000";
|
||||||
|
answer = mee.Algorithms.ToBin(num);
|
||||||
|
Assert.AreEqual(correctAnswer, answer, "ToBin 3 failed");
|
||||||
|
|
||||||
|
//Test 4
|
||||||
|
long longNum = 7;
|
||||||
|
correctAnswer = "111";
|
||||||
|
answer = mee.Algorithms.ToBin(longNum);
|
||||||
|
Assert.AreEqual(correctAnswer, answer, "ToBing long 1 failed");
|
||||||
|
//Test 5
|
||||||
|
longNum = 0;
|
||||||
|
correctAnswer = "0";
|
||||||
|
answer = mee.Algorithms.ToBin(longNum);
|
||||||
|
Assert.AreEqual(correctAnswer, answer, "ToBin long 2 failed");
|
||||||
|
//Test 6
|
||||||
|
longNum = 1000000;
|
||||||
|
correctAnswer = "11110100001001000000";
|
||||||
|
answer = mee.Algorithms.ToBin(longNum);
|
||||||
|
Assert.AreEqual(correctAnswer, answer, "ToBing long 3 failed");
|
||||||
|
|
||||||
|
//Test 7
|
||||||
|
BigInteger bigNum = 7;
|
||||||
|
correctAnswer = "111";
|
||||||
|
answer = mee.Algorithms.ToBin(bigNum);
|
||||||
|
Assert.AreEqual(correctAnswer, answer, "ToBin big 1 failed");
|
||||||
|
//Test 8
|
||||||
|
bigNum = 0;
|
||||||
|
correctAnswer = "0";
|
||||||
|
answer = mee.Algorithms.ToBin(bigNum);
|
||||||
|
Assert.AreEqual(correctAnswer, answer, "ToBin big 2 failed");
|
||||||
|
//Test 9
|
||||||
|
bigNum = 1000000;
|
||||||
|
correctAnswer = "11110100001001000000";
|
||||||
|
answer = mee.Algorithms.ToBin(bigNum);
|
||||||
|
Assert.AreEqual(correctAnswer, answer, "ToBin big 3 failed");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user