How to resolve the algorithm Palindrome dates step by step in the AWK programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Palindrome dates step by step in the AWK programming language
Table of Contents
Problem Statement
Today (2020-02-02, at the time of this writing) happens to be a palindrome, without the hyphens, not only for those countries which express their dates in the yyyy-mm-dd format but, unusually, also for countries which use the dd-mm-yyyy format.
Write a program which calculates and shows the next 15 palindromic dates for those countries which express their dates in the yyyy-mm-dd format.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Palindrome dates step by step in the AWK programming language
Source code in the awk programming language
# syntax: GAWK -f PALINDROME_DATES.AWK
BEGIN {
show = 15
year_b = 2020
year_e = 9999
split("31,28,31,30,31,30,31,31,30,31,30,31",daynum_array,",") # days per month in non leap year
for (y=year_b; y<=year_e; y++) {
daynum_array[2] = (y % 400 == 0 || (y % 4 == 0 && y % 100)) ? 29 : 28
for (m=1; m<=12; m++) {
for (d=1; d<=daynum_array[m]; d++) {
ymd = sprintf("%04d%02d%02d",y,m,d)
if (substr(ymd,1,1) == substr(ymd,8,1)) { # speed up
if (ymd == reverse(ymd)) {
arr[++n] = ymd
}
}
}
}
}
printf("%04d0101-%04d1231=%d years, %d palindromes, showing first and last %d\n",year_b,year_e,year_e-year_b+1,n,show)
printf("YYYYMMDD YYYYMMDD\n")
for (i=1; i<=show; i++) {
printf("%s %s\n",arr[i],arr[n-show+i])
}
exit(0)
}
function reverse(str, i,rts) {
for (i=length(str); i>=1; i--) {
rts = rts substr(str,i,1)
}
return(rts)
}
You may also check:How to resolve the algorithm Program termination step by step in the GW-BASIC programming language
You may also check:How to resolve the algorithm String comparison step by step in the WDTE programming language
You may also check:How to resolve the algorithm Terminal control/Ringing the terminal bell step by step in the D programming language
You may also check:How to resolve the algorithm Loops/For with a specified step step by step in the TUSCRIPT programming language
You may also check:How to resolve the algorithm Roots of a quadratic function step by step in the MATLAB / Octave programming language