package com.mystuff.juststuff; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; import java.util.Arrays; import java.util.Set; import java.util.SortedSet; import java.util.TreeSet; import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.stream.IntStream; public class Palindrome { private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMddyyyy"); public Set<LocalDate> countDatePalindromes(final LocalDate startDate, final LocalDate endDate) { final SortedSet<LocalDate> sortedDates = new TreeSet<>(Arrays.asList(startDate, endDate)); return IntStream.rangeClosed(sortedDates.first().getYear(), sortedDates.last().getYear()) .filter(Palindrome::isPalindromePossible) .mapToObj(Palindrome::createPalindrome) .filter(isDateInRange(sortedDates.first(), sortedDates.last())) .collect(Collectors.toCollection(TreeSet::new)); } private static boolean isPalindromePossible(int year) { int monthFirstDigit = year % 10; return monthFirstDigit == 0 || monthFirstDigit == 1; } private static LocalDate createPalindrome(final int year) { final String yearStr = String.valueOf(year); final String datePalindrome = new StringBuilder(yearStr).reverse().append(yearStr).toString(); try { return LocalDate.parse(datePalindrome, formatter); } catch (final DateTimeParseException e) {} return null; } private static Predicate<LocalDate> isDateInRange(final LocalDate startDate, final LocalDate endDate) { return (date) -> !(date == null || date.isBefore(startDate) || date.isAfter(endDate)); } }
- package com.mystuff.juststuff;
- import java.time.LocalDate;
- import java.time.format.DateTimeFormatter;
- import java.time.format.DateTimeParseException;
- import java.util.Arrays;
- import java.util.Set;
- import java.util.SortedSet;
- import java.util.TreeSet;
- import java.util.function.Predicate;
- import java.util.stream.Collectors;
- import java.util.stream.IntStream;
- public class Palindrome {
- private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMddyyyy");
- public Set<LocalDate> countDatePalindromes(final LocalDate startDate, final LocalDate endDate) {
- final SortedSet<LocalDate> sortedDates = new TreeSet<>(Arrays.asList(startDate, endDate));
- return IntStream.rangeClosed(sortedDates.first().getYear(), sortedDates.last().getYear())
- .filter(Palindrome::isPalindromePossible)
- .mapToObj(Palindrome::createPalindrome)
- .filter(isDateInRange(sortedDates.first(), sortedDates.last()))
- .collect(Collectors.toCollection(TreeSet::new));
- }
- private static boolean isPalindromePossible(int year) {
- int monthFirstDigit = year % 10;
- return monthFirstDigit == 0 || monthFirstDigit == 1;
- }
- private static LocalDate createPalindrome(final int year) {
- final String yearStr = String.valueOf(year);
- final String datePalindrome = new StringBuilder(yearStr).reverse().append(yearStr).toString();
- try {
- return LocalDate.parse(datePalindrome, formatter);
- } catch (final DateTimeParseException e) {}
- return null;
- }
- private static Predicate<LocalDate> isDateInRange(final LocalDate startDate, final LocalDate endDate) {
- return (date) -> !(date == null || date.isBefore(startDate) || date.isAfter(endDate));
- }
- }
package com.mystuff.juststuff; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; import java.util.Set; import java.util.TreeSet; import java.util.stream.Collectors; import java.util.stream.IntStream; public class Palindrome { private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMddyyyy"); public Set<LocalDate> countDatePalindromes(final LocalDate startDate, final LocalDate endDate) { return IntStream.range(startDate.getYear(), endDate.getYear() + 1).mapToObj(Palindrome::createPalindrome) .filter(date -> isValidDate(date, startDate, endDate)).collect(Collectors.toCollection(TreeSet::new)); } private static LocalDate createPalindrome(int year) { String yearStr = String.valueOf(year); String datePalindrome = new StringBuilder().append(yearStr).reverse().append(yearStr).toString(); try { return LocalDate.parse(datePalindrome, formatter); } catch (DateTimeParseException e) { return null; } } private static boolean isValidDate(final LocalDate date, LocalDate startDate, LocalDate endDate) { if (date == null) return false; if (startDate.isEqual(date) || endDate.isEqual(date)) return true; return startDate.isBefore(date) && endDate.isAfter(date) || endDate.isBefore(date) && startDate.isAfter(date); } }
- package com.mystuff.juststuff;
- import java.time.LocalDate;
- import java.time.format.DateTimeFormatter;
- import java.time.format.DateTimeParseException;
- import java.util.Set;
import java.util.SortedSet;- import java.util.TreeSet;
- import java.util.stream.Collectors;
import java.util.stream.Stream;import java.time.LocalDate;import java.time.format.DateTimeFormatter;import java.time.temporal.ChronoUnit;- import java.util.stream.IntStream;
- public class Palindrome {
private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMddyyyy");- private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMddyyyy");
public Set<LocalDate> countDatePalindromes(final LocalDate startDate, final LocalDate endDate) {final SortedSet<LocalDate> sortedDates = Stream.of(startDate, endDate).collect(Collectors.toCollection(TreeSet::new));return Stream.iterate(sortedDates.first(), date -> date.plusDays(1)).limit(ChronoUnit.DAYS.between(sortedDates.first(), sortedDates.last().plusDays(1))).filter(Palindrome::isPalindrome).collect(Collectors.toCollection(TreeSet::new));}private static boolean isPalindrome(final LocalDate date) {final String fwd = date.format(formatter);return fwd.equals(new StringBuilder(fwd).reverse().toString());}- public Set<LocalDate> countDatePalindromes(final LocalDate startDate, final LocalDate endDate) {
- return IntStream.range(startDate.getYear(), endDate.getYear() + 1).mapToObj(Palindrome::createPalindrome)
- .filter(date -> isValidDate(date, startDate, endDate)).collect(Collectors.toCollection(TreeSet::new));
- }
- private static LocalDate createPalindrome(int year) {
- String yearStr = String.valueOf(year);
- String datePalindrome = new StringBuilder().append(yearStr).reverse().append(yearStr).toString();
- try {
- return LocalDate.parse(datePalindrome, formatter);
- } catch (DateTimeParseException e) {
- return null;
- }
- }
- private static boolean isValidDate(final LocalDate date, LocalDate startDate, LocalDate endDate) {
- if (date == null)
- return false;
- if (startDate.isEqual(date) || endDate.isEqual(date))
- return true;
- return startDate.isBefore(date) && endDate.isAfter(date) ||
- endDate.isBefore(date) && startDate.isAfter(date);
- }
- }
package com.mystuff.juststuff; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import java.time.LocalDate; import java.util.Set; import org.junit.Test; public class PalindromeTest { @Test public void testCountDatePalindromesEndDateBeforeStartDate() { final LocalDate startDate = LocalDate.of(2001, 12, 1); final LocalDate endDate = LocalDate.of(2001, 1, 31); final Set<LocalDate> palSet = new Palindrome().countDatePalindromes(startDate, endDate); assertTrue("Set size should be greater than zero", palSet.size() > 0); } @Test public void testCountDatePalindromesStartDateBeforeEndDate() { final LocalDate startDate = LocalDate.of(2001, 1, 1); final LocalDate endDate = LocalDate.of(2001, 12, 31); final Set<LocalDate> palSet = new Palindrome().countDatePalindromes(startDate, endDate); assertTrue("Set size should be greater than zero", palSet.size() > 0); } @Test public void testCountDatePalindromesStartDateEqualsEndDateNoPalindromes() { final LocalDate startDate = LocalDate.of(2001, 12, 31); // Dec 31, 2001 is not a palindrome date final LocalDate endDate = LocalDate.of(2001, 12, 31); final Set<LocalDate> palSet = new Palindrome().countDatePalindromes(startDate, endDate); assertEquals("Set size should be zero", 0, palSet.size()); } @Test public void testCountDatePalindromesStartDateEqualsEndDateOnePalindrome() { final LocalDate startDate = LocalDate.of(2001, 10, 2); // Oct 2, 2001 is a palindrome date final LocalDate endDate = LocalDate.of(2001, 10, 2); final Set<LocalDate> palSet = new Palindrome().countDatePalindromes(startDate, endDate); assertEquals("Set size should be one", 1, palSet.size()); } @Test public void testCountDatePalindromesHugeDateRange() { final LocalDate startDate = LocalDate.of(1900, 1, 1); final LocalDate endDate = LocalDate.of(2001, 12, 31); final Set<LocalDate> palSet = new Palindrome().countDatePalindromes(startDate, endDate); assertEquals("There is 1 palindrom date between 1/1/1900 12/31/2001", 1, palSet.size()); } @Test public void testCountDatePalindromesMultipleValues() { final LocalDate startDate = LocalDate.of(1900, 1, 1); final LocalDate endDate = LocalDate.of(2010, 12, 31); final Set<LocalDate> palSet = new Palindrome().countDatePalindromes(startDate, endDate); assertEquals("There are 2 palindrom date between 1/1/1900 12/31/2010", 2, palSet.size()); } }
- package com.mystuff.juststuff;
- import static org.junit.Assert.assertEquals;
- import static org.junit.Assert.assertTrue;
import java.util.Set;- import java.time.LocalDate;
- import java.util.Set;
- import org.junit.Test;
- public class PalindromeTest {
- @Test
- public void testCountDatePalindromesEndDateBeforeStartDate() {
- final LocalDate startDate = LocalDate.of(2001, 12, 1);
- final LocalDate endDate = LocalDate.of(2001, 1, 31);
- final Set<LocalDate> palSet = new Palindrome().countDatePalindromes(startDate, endDate);
- assertTrue("Set size should be greater than zero", palSet.size() > 0);
- }
- @Test
- public void testCountDatePalindromesStartDateBeforeEndDate() {
- final LocalDate startDate = LocalDate.of(2001, 1, 1);
- final LocalDate endDate = LocalDate.of(2001, 12, 31);
- final Set<LocalDate> palSet = new Palindrome().countDatePalindromes(startDate, endDate);
- assertTrue("Set size should be greater than zero", palSet.size() > 0);
- }
- @Test
- public void testCountDatePalindromesStartDateEqualsEndDateNoPalindromes() {
- final LocalDate startDate = LocalDate.of(2001, 12, 31); // Dec 31, 2001 is not a palindrome date
- final LocalDate endDate = LocalDate.of(2001, 12, 31);
- final Set<LocalDate> palSet = new Palindrome().countDatePalindromes(startDate, endDate);
assertTrue("Set size should be zero", palSet.size() == 0);- assertEquals("Set size should be zero", 0, palSet.size());
- }
- @Test
- public void testCountDatePalindromesStartDateEqualsEndDateOnePalindrome() {
- final LocalDate startDate = LocalDate.of(2001, 10, 2); // Oct 2, 2001 is a palindrome date
- final LocalDate endDate = LocalDate.of(2001, 10, 2);
- final Set<LocalDate> palSet = new Palindrome().countDatePalindromes(startDate, endDate);
assertTrue("Set size should be one", palSet.size() == 1);- assertEquals("Set size should be one", 1, palSet.size());
- }
- @Test
- public void testCountDatePalindromesHugeDateRange() {
- final LocalDate startDate = LocalDate.of(1900, 1, 1);
- final LocalDate endDate = LocalDate.of(2001, 12, 31);
- final Set<LocalDate> palSet = new Palindrome().countDatePalindromes(startDate, endDate);
assertTrue("Set size should be greater than zero", palSet.size() > 0);- assertEquals("There is 1 palindrom date between 1/1/1900 12/31/2001", 1, palSet.size());
- }
- @Test
- public void testCountDatePalindromesMultipleValues() {
- final LocalDate startDate = LocalDate.of(1900, 1, 1);
- final LocalDate endDate = LocalDate.of(2010, 12, 31);
- final Set<LocalDate> palSet = new Palindrome().countDatePalindromes(startDate, endDate);
- assertEquals("There are 2 palindrom date between 1/1/1900 12/31/2010", 2, palSet.size());
- }
- }