Programming challenge
Paul Seamons
paul at seamons.com
Thu Sep 27 16:00:50 MDT 2007
Here's my two minutes (plus two to clean it up and comment it):
#!/usr/bin/perl
my $a = "1 + 2:30 - 0:45:30.2 - 1:40.243";
$a =~ s{
\b (\d+) # opening hour or minute
: (\d+ (?:\.\d+)?) # minute or second (decimals optional)
(?:
: (\d+ (?:\.\d+)?) # seconds (if present) (decimals optional)
)?
}{
defined($3) # had seconds ?
? $3+60 * ($2+60 * $1)
: $2+60 * $1
}xge;
print "($a)\n";
# prints
(1 + 150 - 2730.2 - 100.243)
That regex will also work in javascript as well.
Various assumptions were made that weren't described in the problem
description.
Paul
More information about the PLUG
mailing list