Scripting Challenge Beginner 5 - working with dates.
am 28.02.2008 19:59:01 von bdaoustThis is a multi-part message in MIME format.
--===============1558578940==
Content-type: multipart/alternative;
boundary=------------080908000005060002060902
This is a multi-part message in MIME format.
--------------080908000005060002060902
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
%$#@!
Beginner Challenge 5 - at the contest!
my script b5.pl
solution script sb5.pl
D:\My Documents\perl scripts\Challenge>b5.pl February 21, 2009
Days: 367
Months: 12
Months/Days: 12 / 1
D:\My Documents\perl scripts\Challenge>sb5.pl February 21, 2009
Date::Calc::Delta_Days(): not a valid date at D:\My Documents\perl
scripts\Challenge\sb5.pl line 8.
D:\My Documents\perl scripts\Challenge>sb5.pl 2/21/2009
Days: 367
Months: 12
Months/Days: 12 / 1
D:\My Documents\perl scripts\Challenge>
Quote from the contest!"English (United States) regional and language
option settings, and we will always enter dates using a string value
similar to this: March 3, 2008"
But the solution fails if you enter a string date, like March 3, 2008.
Its likely my solution is wrong especially if today is like the 31st
cause I had to fudge not passing the target day / when I hit the first
before I hit the day of month. But the solution provided fails long
before that and we have no test data so I can not see a successful
solution and where I broke down. I would love to see the test data they
used! I understand they can't give back everyone feedback.
Sorry about my venting. Would anyone care to comment on my failed script?
I could not find the rules, so I have been using just the default
ActivePerl install, and all scripts so far have been doable, even the
read from a database, which I'm not to happy with the documentation that
keeps saying you must use the DATASOURCES ( ODBC) panel to associate
the database with the driver. This is totally bad, I did find the info
I wanted so I could create the DSN in the script thank GOODNESS. OOPS,
get of my soap box.
Sorry about the lack of comments. s variables are the starting date, ie
today that I advance to equal the e variables "entered date".
#!/perl
use warnings;
use strict;
use ActiveState::DateTime qw(is_leap_year days_in_month check_date
month_name_short month_name_long gmt_offset);
my @months = qw(January February March April May June July August
September October November December);
my ($ss,$sm,$sh,$smday,$smon,$syear,$swday,$syday,$sisdst) =
localtime(time);
$syear+=1900;
my $em = shift or badinput();
badinput() unless join(" ",@months) =~ /$em/i;
my $ed = shift or badinput();
$ed =~ tr/,//d;
my $ey = shift or badinput();
my $emc=0;
foreach ( @months ){
$emc++;
last if $months[$emc] eq $em;
}
my ($mm,$md,$cd,$cm);
$mm=$md=$cd=$cm=0;
my $loop=1;
my $max_days = days_in_month($syear, $smon+1);
my $mdtrigger=$smday;
while($emc!=$smon || $ed!=$smday || $ey!=$syear){
if ($smday+1 > $max_days) {
if ( $smon == 11 ) {
$syear++;
$smon=0;
} else {
$smon++;
}
$max_days = days_in_month($syear, $smon+1);
$smday=0;
$cm++;
}
$cd++;
$smday++;
if ($smday == $mdtrigger || $mdtrigger > days_in_month($syear,
$smon==0?12:$smon) && $smday == 1){
$mm++;
$md=-1;
}
$md++;
}
print "Days: $cd\n";
print "Months: $cm\n";
print "Months/Days: $mm / $md\n";
sub badinput{
die "Expected a date of the format $months[$smon] $smday, $syear, to
be entered on the command line.\n";
}
--------------080908000005060002060902
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit
%$#@!
Beginner Challenge 5 - at the contest!
my script b5.pl
solution script sb5.pl
D:\My Documents\perl scripts\Challenge>b5.pl February 21, 2009
Days: 367
Months: 12
Months/Days: 12 / 1
D:\My Documents\perl scripts\Challenge>sb5.pl February 21, 2009
Date::Calc::Delta_Days(): not a valid date at D:\My Documents\perl
scripts\Challenge\sb5.pl line 8.
D:\My Documents\perl scripts\Challenge>sb5.pl 2/21/2009
Days: 367
Months: 12
Months/Days: 12 / 1
D:\My Documents\perl scripts\Challenge>
Quote from the contest!"English (United States)
regional and language option settings, and we will always enter dates
using a string value similar to this: March 3, 2008"
But the solution fails if you enter a string date, like March 3, 2008.
Its likely my solution is wrong especially if today is like the 31st
cause I had to fudge not passing the target day / when I hit the first
before I hit the day of month. But the solution provided fails long
before that and we have no test data so I can not see a successful
solution and where I broke down. I would love to see the test data
they used! I understand they can't give back everyone feedback.
Sorry about my venting. Would anyone care to comment on my failed
script?
I could not find the rules, so I have been using just the default
ActivePerl install, and all scripts so far have been doable, even the
read from a database, which I'm not to happy with the documentation
that keeps saying you must use the DATASOURCES ( ODBC) panel to
associate the database with the driver. This is totally bad, I did
find the info I wanted so I could create the DSN in the script thank
GOODNESS. OOPS, get of my soap box.
Sorry about the lack of comments. s variables are the starting date,
ie today that I advance to equal the e variables "entered date".
#!/perl
use warnings;
use strict;
use ActiveState::DateTime qw(is_leap_year days_in_month check_date
month_name_short month_name_long gmt_offset);
my @months = qw(January February March April May June July August
September October November December);
my ($ss,$sm,$sh,$smday,$smon,$syear,$swday,$syday,$sisdst) =
localtime(time);
$syear+=1900;
my $em = shift or badinput();
badinput() unless join(" ",@months) =~ /$em/i;
my $ed = shift or badinput();
$ed =~ tr/,//d;
my $ey = shift or badinput();
my $emc=0;
foreach ( @months ){
$emc++;
last if $months[$emc] eq $em;
}
my ($mm,$md,$cd,$cm);
$mm=$md=$cd=$cm=0;
my $loop=1;
my $max_days = days_in_month($syear, $smon+1);
my $mdtrigger=$smday;
while($emc!=$smon || $ed!=$smday || $ey!=$syear){
if ($smday+1 > $max_days) {
if ( $smon == 11 ) {
$syear++;
$smon=0;
} else {
$smon++;
}
$max_days = days_in_month($syear, $smon+1);
$smday=0;
$cm++;
}
$cd++;
$smday++;
if ($smday == $mdtrigger || $mdtrigger > days_in_month($syear,
$smon==0?12:$smon) && $smday == 1){
$mm++;
$md=-1;
}
$md++;
}
print "Days: $cd\n";
print "Months: $cm\n";
print "Months/Days: $mm / $md\n";
sub badinput{
die "Expected a date of the format $months[$smon] $smday, $syear, to
be entered on the command line.\n";
}
--------------080908000005060002060902--
--===============1558578940==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============1558578940==--