Forecast Palindromic Dates
MATL, 24 23 bytes
YOZ}&:"@23XOtt47>)tP=?8M
Accepts input in the form of an array of string {lower, upper}
where the date format is 'MM/DD/YYYY'
. Output is in the format MM/DD/YYYY
as well.
Try it Online
Explanation
% Implicitly grab the two inputs
YO % Convert to serial date format
Z} % Push them onto the stack separately
&: % Create an array from [lower...upper] incrementing 1 day
" % For each day
@23XO % Get the string version of the date (mm/dd/yyyy)
tt % Duplicate twice
47>) % Get the numeric parts
tP= % Compare numeric part with the flipped version of the numeric part
?8M % If they are the same push it to the stack
% Implicitly display stack contents
Python 2, 197 bytes
One byte saved thanks to @cat!
from datetime import*
def g(a,b):
for s in"ab":exec"%s=date(*[int(x)for x in %s.split('-')])"%(s,s)
for d in range((b-a).days+1):
x=str(a+timedelta(d));y=x.replace("-","")
if y==y[::-1]:print x
Try it here!
Input and output format is YYYY-MM-DD
. First intendation level is spaces, second one is tabs.
Nothing too special going on here. Uses some exec
abuse to convert the input to date
objects by splitting the date string on -
and splatting the list into the date
constructor. Then we just iterate over all dates in their inclusive range and print the ones which are palindromic.
Bash + GNU utilities, 116 84
Requires 64-bit version of date for the given testcase.
set date -uf- +%
jot -w@ - `$@s` 86400|$@F|sed -r 'h
:
s/-|^(.)(.*)\1$/\2/
t
/./d
g'
I/O is in YYYY-MM-DD
format. Input is taken from two lines of stdin, e.g.
printf "%s\n" 2050-05-02 2060-12-12 | ./palindate.sh
Explanation
set
saves the date command template so it may be accessed using the$@
parameterdate -uf- +%s
converts endpoint dates to number of seconds since the Unix epochjot
interpolates this to give a list of seconds-from-the-epoch, one per day, each prefixed with@
date -uf- +%F
formats each list entry asYYYY-MM-DD
sed
checks for palindromes:h
save the input line to the hold buffer:
define "unnamed" labels/-|^(.)(.*)\1$/\2/
if a dash is found, remove it or if the first and last characters match, remove themt
if there was a match above, jump back to the unnamed label/./d
if there are any characters left over, the line is not a palindrome - delete it and continue to the next lineg
if we got here, then no line deletion happened, thus the line must have been a palindrome. Get the line back from the hold buffer and implicitly display it.