diff options
| author | saiftynet <saiftynet@gmail.com> | 2019-12-04 21:01:32 +0000 |
|---|---|---|
| committer | saiftynet <saiftynet@gmail.com> | 2019-12-04 21:01:32 +0000 |
| commit | 03f61bbf33a8360b23882639007f2fa59ccae278 (patch) | |
| tree | 5d5684bf1ae260e121621cbd36eb60ea454380af /challenge-037 | |
| parent | 56e0be60844231b9160dcf264b56a21cc65ed8d2 (diff) | |
| download | perlweeklychallenge-club-03f61bbf33a8360b23882639007f2fa59ccae278.tar.gz perlweeklychallenge-club-03f61bbf33a8360b23882639007f2fa59ccae278.tar.bz2 perlweeklychallenge-club-03f61bbf33a8360b23882639007f2fa59ccae278.zip | |
Answers uploaded for ch037
Diffstat (limited to 'challenge-037')
| -rw-r--r-- | challenge-037/saiftynet/perl5/ch-1.pl | 44 | ||||
| -rw-r--r-- | challenge-037/saiftynet/perl5/ch-2.pl | 78 |
2 files changed, 122 insertions, 0 deletions
diff --git a/challenge-037/saiftynet/perl5/ch-1.pl b/challenge-037/saiftynet/perl5/ch-1.pl new file mode 100644 index 0000000000..b8fe9cf778 --- /dev/null +++ b/challenge-037/saiftynet/perl5/ch-1.pl @@ -0,0 +1,44 @@ +#!/usr/env perl +# weekdays in month +# Challenge 1 week 37 + +use strict; +use warnings; +use DateTime; + +my $dt = DateTime->new( # start at first day of the year + year => 2019, + month => 1, + day => 1,); + +my %counters; # set up weekday counters + +while ($dt->year() ==2019){ # Until the year is over + $counters{$dt->month()}+=1 # add to each month's counter + if ($dt->day_of_week() <6); # if the day is a week day. + $dt->add( days => 1 ) # Get next day +} + +print "Answer to 1st Challenge of week 37\nIn 2019\n"; +for (1..12){ # now print out each month + print qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/[$_-1]. + " has " .$counters{$_} . " Weekdays \n"; # and its weeday counts +} + +#Output +#Answer to 1st Challenge of week 37 +#In 2019 +#Jan has 23 Weekdays +#Feb has 20 Weekdays +#Mar has 21 Weekdays +#Apr has 22 Weekdays +#May has 23 Weekdays +#Jun has 20 Weekdays +#Jul has 23 Weekdays +#Aug has 22 Weekdays +#Sep has 21 Weekdays +#Oct has 23 Weekdays +#Nov has 21 Weekdays +#Dec has 22 Weekdays + + diff --git a/challenge-037/saiftynet/perl5/ch-2.pl b/challenge-037/saiftynet/perl5/ch-2.pl new file mode 100644 index 0000000000..7c328695d0 --- /dev/null +++ b/challenge-037/saiftynet/perl5/ch-2.pl @@ -0,0 +1,78 @@ +#!/usr/env perl +# Daylight Hours +# Challenge 2 Week 37 + +use strict; +use warnings; + +use LWP::Simple qw(get); + +my $difference=getDayLight( country=>"uk", city =>"london", month =>"11", year =>"2019", ) - + getDayLight( country=>"uk", city =>"london", month =>"12", year =>"2019", ); + +print "\n\nThe difference in daylight is ".s2hms($difference)."(Hours:Minutes:Seconds)\n"; + +sub s2hms{ # converts seconds to days hours minutes and seconds + my $time=shift; + my $sec=$time%60; + $time=($time-$sec)/60; + my $min=$time%60; + $time=($time-$min)/60; + my $hrs=$time%24; + my $days=($time-$hrs)/24; + return ($days?"$days days ":"").sprintf "%d:%02d:%02d",$hrs,$min,$sec; + +} + +sub hms2s{ + my($hours,$mins,$secs)=split /:/, shift; + return 3600*$hours+60*$mins+$secs; + +} + +sub getDayLight{ #return total number of seconds of daylight; + my %data=@_; + my $url = "https://www.timeanddate.com/sun/$data{country}/$data{city}?month=$data{month}&year=$data{year}"; + print "According to \n $url \n"; + my $html = get $url; + + $html=~s/^.+<tbody>//s; #remove everything before the table body + $html=~s/<\/tbody>.*$//s; #remove everything after the table body + $html=~s/<\/tr>/-\n/gs; #each row of table in new line, remove row tags + $html=~s/<\/?t[^>]+>/_/gs; #replace each tag with '_' + $html=~s/<\/?s[^>]+>//gs; #span tags are not useful + $html=~s/−/\-/gs; #html minus charcode to be replaced with - + $html=~s/&43;/\+/gs; #html plus charcode to be replaced with + + + my @tt; #array containing each day of the month + foreach my $line(split/\n/, $html){ #converts row into hash + my @tmp=split /__/,$line;shift @tmp; my $dt=shift @tmp; my %ttmp; + @ttmp{qw/sr ss dl dld ATS ATE NTS NTE CTS CTE noon MKm/}=@tmp; + $tt[$dt]=\%ttmp; #put each row hash into the date + } + + my $dlSum=0; # the accumulator for the daylight time + + foreach my $date (1..$#tt){ #for each date collected + $dlSum+=hms2s($tt[$date]{dl}); #get the daylight time converted to seconds + } + #spit out the time + print " There are ".s2hms($dlSum) . " of daylight in\n ". + ucfirst($data{city})." during month $data{month}, year $data{year}\n"; + return $dlSum; +} + +# output is: - +# +# According to +# https://www.timeanddate.com/sun/uk/london?month=11&year=2019 +# There are 11 days 3:00:40 of daylight in +# london during month 11, year 2019 +# According to +# https://www.timeanddate.com/sun/uk/london?month=12&year=2019 +# There are 10 days 5:45:01 of daylight in +# london during month 12, year 2019 +# +# +# The difference in daylight is 21:15:39(Hours:Minutes:Seconds) + |
