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;
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|