Regular Expressions
Declarative Programming
Advanced Language Features
Programming Paradigms
Fundamentals
Strings
How easy is it to master even-and-odd-ology from a strong foundation of knowledge about obscure numeral systems?
class StringParity { public static String parityName(int n) { return Character.getName('౼' - n % 2).split(" ")[5]; } public static boolean isEvenLength(String str) { return parityName(str.length()).equals("EVEN"); } public static boolean isOddLength(String str) { return parityName(str.length()).equals("ODD"); } }
- class StringParity {
- public static String parityName(int n) {
- return Character.getName('౼' - n % 2).split(" ")[5];
- }
- public static boolean isEvenLength(String str) {
return str.replaceAll("(?s)..", "").isEmpty(); // (?s) enables DOTALL- return parityName(str.length()).equals("EVEN");
- }
- public static boolean isOddLength(String str) {
- return parityName(str.length()).equals("ODD");
- }
- }
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; class SolutionTest { @Test void testEven() { assertEquals(true, StringParity.isEvenLength("even")); assertEquals(false, StringParity.isEvenLength("odd")); assertEquals(true, StringParity.isEvenLength("")); assertEquals(false, StringParity.isEvenLength("Instant Noodles")); assertEquals(true, StringParity.isEvenLength("\n\n")); } @Test void testOdd() { assertEquals(false, StringParity.isOddLength("even")); assertEquals(true, StringParity.isOddLength("odd")); assertEquals(false, StringParity.isOddLength("")); assertEquals(true, StringParity.isOddLength("Instant Noodles")); assertEquals(false, StringParity.isOddLength("\n\n")); } }
- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- class SolutionTest {
- @Test
void testSomething() {- void testEven() {
- assertEquals(true, StringParity.isEvenLength("even"));
- assertEquals(false, StringParity.isEvenLength("odd"));
- assertEquals(true, StringParity.isEvenLength(""));
- assertEquals(false, StringParity.isEvenLength("Instant Noodles"));
- assertEquals(true, StringParity.isEvenLength("\n\n"));
- }
- @Test
- void testOdd() {
- assertEquals(false, StringParity.isOddLength("even"));
- assertEquals(true, StringParity.isOddLength("odd"));
- assertEquals(false, StringParity.isOddLength(""));
- assertEquals(true, StringParity.isOddLength("Instant Noodles"));
- assertEquals(false, StringParity.isOddLength("\n\n"));
- }
- }