mirror of
https://bitbucket.org/Mattrixwv/javaclasses.git
synced 2025-12-06 23:13:57 -05:00
Added functions to check for palindromes and convert nums to bin strings
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
//JavaClasses/src/main/java/mattrixwv/Algorithms.java
|
//JavaClasses/src/main/java/mattrixwv/Algorithms.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 03-02-19
|
// Created: 03-02-19
|
||||||
//Modified: 06-01-21
|
//Modified: 06-29-21
|
||||||
//This class holds many algorithms that I have found it useful to keep around
|
//This class holds many algorithms that I have found it useful to keep around
|
||||||
//As such all of the functions in here are static and meant to be used as stand alone functions
|
//As such all of the functions in here are static and meant to be used as stand alone functions
|
||||||
/*
|
/*
|
||||||
@@ -945,4 +945,27 @@ public class Algorithms{
|
|||||||
public static long findNumOccurrence(String str, char c){
|
public static long findNumOccurrence(String str, char c){
|
||||||
return str.chars().filter(ch -> ch == c).count();
|
return str.chars().filter(ch -> ch == c).count();
|
||||||
}
|
}
|
||||||
|
//Returns true if the string passed in is a palindrome
|
||||||
|
public static boolean isPalindrome(String str){
|
||||||
|
String rev = new StringBuilder(str).reverse().toString();
|
||||||
|
if(str.equals(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
|
||||||
|
return Integer.toBinaryString(num);
|
||||||
|
}
|
||||||
|
public static String toBin(long num){
|
||||||
|
//Convert the number to binary string
|
||||||
|
return Long.toBinaryString(num);
|
||||||
|
}
|
||||||
|
public static String toBin(BigInteger num){
|
||||||
|
//Conver the number to binary string
|
||||||
|
return num.toString(2);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//JavaClasses/src/test/java/mattrixwv/TestAlgorithms.java
|
//JavaClasses/src/test/java/mattrixwv/TestAlgorithms.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 06-07-20
|
// Created: 06-07-20
|
||||||
//Modified: 06-01-21
|
//Modified: 06-29-21
|
||||||
//This class holds many algorithms that I have found it useful to keep around
|
//This class holds many algorithms that I have found it useful to keep around
|
||||||
//As such all of the functions in here are static and meant to be used as stand alone functions
|
//As such all of the functions in here are static and meant to be used as stand alone functions
|
||||||
/*
|
/*
|
||||||
@@ -476,4 +476,72 @@ public class TestAlgorithms{
|
|||||||
answer = Algorithms.findNumOccurrence(testString, testChar);
|
answer = Algorithms.findNumOccurrence(testString, testChar);
|
||||||
assertEquals("FindNumOccurrence 3 failed", correctAnswer, answer);
|
assertEquals("FindNumOccurrence 3 failed", correctAnswer, answer);
|
||||||
}
|
}
|
||||||
|
@Test
|
||||||
|
public void testIsPalindrome(){
|
||||||
|
//Test 1
|
||||||
|
String str = "101";
|
||||||
|
boolean correctAnswer = true;
|
||||||
|
boolean answer = Algorithms.isPalindrome(str);
|
||||||
|
assertEquals("isPalindrome 1 failed", correctAnswer, answer);
|
||||||
|
//Test 2
|
||||||
|
str = "100";
|
||||||
|
correctAnswer = false;
|
||||||
|
answer = Algorithms.isPalindrome(str);
|
||||||
|
assertEquals("isPalindrome 2 failed", correctAnswer, answer);
|
||||||
|
//Test 3
|
||||||
|
str = "";
|
||||||
|
correctAnswer = true;
|
||||||
|
answer = Algorithms.isPalindrome(str);
|
||||||
|
assertEquals("isPalindrome 3 failed", correctAnswer, answer);
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void testToBin(){
|
||||||
|
//Test 1
|
||||||
|
int num = 7;
|
||||||
|
String correctAnswer = "111";
|
||||||
|
String answer = Algorithms.toBin(num);
|
||||||
|
assertEquals("toBin 1 failed", correctAnswer, answer);
|
||||||
|
//Test 2
|
||||||
|
num = 0;
|
||||||
|
correctAnswer = "0";
|
||||||
|
answer = Algorithms.toBin(num);
|
||||||
|
assertEquals("toBin 2 failed", correctAnswer, answer);
|
||||||
|
//Test 3
|
||||||
|
num = 1000000;
|
||||||
|
correctAnswer = "11110100001001000000";
|
||||||
|
answer = Algorithms.toBin(num);
|
||||||
|
assertEquals("toBin 3 failed", correctAnswer, answer);
|
||||||
|
|
||||||
|
//Test 4
|
||||||
|
long longNum = 7;
|
||||||
|
correctAnswer = "111";
|
||||||
|
answer = Algorithms.toBin(longNum);
|
||||||
|
assertEquals("toBin long 1 failed", correctAnswer, answer);
|
||||||
|
//Test 5
|
||||||
|
longNum = 0;
|
||||||
|
correctAnswer = "0";
|
||||||
|
answer = Algorithms.toBin(longNum);
|
||||||
|
assertEquals("toBin long 2 failed", correctAnswer, answer);
|
||||||
|
//Test 6
|
||||||
|
longNum = 1000000;
|
||||||
|
correctAnswer = "11110100001001000000";
|
||||||
|
answer = Algorithms.toBin(longNum);
|
||||||
|
assertEquals("toBin long 3 failed", correctAnswer, answer);
|
||||||
|
|
||||||
|
//Test 7
|
||||||
|
BigInteger bigNum = BigInteger.valueOf(7);
|
||||||
|
correctAnswer = "111";
|
||||||
|
answer = Algorithms.toBin(bigNum);
|
||||||
|
assertEquals("toBin big 1 failed", correctAnswer, answer);
|
||||||
|
//Test 8
|
||||||
|
bigNum = BigInteger.ZERO;
|
||||||
|
correctAnswer = "0";
|
||||||
|
answer = Algorithms.toBin(bigNum);
|
||||||
|
assertEquals("toBin big 2 failed", correctAnswer, answer);
|
||||||
|
//Test 9
|
||||||
|
bigNum = BigInteger.valueOf(1000000);
|
||||||
|
correctAnswer = "11110100001001000000";
|
||||||
|
answer = Algorithms.toBin(bigNum);
|
||||||
|
assertEquals("toBin big 3 failed", correctAnswer, answer);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user