How Many More Palindrome Dates Will You See?

I'm currently on paternity leave and in search of quick but interesting programming challenges to stay fresh. The latest Riddler Express provided a quick refresher on string manipulation.

If we write out dates in the American format of MM/DD/YYYY (i.e., the two digits of the month, followed by the two digits of the day, followed by the four digits of the year), how many more palindromic dates will there be this century?

The trick is to create a sequence of all remaining dates in the century, store them as character strings in MM/DD/YYYY format, then reverse each string. If the string is equal to its reverse, it's a palindrome.

library(tidyverse)
## ── Attaching packages ─────────────────────────────────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.2.1     ✓ purrr   0.3.3
## ✓ tibble  2.1.3     ✓ dplyr   0.8.3
## ✓ tidyr   1.0.0     ✓ stringr 1.4.0
## ✓ readr   1.3.1     ✓ forcats 0.4.0
## ── Conflicts ────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
## 
##     date
library(knitr)

palindrome_usa <- tibble(
  # generate sequence of dates
  date = seq(from = mdy("02/03/2020"), to = mdy("12/31/2100"), by = "1 day"),
  # convert to character string
  date_char = date %>%
    format('%m-%d-%Y') %>%
    as.character() %>%
    # remove hyphens
    str_remove_all(pattern = "-"),
  # reverse the string and compare to original
  date_rev = stringi::stri_reverse(date_char),
  palindrome = date_char == date_rev
) %>%
  # which dates are palindromes
  filter(palindrome)
kable(palindrome_usa)
datedate_chardate_revpalindrome
2021-12-021202202112022021TRUE
2030-03-020302203003022030TRUE
2040-04-020402204004022040TRUE
2050-05-020502205005022050TRUE
2060-06-020602206006022060TRUE
2070-07-020702207007022070TRUE
2080-08-020802208008022080TRUE
2090-09-020902209009022090TRUE

Only 8 palindrome dates remaining. Out of curiosity, do any exist using the European DD/MM/YYYY format?

palindrome_eur <- tibble(
  # generate sequence of dates
  date = seq(from = mdy("02/03/2020"), to = mdy("12/31/2100"), by = "1 day"),
  # convert to character string
  date_char = date %>%
    format('%d-%m-%Y') %>%
    as.character() %>%
    # remove hyphens
    str_remove_all(pattern = "-"),
  # reverse the string and compare to original
  date_rev = stringi::stri_reverse(date_char),
  palindrome = date_char == date_rev
) %>%
  # which dates are palindromes
  filter(palindrome)
kable(palindrome_eur)
datedate_chardate_revpalindrome
2021-02-121202202112022021TRUE
2022-02-222202202222022022TRUE
2030-02-030302203003022030TRUE
2031-02-131302203113022031TRUE
2032-02-232302203223022032TRUE
2040-02-040402204004022040TRUE
2041-02-141402204114022041TRUE
2042-02-242402204224022042TRUE
2050-02-050502205005022050TRUE
2051-02-151502205115022051TRUE
2052-02-252502205225022052TRUE
2060-02-060602206006022060TRUE
2061-02-161602206116022061TRUE
2062-02-262602206226022062TRUE
2070-02-070702207007022070TRUE
2071-02-171702207117022071TRUE
2072-02-272702207227022072TRUE
2080-02-080802208008022080TRUE
2081-02-181802208118022081TRUE
2082-02-282802208228022082TRUE
2090-02-090902209009022090TRUE
2091-02-191902209119022091TRUE
2092-02-292902209229022092TRUE

A lot more thanks to the reversed positioning of the month and date.

Is there any overlap between the two? That is, like 02/02/2020 and 02/02/2020, are there any palindromic dates under both formats?

semi_join(palindrome_usa, palindrome_eur, by = "date")
## # A tibble: 0 x 4
## # … with 4 variables: date <date>, date_char <chr>, date_rev <chr>,
## #   palindrome <lgl>

Nope.

Avatar
Benjamin Soltoff
Assistant Instructional Professor of Computational Social Science

Husband, father, political scientist computational social scientist, teacher, data nerd.

Related