Puzzles
Regular Expressions
import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.Arrays; public class PasswordValidation { private static final String TODAY_DATE = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")); private static final String[] VALID_MONTHS = { "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec" }; public static boolean validatePassword(String password) { if (password.length() < 20 | password.length() > 50) return false; if (!isPrime(password.length())) return false; if (!password.contains(String.valueOf(password.length()))) return false; if (!password.contains(TODAY_DATE)) return false; if (!Arrays.stream(VALID_MONTHS).anyMatch(s -> password.toLowerCase().contains(s))) return false; if (!password.matches(("(.*[A-Z].*){3,}")) || !password.matches("(.*[a-z].*){1,}") || !password.matches("(.*[-._@#$&].*){1,}") || password.matches(".*[-._@#$&][-._@#$&].*")) return false; if (Arrays.stream(password.split("")).filter(s -> s.matches("[0-9]")).mapToInt(s -> Integer.valueOf(s)).sum() < 25) return false; return true; } public static boolean isPrime(int n) { for (int i = 2; i <= Math.sqrt(n); i++) { if (n % i == 0) return false; } return true; } }
- import java.time.LocalDate;
- import java.time.format.DateTimeFormatter;
- import java.util.Arrays;
- public class PasswordValidation {
- private static final String TODAY_DATE = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
- private static final String[] VALID_MONTHS = { "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct",
- "nov", "dec" };
- public static boolean validatePassword(String password) {
- if (password.length() < 20 | password.length() > 50)
- return false;
- if (!isPrime(password.length()))
- return false;
- if (!password.contains(String.valueOf(password.length())))
- return false;
- if (!password.contains(TODAY_DATE))
- return false;
- if (!Arrays.stream(VALID_MONTHS).anyMatch(s -> password.toLowerCase().contains(s)))
- return false;
if (password.matches("[A-Z]{0,2}|[^a-z]+|[^-._@#$&]+|.*[-._@#$&][-._@#$&].*"))- if (!password.matches(("(.*[A-Z].*){3,}"))
- || !password.matches("(.*[a-z].*){1,}")
- || !password.matches("(.*[-._@#$&].*){1,}")
- || password.matches(".*[-._@#$&][-._@#$&].*"))
- return false;
- if (Arrays.stream(password.split("")).filter(s -> s.matches("[0-9]")).mapToInt(s -> Integer.valueOf(s)).sum() < 25)
- return false;
- return true;
- }
- public static boolean isPrime(int n) {
- for (int i = 2; i <= Math.sqrt(n); i++) {
- if (n % i == 0)
- return false;
- }
- return true;
- }
- }
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.Test; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.Arrays; import java.util.Random; class SolutionTest { // Today's date private static final String TODAY_DATE = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")); // String of all available characters private static final String CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.-_@#$&0123456789"; // Diminutive of the months private static final String[] VALID_MONTHS = { "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec" }; // List of all possible lengths. Prime numbers are more than once so they appear // more often private static final int[] LENGTHS = { 20, 21, 22, 23, 23, 23, 23, 23, 23, 23, 23, 24, 25, 26, 27, 28, 29, 29, 29, 29, 29, 30, 31, 31, 31, 31, 31, 32, 33, 34, 35, 36, 37, 37, 37, 37, 37, 38, 39, 40, 41, 41, 41, 41, 41, 42, 43, 43, 43, 43, 43, 44, 45, 46, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 48, 49, 50 }; // Prime numbers between 20 & 50 private static final int[] PRIME_LENGTHS = { 23, 29, 31, 37, 41, 43, 47 }; @Test void basicTest() { assertFalse(PasswordValidation.validatePassword("")); assertTrue(PasswordValidation.validatePassword("pass73$OCT" + TODAY_DATE + "len23")); assertFalse(PasswordValidation.validatePassword("PASS73$OCT" + TODAY_DATE + "LEN23")); assertFalse(PasswordValidation.validatePassword( TODAY_DATE + "TOdaY.feb29P6g@#fahHl")); assertFalse(PasswordValidation.validatePassword("sib$dic&omonUl98DSA" + TODAY_DATE + "len17shJDAIDB78334&Okk.53jfh")); assertTrue(PasswordValidation.validatePassword("dskjRUE3576&sj_88@mAyJun" + TODAY_DATE + "JUL47.1j6SB2nI7")); } @Test void largestBasicTest() { String randomDay = LocalDate.now().plusDays(2).format(DateTimeFormatter.ofPattern("yyyyMMdd")); assertTrue(PasswordValidation.validatePassword("TOdaY" + TODAY_DATE + ".feb29P6gwo@fahH")); assertTrue(PasswordValidation.validatePassword("h&ks-KlOCt" + TODAY_DATE + "#aai78Ho37M7M_lj73.")); assertTrue(PasswordValidation.validatePassword(TODAY_DATE + "&jUN1.df487-ks228d43#hULP0@cb111baQ")); assertFalse(PasswordValidation.validatePassword("k17DiC9@P" + TODAY_DATE)); assertFalse(PasswordValidation.validatePassword("hJAN" + TODAY_DATE + "&17a9")); assertFalse(PasswordValidation.validatePassword("p6NOv#L17" + TODAY_DATE)); assertFalse(PasswordValidation.validatePassword("MyP@ssworD-67sAloU" + TODAY_DATE + "29o")); assertFalse(PasswordValidation.validatePassword("MyP@sswMAy-67sAlU" + TODAY_DATE + "29o")); assertFalse(PasswordValidation.validatePassword("Lol_sC_apR01" + TODAY_DATE + "nOp")); assertFalse(PasswordValidation.validatePassword(".SiL" + TODAY_DATE + "jUt7190lAUG2f27")); assertFalse(PasswordValidation.validatePassword("JupoJUN43.90-12plUSlenght31$h#1")); assertFalse(PasswordValidation.validatePassword("n0TC84nTeInmar.nn_" + randomDay + "poAl98712mQ")); } public static String generateRandomPass() { Random random = new Random(); StringBuilder pass = new StringBuilder(); int length = LENGTHS[random.nextInt(LENGTHS.length)]; int month_pos = random.nextInt(length - 3); int date_pos = random.nextInt(length - 8); int length_pos = random.nextInt(length - 2); for (int i = 0; i < length; i++) { pass.append(CHARS.charAt(random.nextInt(CHARS.length()))); if (i == month_pos) { pass.append(VALID_MONTHS[random.nextInt(VALID_MONTHS.length)]); i += 3; } if (i == length_pos) { pass.append(length); i += 2; } if (i == date_pos) { pass.append(TODAY_DATE); i += 8; } } return pass.toString(); } public static boolean checkPass(String pass) { if (!Arrays.stream(PRIME_LENGTHS).anyMatch(n -> pass.length() == n) || !pass.contains(String.valueOf(pass.length())) || !pass.contains(TODAY_DATE) || !Arrays.stream(VALID_MONTHS).anyMatch(s -> pass.toLowerCase().contains(s)) || !pass.matches(("(.*[A-Z].*){3,}")) || !pass.matches("(.*[a-z].*){1,}") || !pass.matches("(.*[-._@#$&].*){1,}") || pass.matches(".*[-._@#$&][-._@#$&].*") || Arrays.stream(pass.split("")) .filter(s -> s.matches("[0-9]")).mapToInt(s -> Integer.valueOf(s)).sum() < 25) { return false; } return true; } @RepeatedTest(100) void randomTest() { String randomPass = generateRandomPass(); assertEquals(checkPass(randomPass), PasswordValidation.validatePassword(randomPass)); } }
- import static org.junit.jupiter.api.Assertions.assertEquals;
- import static org.junit.Assert.assertFalse;
- import static org.junit.Assert.assertTrue;
- import org.junit.jupiter.api.RepeatedTest;
- import org.junit.jupiter.api.Test;
- import java.time.LocalDate;
- import java.time.format.DateTimeFormatter;
- import java.util.Arrays;
- import java.util.Random;
- class SolutionTest {
- // Today's date
- private static final String TODAY_DATE = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
- // String of all available characters
- private static final String CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.-_@#$&0123456789";
- // Diminutive of the months
- private static final String[] VALID_MONTHS = { "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct",
- "nov", "dec" };
- // List of all possible lengths. Prime numbers are more than once so they appear
- // more often
- private static final int[] LENGTHS = { 20, 21, 22, 23, 23, 23, 23, 23, 23, 23, 23, 24, 25, 26, 27, 28, 29, 29, 29,
- 29, 29, 30, 31, 31, 31, 31, 31, 32, 33, 34, 35, 36, 37, 37, 37, 37, 37, 38, 39, 40, 41, 41, 41, 41, 41, 42,
- 43, 43, 43, 43, 43, 44, 45, 46, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 48, 49, 50 };
- // Prime numbers between 20 & 50
- private static final int[] PRIME_LENGTHS = { 23, 29, 31, 37, 41, 43, 47 };
- @Test
- void basicTest() {
- assertFalse(PasswordValidation.validatePassword(""));
- assertTrue(PasswordValidation.validatePassword("pass73$OCT" + TODAY_DATE + "len23"));
- assertFalse(PasswordValidation.validatePassword("PASS73$OCT" + TODAY_DATE + "LEN23"));
- assertFalse(PasswordValidation.validatePassword( TODAY_DATE + "TOdaY.feb29P6g@#fahHl"));
- assertFalse(PasswordValidation.validatePassword("sib$dic&omonUl98DSA" + TODAY_DATE + "len17shJDAIDB78334&Okk.53jfh"));
- assertTrue(PasswordValidation.validatePassword("dskjRUE3576&sj_88@mAyJun" + TODAY_DATE + "JUL47.1j6SB2nI7"));
- }
- @Test
- void largestBasicTest() {
- String randomDay = LocalDate.now().plusDays(2).format(DateTimeFormatter.ofPattern("yyyyMMdd"));
- assertTrue(PasswordValidation.validatePassword("TOdaY" + TODAY_DATE + ".feb29P6gwo@fahH"));
- assertTrue(PasswordValidation.validatePassword("h&ks-KlOCt" + TODAY_DATE + "#aai78Ho37M7M_lj73."));
- assertTrue(PasswordValidation.validatePassword(TODAY_DATE + "&jUN1.df487-ks228d43#hULP0@cb111baQ"));
- assertFalse(PasswordValidation.validatePassword("k17DiC9@P" + TODAY_DATE));
- assertFalse(PasswordValidation.validatePassword("hJAN" + TODAY_DATE + "&17a9"));
- assertFalse(PasswordValidation.validatePassword("p6NOv#L17" + TODAY_DATE));
- assertFalse(PasswordValidation.validatePassword("MyP@ssworD-67sAloU" + TODAY_DATE + "29o"));
- assertFalse(PasswordValidation.validatePassword("MyP@sswMAy-67sAlU" + TODAY_DATE + "29o"));
- assertFalse(PasswordValidation.validatePassword("Lol_sC_apR01" + TODAY_DATE + "nOp"));
- assertFalse(PasswordValidation.validatePassword(".SiL" + TODAY_DATE + "jUt7190lAUG2f27"));
- assertFalse(PasswordValidation.validatePassword("JupoJUN43.90-12plUSlenght31$h#1"));
- assertFalse(PasswordValidation.validatePassword("n0TC84nTeInmar.nn_" + randomDay + "poAl98712mQ"));
- }
- public static String generateRandomPass() {
- Random random = new Random();
- StringBuilder pass = new StringBuilder();
- int length = LENGTHS[random.nextInt(LENGTHS.length)];
- int month_pos = random.nextInt(length - 3);
- int date_pos = random.nextInt(length - 8);
- int length_pos = random.nextInt(length - 2);
- for (int i = 0; i < length; i++) {
- pass.append(CHARS.charAt(random.nextInt(CHARS.length())));
- if (i == month_pos) {
- pass.append(VALID_MONTHS[random.nextInt(VALID_MONTHS.length)]);
- i += 3;
- }
- if (i == length_pos) {
- pass.append(length);
- i += 2;
- }
- if (i == date_pos) {
- pass.append(TODAY_DATE);
- i += 8;
- }
- }
- return pass.toString();
- }
- public static boolean checkPass(String pass) {
- if (!Arrays.stream(PRIME_LENGTHS).anyMatch(n -> pass.length() == n)
- || !pass.contains(String.valueOf(pass.length()))
- || !pass.contains(TODAY_DATE)
- || !Arrays.stream(VALID_MONTHS).anyMatch(s -> pass.toLowerCase().contains(s))
|| pass.matches("[^A-Z]{3,}|[^a-z]+|[^-._@#$&]+|.*[-._@#$&][-._@#$&].*")- || !pass.matches(("(.*[A-Z].*){3,}"))
- || !pass.matches("(.*[a-z].*){1,}")
- || !pass.matches("(.*[-._@#$&].*){1,}")
- || pass.matches(".*[-._@#$&][-._@#$&].*")
- || Arrays.stream(pass.split(""))
- .filter(s -> s.matches("[0-9]")).mapToInt(s -> Integer.valueOf(s)).sum() < 25) {
- return false;
- }
- return true;
- }
- @RepeatedTest(100)
- void randomTest() {
- String randomPass = generateRandomPass();
- assertEquals(checkPass(randomPass), PasswordValidation.validatePassword(randomPass));
- }
- }
Puzzles
Regular Expressions
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.Arrays; import java.util.Random; import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.Test; class SolutionTest { // Today's date private static final String TODAY_DATE = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")); // String of all available characters private static final String CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.-_@#$&0123456789"; // Diminutive of the months private static final String[] VALID_MONTHS = { "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec" }; // List of all possible lengths. Prime numbers are more than once so they appear // more often private static final int[] LENGTHS = { 20, 21, 22, 23, 23, 23, 23, 23, 24, 25, 26, 27, 28, 29, 29, 29, 29, 29, 30, 31, 31, 31, 31, 31, 32, 33, 34, 35, 36, 37, 37, 37, 37, 37, 38, 39, 40, 41, 41, 41, 41, 41, 42, 43, 43, 43, 43, 43, 44, 45, 46, 47, 47, 47, 47, 47, 48, 49, 50 }; @Test void basicTest() { assertFalse(PasswordValidation.validatePassword("")); assertTrue(PasswordValidation.validatePassword("pass73$OCT" + TODAY_DATE + "len23")); assertFalse(PasswordValidation.validatePassword("PASS73$OCT" + TODAY_DATE + "LEN23")); assertFalse(PasswordValidation.validatePassword("TOdaY" + TODAY_DATE + ".feb29P6g@#fahHl")); assertFalse(PasswordValidation.validatePassword("sib$dic&omonUl98DSA" + TODAY_DATE + "len17shJDAIDB78334&Okk.53jfh")); assertTrue(PasswordValidation.validatePassword("dskjRUE3576&sj_88@mAyJun" + TODAY_DATE + "JUL47.1j6SB2nI7")); } @Test void largestBasicTest() { String randomDay = LocalDate.now().plusDays(2).format(DateTimeFormatter.ofPattern("yyyyMMdd")); assertTrue(PasswordValidation.validatePassword("TOdaY" + TODAY_DATE + ".feb29P6gwo@fahH")); assertTrue(PasswordValidation.validatePassword("h&ks-KlOCt" + TODAY_DATE + "#aai78Ho37M7M_lj73.")); assertTrue(PasswordValidation.validatePassword(TODAY_DATE + "&jUN1.df487-ks228d43#hULP0@cb111baQ")); assertFalse(PasswordValidation.validatePassword("k17DiC9@P" + TODAY_DATE)); assertFalse(PasswordValidation.validatePassword("hJAN" + TODAY_DATE + "&17a9")); assertFalse(PasswordValidation.validatePassword("p6NOv#L17" + TODAY_DATE)); assertFalse(PasswordValidation.validatePassword("MyP@ssworD-67sAloU" + TODAY_DATE + "29o")); assertFalse(PasswordValidation.validatePassword("MyP@sswMAy-67sAlU" + TODAY_DATE + "29o")); assertFalse(PasswordValidation.validatePassword("Lol_sC_apR01" + TODAY_DATE + "nOp")); assertFalse(PasswordValidation.validatePassword(".SiL" + TODAY_DATE + "jUt7190lAUG2f27")); assertFalse(PasswordValidation.validatePassword("JupoJUN43.90-12plUSlenght31$h#1")); assertFalse(PasswordValidation.validatePassword("n0TC84nTeInmar.nn_" + randomDay + "poAl98712mQ")); } public static String generateRandomPass() { Random random = new Random(); StringBuilder pass = new StringBuilder(); int length = LENGTHS[random.nextInt(LENGTHS.length)]; int month_pos = random.nextInt(length - 3); int date_pos = random.nextInt(length); int length_pos = random.nextInt(length - 2); for (int i = 0; i < length; i++) { pass.append(CHARS.charAt(random.nextInt(CHARS.length()))); if (i == month_pos) { pass.append(VALID_MONTHS[random.nextInt(VALID_MONTHS.length)]); i += 3; } if (i == length_pos) { pass.append(length); i += 2; } if (i == date_pos) { pass.append(TODAY_DATE); i += 8; } } return pass.toString(); } public static boolean checkPass(String pass) { if (pass.length() < 20 | pass.length() > 50) return false; if (!isPrime(pass.length())) return false; if (!pass.contains(String.valueOf(pass.length()))) return false; if (!pass.contains(TODAY_DATE)) return false; if (!Arrays.stream(VALID_MONTHS).anyMatch(s -> pass.toLowerCase().contains(s))) return false; if (pass.matches("[A-Z]{0,2}|[^a-z]+|[^-._@#$&]+|.*[-._@#$&][-._@#$&].*")) return false; if (Arrays.stream(pass.split("")).filter(s -> s.matches("[0-9]")).mapToInt(s -> Integer.valueOf(s)).sum() < 25) return false; return true; } public static boolean isPrime(int n) { for (int i = 2; i <= Math.sqrt(n); i++) { if (n % i == 0) return false; } return true; } @RepeatedTest(100) void randomTest() { String randomPass = generateRandomPass(); assertEquals(checkPass(randomPass), PasswordValidation.validatePassword(randomPass)); } }
- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- import static org.junit.Assert.assertFalse;
- import static org.junit.Assert.assertTrue;
- import java.time.LocalDate;
- import java.time.format.DateTimeFormatter;
- import java.util.Arrays;
- import java.util.Random;
- import org.junit.jupiter.api.RepeatedTest;
- import org.junit.jupiter.api.Test;
- class SolutionTest {
- // Today's date
- private static final String TODAY_DATE = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
- // String of all available characters
- private static final String CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.-_@#$&0123456789";
- // Diminutive of the months
- private static final String[] VALID_MONTHS = { "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec" };
- // List of all possible lengths. Prime numbers are more than once so they appear
- // more often
- private static final int[] LENGTHS = { 20, 21, 22, 23, 23, 23, 23, 23, 24, 25, 26, 27, 28, 29, 29, 29, 29, 29, 30,
- 31, 31, 31, 31, 31, 32, 33, 34, 35, 36, 37, 37, 37, 37, 37, 38, 39, 40, 41, 41, 41, 41, 41, 42, 43, 43, 43,
- 43, 43, 44, 45, 46, 47, 47, 47, 47, 47, 48, 49, 50 };
- @Test
- void basicTest() {
- assertFalse(PasswordValidation.validatePassword(""));
- assertTrue(PasswordValidation.validatePassword("pass73$OCT" + TODAY_DATE + "len23"));
- assertFalse(PasswordValidation.validatePassword("PASS73$OCT" + TODAY_DATE + "LEN23"));
- assertFalse(PasswordValidation.validatePassword("TOdaY" + TODAY_DATE + ".feb29P6g@#fahHl"));
- assertFalse(PasswordValidation.validatePassword("sib$dic&omonUl98DSA" + TODAY_DATE + "len17shJDAIDB78334&Okk.53jfh"));
- assertTrue(PasswordValidation.validatePassword("dskjRUE3576&sj_88@mAyJun" + TODAY_DATE + "JUL47.1j6SB2nI7"));
- }
- @Test
- void largestBasicTest() {
- String randomDay = LocalDate.now().plusDays(2).format(DateTimeFormatter.ofPattern("yyyyMMdd"));
- assertTrue(PasswordValidation.validatePassword("TOdaY" + TODAY_DATE + ".feb29P6gwo@fahH"));
- assertTrue(PasswordValidation.validatePassword("h&ks-KlOCt" + TODAY_DATE + "#aai78Ho37M7M_lj73."));
- assertTrue(PasswordValidation.validatePassword(TODAY_DATE + "&jUN1.df487-ks228d43#hULP0@cb111baQ"));
- assertFalse(PasswordValidation.validatePassword("k17DiC9@P" + TODAY_DATE));
- assertFalse(PasswordValidation.validatePassword("hJAN" + TODAY_DATE + "&17a9"));
- assertFalse(PasswordValidation.validatePassword("p6NOv#L17" + TODAY_DATE));
- assertFalse(PasswordValidation.validatePassword("MyP@ssworD-67sAloU" + TODAY_DATE + "29o"));
- assertFalse(PasswordValidation.validatePassword("MyP@sswMAy-67sAlU" + TODAY_DATE + "29o"));
- assertFalse(PasswordValidation.validatePassword("Lol_sC_apR01" + TODAY_DATE + "nOp"));
- assertFalse(PasswordValidation.validatePassword(".SiL" + TODAY_DATE + "jUt7190lAUG2f27"));
- assertFalse(PasswordValidation.validatePassword("JupoJUN43.90-12plUSlenght31$h#1"));
- assertFalse(PasswordValidation.validatePassword("n0TC84nTeInmar.nn_" + randomDay + "poAl98712mQ"));
- }
- public static String generateRandomPass() {
- Random random = new Random();
- StringBuilder pass = new StringBuilder();
- int length = LENGTHS[random.nextInt(LENGTHS.length)];
for (int i = 0; i < length; i++) {int position = random.nextInt(4);switch (position) {case 0 -> pass.append(CHARS.charAt(random.nextInt(CHARS.length())));case 1 -> {int monthIndex = random.nextInt(VALID_MONTHS.length);pass.append(VALID_MONTHS[monthIndex]);i += 2;}case 2 -> {pass.append(length);i++;}case 3 ->{pass.append(TODAY_DATE);i += 7;}}}return pass.toString();- int month_pos = random.nextInt(length - 3);
- int date_pos = random.nextInt(length);
- int length_pos = random.nextInt(length - 2);
- for (int i = 0; i < length; i++) {
- pass.append(CHARS.charAt(random.nextInt(CHARS.length())));
- if (i == month_pos) {
- pass.append(VALID_MONTHS[random.nextInt(VALID_MONTHS.length)]);
- i += 3;
- }
- if (i == length_pos) {
- pass.append(length);
- i += 2;
- }
- if (i == date_pos) {
- pass.append(TODAY_DATE);
- i += 8;
- }
- }
- return pass.toString();
- }
- public static boolean checkPass(String pass) {
- if (pass.length() < 20 | pass.length() > 50)
- return false;
- if (!isPrime(pass.length()))
- return false;
- if (!pass.contains(String.valueOf(pass.length())))
- return false;
- if (!pass.contains(TODAY_DATE))
- return false;
- if (!Arrays.stream(VALID_MONTHS).anyMatch(s -> pass.toLowerCase().contains(s)))
- return false;
- if (pass.matches("[A-Z]{0,2}|[^a-z]+|[^-._@#$&]+|.*[-._@#$&][-._@#$&].*"))
- return false;
- if (Arrays.stream(pass.split("")).filter(s -> s.matches("[0-9]")).mapToInt(s -> Integer.valueOf(s)).sum() < 25)
- return false;
- return true;
- }
- public static boolean isPrime(int n) {
- for (int i = 2; i <= Math.sqrt(n); i++) {
- if (n % i == 0)
- return false;
- }
- return true;
- }
- @RepeatedTest(100)
- void randomTest() {
- String randomPass = generateRandomPass();
- assertEquals(checkPass(randomPass), PasswordValidation.validatePassword(randomPass));
- }
- }
Puzzles
Regular Expressions
import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.Arrays; public class PasswordValidation { private static final String TODAY_DATE = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")); private static final String[] VALID_MONTHS = { "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec" }; public static boolean validatePassword(String password) { if (password.length() < 20 | password.length() > 50) return false; if (!isPrime(password.length())) return false; if (!password.contains(String.valueOf(password.length()))) return false; if (!password.contains(TODAY_DATE)) return false; if (!Arrays.stream(VALID_MONTHS).anyMatch(s -> password.toLowerCase().contains(s))) return false; if (password.matches("[A-Z]{0,2}|[^a-z]+|[^-._@#$&]+|.*[-._@#$&][-._@#$&].*")) return false; if (Arrays.stream(password.split("")).filter(s -> s.matches("[0-9]")).mapToInt(s -> Integer.valueOf(s)).sum() < 25) return false; return true; } public static boolean isPrime(int n) { for (int i = 2; i <= Math.sqrt(n); i++) { if (n % i == 0) return false; } return true; } }
- import java.time.LocalDate;
- import java.time.format.DateTimeFormatter;
- import java.util.Arrays;
- public class PasswordValidation {
- private static final String TODAY_DATE = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
- private static final String[] VALID_MONTHS = { "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct",
- "nov", "dec" };
public static boolean validatePassword(String password) {- public static boolean validatePassword(String password) {
- if (password.length() < 20 | password.length() > 50)
- return false;
- if (!isPrime(password.length()))
- return false;
- if (!password.contains(String.valueOf(password.length())))
- return false;
- if (!password.contains(TODAY_DATE))
- return false;
- if (!Arrays.stream(VALID_MONTHS).anyMatch(s -> password.toLowerCase().contains(s)))
- return false;
- if (password.matches("[A-Z]{0,2}|[^a-z]+|[^-._@#$&]+|.*[-._@#$&][-._@#$&].*"))
- return false;
- if (Arrays.stream(password.split("")).filter(s -> s.matches("[0-9]")).mapToInt(s -> Integer.valueOf(s)).sum() < 25)
- return false;
- return true;
- }
- public static boolean isPrime(int n) {
- for (int i = 2; i <= Math.sqrt(n); i++) {
- if (n % i == 0)
- return false;
- }
- return true;
- }
- }