mirror of
https://bitbucket.org/Mattrixwv/javaclasses.git
synced 2025-12-06 23:13:57 -05:00
Added isPandigital function
This commit is contained in:
2
pom.xml
2
pom.xml
@@ -9,7 +9,7 @@
|
|||||||
<version>1.0-SNAPSHOT</version>
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
|
||||||
<name>myClasses</name>
|
<name>myClasses</name>
|
||||||
<!-- FIXME change it to the project's website -->
|
<!-- TODO: change it to the project's website -->
|
||||||
<url>http://www.example.com</url>
|
<url>http://www.example.com</url>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//JavaClasses/src/main/java/mattrixwv/StringAlgorithms.java
|
//JavaClasses/src/main/java/mattrixwv/StringAlgorithms.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 07-03-21
|
// Created: 07-03-21
|
||||||
//Modified: 07-03-21
|
//Modified: 10-11-21
|
||||||
//This class contains algorithms for strings that I've found it useful to keep around
|
//This class contains algorithms for strings that I've found it useful to keep around
|
||||||
/*
|
/*
|
||||||
Copyright (C) 2021 Matthew Ellison
|
Copyright (C) 2021 Matthew Ellison
|
||||||
@@ -86,5 +86,29 @@ public class StringAlgorithms{
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//Returns true if the string passed to it is a pandigital
|
||||||
|
public static boolean isPandigital(String str, char bottom, char top){
|
||||||
|
//Return false if top < bottom
|
||||||
|
if(top < bottom){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Return false if the wrong number of characters are in the string
|
||||||
|
if(str.length() != (top - bottom + 1)){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Make sure that all of the needed characters are in the string exactly one time
|
||||||
|
for(char cnt = bottom;cnt <= top;++cnt){
|
||||||
|
if(findNumOccurrence(str, cnt) != 1){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//If the function has reached this part it has passed all of the falsifying tests
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
public static boolean isPandigital(String str){
|
||||||
|
return isPandigital(str, '1', '9');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//JavaClasses/src/test/java/mattrixwv/TestStringAlgorithms.java
|
//JavaClasses/src/test/java/mattrixwv/TestStringAlgorithms.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 07-03-21
|
// Created: 07-03-21
|
||||||
//Modified: 07-03-21
|
//Modified: 10-11-21
|
||||||
//This class contains tests for my number algorithms
|
//This class contains tests for my number algorithms
|
||||||
/*
|
/*
|
||||||
Copyright (C) 2021 Matthew Ellison
|
Copyright (C) 2021 Matthew Ellison
|
||||||
@@ -76,4 +76,42 @@ public class TestStringAlgorithms{
|
|||||||
answer = StringAlgorithms.isPalindrome(str);
|
answer = StringAlgorithms.isPalindrome(str);
|
||||||
assertEquals("isPalindrome 3 failed", correctAnswer, answer);
|
assertEquals("isPalindrome 3 failed", correctAnswer, answer);
|
||||||
}
|
}
|
||||||
|
@Test
|
||||||
|
public void testIsPandigital(){
|
||||||
|
//Test 1
|
||||||
|
String num = "123456789";
|
||||||
|
boolean correctAnswer = true;
|
||||||
|
boolean answer = StringAlgorithms.isPandigital(num);
|
||||||
|
assertEquals("isPandigital 1 failed", correctAnswer, answer);
|
||||||
|
|
||||||
|
//Test 2
|
||||||
|
num = "123";
|
||||||
|
correctAnswer = true;
|
||||||
|
answer = StringAlgorithms.isPandigital(num, '1', '3');
|
||||||
|
assertEquals("isPandigital 2 failed", correctAnswer, answer);
|
||||||
|
|
||||||
|
//Test 3
|
||||||
|
num = "123";
|
||||||
|
correctAnswer = false;
|
||||||
|
answer = StringAlgorithms.isPandigital(num);
|
||||||
|
assertEquals("isPandigital 3 failed", correctAnswer, answer);
|
||||||
|
|
||||||
|
//Test 4
|
||||||
|
num = "123";
|
||||||
|
correctAnswer = false;
|
||||||
|
answer = StringAlgorithms.isPandigital(num, '3', '1');
|
||||||
|
assertEquals("inPandigital 4 failed", correctAnswer, answer);
|
||||||
|
|
||||||
|
//Test 5
|
||||||
|
num = "1";
|
||||||
|
correctAnswer = true;
|
||||||
|
answer = StringAlgorithms.isPandigital(num, '1', '1');
|
||||||
|
assertEquals("inPandigital 5 failed", correctAnswer, answer);
|
||||||
|
|
||||||
|
//Test 6
|
||||||
|
num = "112";
|
||||||
|
correctAnswer = false;
|
||||||
|
answer = StringAlgorithms.isPandigital(num, '1', '3');
|
||||||
|
assertEquals("inPandigital 6 failed", correctAnswer, answer);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user