With the recent passing of a 'symmetrical date' (20:02 20/02 2002) and my inability to find any related posts on here, I present the following code from a co-member of a local linux users' group to produce a list of all symmetrical dates of the form HH:MM DD/MM YYYY
By symmetrical I mean either of the following examples:
- overall symmetry: 16:12 30/03 2161
- component and overall symmetry: 20:02 20:02 2002
We are in Australia, so we put dates in the form DD/MM .. assuming we can start with (00:00 10/01 0000) and end with (19:59 21/12 9591) - the code below produces 8640 such dates.
I'm simply curious to see the results of a few rounds of golf on this problem.
#!/usr/bin/perl -w
my %symmetrical = ();
my $time;
my $year;
my @months = ('1001', '2002', '3003', '0110', '1111', '2112');
foreach my $i (0 .. 2359)
{
$i = &fill($i);
$i =~ m/(\d\d)(\d\d)/;
if (($1 > 23) or ($2 > 59)) { next; }
$time = $i;
$year = reverse($i);
$symmetrical{$year} = $time;
}
for my $i (sort(keys %symmetrical))
{
my $ryear = $i;
my $rtime = $symmetrical{$i};
$rtime =~ s/(\d\d)(\d\d)/$1:$2/;
for my $rmonth (@months)
{
$rmonth =~ s/(\d\d)(\d\d)/$1\/$2/;
print "$rtime\t$rmonth\t$ryear\n";
}
}
sub fill
{
my $i = shift;
while (length($i) < 4)
{
$i = '0' . $i;
}
return $i;
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.