How to resolve the algorithm Palindrome dates step by step in the Perl programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Palindrome dates step by step in the Perl 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 Perl programming language

Source code in the perl programming language

use Time::Piece;
my $d = Time::Piece->strptime("2020-02-02", "%Y-%m-%d");

for (my $k = 1 ; $k <= 15 ; $d += Time::Piece::ONE_DAY) {
    my $s = $d->strftime("%Y%m%d");
    if ($s eq reverse($s) and ++$k) {
        print $d->strftime("%Y-%m-%d\n");
    }
}


use strict;
use warnings;
use feature 'say';
use ntheory qw/forsetproduct/;

my $start = '2020-02-02' =~ s/-//gr;
my($y) = substr($start,0,4);

my(@dates,$cnt);
forsetproduct { push @dates, "@_" } [$y..$y+999],['01'..'12'],['01'..'31'];
for (@dates) {
    (my $date = $_) =~ s/ //g;
    next unless $date > $start and $date eq reverse $date;
    say s/ /-/gr;
    last if 15 == ++$cnt;
}


  

You may also check:How to resolve the algorithm Mian-Chowla sequence step by step in the C++ programming language
You may also check:How to resolve the algorithm Honeycombs step by step in the Liberty BASIC programming language
You may also check:How to resolve the algorithm Loops/Downward for step by step in the GAP programming language
You may also check:How to resolve the algorithm Find the last Sunday of each month step by step in the REXX programming language
You may also check:How to resolve the algorithm Angle difference between two bearings step by step in the Nim programming language