Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,21 @@
|
|||
void setup(){
|
||||
println(isPalindrome(InsertPalindromeHere));
|
||||
}
|
||||
|
||||
boolean isPalindrome(string check){
|
||||
char[] letters = new char[check.length];
|
||||
string invert = " ";
|
||||
string modCheck = " " + check;
|
||||
for(int i = 0; i < letters.length; i++){
|
||||
letters[i] = check.charAt(i);
|
||||
}
|
||||
for(int i = letters.length-1; i >= 0; i--){
|
||||
invert = invert + letters[i];
|
||||
}
|
||||
|
||||
if(invert == modCheck){
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
void setup(){
|
||||
println("PalindromeDetection");
|
||||
|
||||
String[] tests = {
|
||||
"abcba",
|
||||
"aa",
|
||||
"a",
|
||||
"",
|
||||
" ",
|
||||
"ab",
|
||||
"abcdba",
|
||||
"A man, a plan, a canal: Panama!",
|
||||
"Dammit, I’m Mad!",
|
||||
"Never odd or even",
|
||||
"ingirumimusnocteetconsumimurigni"
|
||||
};
|
||||
|
||||
for (int i = 0; i < tests.length; i++){
|
||||
println((i + 1) + ". '" + tests[i] + "' isExactPalindrome: " + isExactPalindrome(tests[i]) + " isInexactPalindrome: " + isInexactPalindrome(tests[i]));
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Check for exact palindrome using StringBuilder and String since String in Java does not provide any reverse functionality because Strings are immutable.
|
||||
*/
|
||||
boolean isExactPalindrome(String s){
|
||||
StringBuilder sb = new StringBuilder(s);
|
||||
return s.equals(sb.reverse().toString());
|
||||
}
|
||||
|
||||
/*
|
||||
* Check for inexact palindrome using the check for exact palindromeabove.
|
||||
*/
|
||||
boolean isInexactPalindrome(String s){
|
||||
// removes all whitespaces and non-visible characters,
|
||||
// remove anything besides alphabet characters
|
||||
// ignore case
|
||||
return isExactPalindrome(s.replaceAll("\\s+","").replaceAll("[^A-Za-z]+", "").toLowerCase());
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue