Added isPandigital function

This commit is contained in:
2021-10-11 12:58:16 -04:00
parent 97a84dbe34
commit e56fe7dc5a
3 changed files with 65 additions and 3 deletions

View File

@@ -9,7 +9,7 @@
<version>1.0-SNAPSHOT</version>
<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>
<properties>

View File

@@ -1,7 +1,7 @@
//JavaClasses/src/main/java/mattrixwv/StringAlgorithms.java
//Matthew Ellison
// 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
/*
Copyright (C) 2021 Matthew Ellison
@@ -86,5 +86,29 @@ public class StringAlgorithms{
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');
}
}

View File

@@ -1,7 +1,7 @@
//JavaClasses/src/test/java/mattrixwv/TestStringAlgorithms.java
//Matthew Ellison
// Created: 07-03-21
//Modified: 07-03-21
//Modified: 10-11-21
//This class contains tests for my number algorithms
/*
Copyright (C) 2021 Matthew Ellison
@@ -76,4 +76,42 @@ public class TestStringAlgorithms{
answer = StringAlgorithms.isPalindrome(str);
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);
}
}