From 642db25a441b301b4e507796c55bfef8a4170185 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Mon, 25 Jul 2022 20:21:43 +0100 Subject: - Added solutions by Robert DiCicco. --- challenge-175/robert-dicicco/perl/ch-1.pl | 63 +++++++++++++++++++++++++++++ challenge-175/robert-dicicco/raku/ch-1.raku | 39 ++++++++++++++++++ challenge-175/robert-dicicco/ruby/ch-1.rb | 35 ++++++++++++++++ 3 files changed, 137 insertions(+) create mode 100644 challenge-175/robert-dicicco/perl/ch-1.pl create mode 100644 challenge-175/robert-dicicco/raku/ch-1.raku create mode 100644 challenge-175/robert-dicicco/ruby/ch-1.rb (limited to 'challenge-175') diff --git a/challenge-175/robert-dicicco/perl/ch-1.pl b/challenge-175/robert-dicicco/perl/ch-1.pl new file mode 100644 index 0000000000..c50b107754 --- /dev/null +++ b/challenge-175/robert-dicicco/perl/ch-1.pl @@ -0,0 +1,63 @@ +#!perl.exe + +use strict; +use warnings; +use DateTime; + +# AUTHOR: Robert DiCicco +# DATE: 2022-07-25 +# Challenge 175 Last Sunday ( Perl ) + +my @molen = qw( 31 28 31 30 31 30 31 31 30 31 30 31); + +sub CreateDateTime { # create a Date object for year and month + my $y = shift; + my $m = shift; + + my $dt = DateTime->last_day_of_month(year => $y, month => $m); + + return $dt; +} + +sub CheckLeapYear { # Adjust February if leap year + my $dt = shift; + + if($dt->is_leap_year){ + + $molen[1] = 29; + + } +} + +sub GetLastSunday { # back up until we land on Sunday + my $dt = shift; + + my $dow = $dt->day_of_week; + + if($dow != 7 ){ + + $dt->subtract(days=>$dow); + + } + + return $dt; + +} + +my $count = 1; + +my $year = $ARGV[0]; + +while( $count <= 12){ + + my $dt = CreateDateTime($year, $count); + + CheckLeapYear($dt); + + $dt = GetLastSunday($dt); + + print($dt->ymd . "\n"); + + $count++; + +} diff --git a/challenge-175/robert-dicicco/raku/ch-1.raku b/challenge-175/robert-dicicco/raku/ch-1.raku new file mode 100644 index 0000000000..15454479c7 --- /dev/null +++ b/challenge-175/robert-dicicco/raku/ch-1.raku @@ -0,0 +1,39 @@ +use v6; + +# AUTHOR: Robert DiCicco +# DATE: 2022-07-25 +# Challenge 175 Last Sunday ( Raku ) + +sub CreateDateTime ( $y, $m ) { # create a Date object for year and month + my $dt = Date.new($y, $m, 1 ); + + return $dt; +} + +sub GetLastSunday($dt) { + my $lastdt = $dt.last-date-in-month; # Get last date and its dow + + my $dow = ($lastdt).day-of-week; + + if $dow == 7 { # if not 7, back up until day = 0 (7) + + print("$lastdt\n"); + + } else { + + say Date.new($lastdt).earlier( ( year => 0, month => 0, day => $dow ) ); + + } + +} + +sub MAIN( Int $year = 2022) { # In no year entered, default to 2022 + + for (1..12) -> $m { # for each month + + my $dt = CreateDateTime($year, $m); + + GetLastSunday($dt); + + } +} diff --git a/challenge-175/robert-dicicco/ruby/ch-1.rb b/challenge-175/robert-dicicco/ruby/ch-1.rb new file mode 100644 index 0000000000..152cb1d287 --- /dev/null +++ b/challenge-175/robert-dicicco/ruby/ch-1.rb @@ -0,0 +1,35 @@ +#ruby.exe + +require 'date' + +# AUTHOR: Robert DiCiccco +# DATE: 2022-07-25 +# Challenge 175 Last Sunday ( Ruby ) + +def CreateDateObject( y, m) + + td = Date.new(y,m,1).next_month.prev_day + + return td + +end + +year = ARGV[0].to_i + +for m in 1..12 do + + td = CreateDateObject(year,m) + + wd = td.wday + + if wd != 7 + + puts (td - wd).to_s + + else + + puts td + + end + +end -- cgit