From d459492fdd0525806d026f85bbff80be0e58d76c Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Sat, 22 Feb 2020 10:25:26 +0100 Subject: Challenge 2 Perl --- challenge-048/lubos-kolouch/perl/ch-2.pl | 50 ++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 challenge-048/lubos-kolouch/perl/ch-2.pl (limited to 'challenge-048') diff --git a/challenge-048/lubos-kolouch/perl/ch-2.pl b/challenge-048/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..6e8dd9311a --- /dev/null +++ b/challenge-048/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,50 @@ +#!/usr/bin/perl +#=============================================================================== +# +# FILE: ch-2.pl +# +# USAGE: ./ch-2.pl +# +# DESCRIPTION: https://perlweeklychallenge.org/blog/perl-weekly-challenge-048/ +# +# Palindrome Dates +# +# Write a script to print all Palindrome Dates between 2000 and 2999. The format of date is mmddyyyy. For example, the first one was on October 2, 2001 as it is represented as 10022001. +# +# OPTIONS: --- +# REQUIREMENTS: --- +# BUGS: --- +# NOTES: --- +# AUTHOR: YOUR NAME (), +# ORGANIZATION: +# VERSION: 1.0 +# CREATED: 02/22/2020 10:11:25 AM +# REVISION: --- +#=============================================================================== + +use strict; +use warnings; +use DateTime; +use feature qw/say/; + +sub is_palindrome { + my $dt = shift; + + return 1 if $dt->mdy('') eq reverse $dt->mdy(''); + + return 0; +} + +my $dt_start = DateTime->new( year => 2000, month => 1, day => 1 ); +my $epoch_test = $dt_start->epoch; + +my $dt_end = DateTime->new( year => 2999, month => 12, day => 31 ); +my $epoch_end = $dt_end->epoch; + +while ($epoch_test < $epoch_end) { + my $dt = DateTime->from_epoch( epoch => $epoch_test); + + say $dt->mdy if is_palindrome($dt); + $epoch_test += 60 * 60 * 24; +} + -- cgit