Title: Find palindrome dates in Python
A palindrome date is a date that, in numeric format, reads the same forward and backward. For example, 12/1/21.
To find the most dates, you can consider all formats including two-digit days and four-digit years, and you can ignore the separator characters. For example, 5/20/2025 is a palindrome because 5202025 is the same forward and backward. (May 2025 has a whopping 12 palindrome dates in the m/d/y format!)
Obviously this depends on whether you use the m/d/y, d/m/y, or y/m/d format. It also depends on whether you use one- or two-digit month and day numbers, and whether you use two- or four-digit years so all of these combinations are possible in m/d/y format: 05/05/25, 5/05/25, 05/5/25, 05/05/2025, 5/05/2025, and 05/5/2025. For this program, I'm going to assume that the month and day values either both use one-digit values or both use two-digit values.
When you click the Go button, the program uses the following code to search for palindrome dates.
def go_click(self):
# Get the selected date format.
# Unix version (untested):
match self.format_var.get():
case 0:
# m/d/y formats.
formats = [
'%m/%d/%y',
'%-m/%-d/%y',
'%m/%d/%Y',
'%-m/%-d/%Y',
]
case 1:
# d/m/y formats.
formats = [
'%d/%m/%y',
'%-d/%-m/%y',
'%d/%m/%Y',
'%-d/%-m/%Y',
]
case 2:
# y/m/d formats.
formats = [
'%y/%d/%m',
'%y/%-d/%-m',
'%Y/%d/%m',
'%Y/%-d/%-m',
]
# Windows version:
match self.format_var.get():
case 0:
# m/d/y formats.
formats = [
'%m/%d/%y',
'%#m/%#d/%y',
'%m/%d/%Y',
'%#m/%#d/%Y',
]
case 1:
# d/m/y formats.
formats = [
'%d/%m/%y',
'%#d/%#m/%y',
'%d/%m/%Y',
'%#d/%#m/%Y',
]
case 2:
# y/m/d formats.
formats = [
'%y/%d/%m',
'%y/%#d/%#m',
'%Y/%d/%m',
'%Y/%#d/%#m',
]
# Start with an empty list or results.
palindromes = []
# Build the list of dates.
self.results_treeview.delete(*self.results_treeview.get_children())
date = datetime.strptime(self.start_date_var.get(), '%Y-%m-%d')
end_date = datetime.strptime(self.end_date_var.get(), '%Y-%m-%d')
while date <= end_date:
for format in formats:
date_string = date.strftime(format)
without_slashes = date_string.replace('/', '')
# Skip it if is already in the list.
if without_slashes in palindromes: continue
# See if it is a palindrome.
is_palindrome = without_slashes == without_slashes[::-1]
if is_palindrome:
print(without_slashes)
self.results_treeview.insert('', tk.END,
values=(f'{without_slashes}', f'({date_string})'))
date += dt.timedelta(days=1)
This code first creates a formats list holding the formats that it will check. Unfortunately the formats differ depending on your operating system. I've checked the Windows formats but don't know if the values shown here work in Unix or macOS.
Next, the program loops through the dates that you entered. For each date, it loops through the formats. It uses the format to convert the date into a string, removes the slashes from the string, and then uses string slicing to determine whether the result is a palindrome. If the date is a palindrome, the program adds the date with and without slashes to the results_treeview widget.
Download the example to see additional details.
|