aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-09-25 20:43:55 +0100
committerGitHub <noreply@github.com>2022-09-25 20:43:55 +0100
commit5b35f8c10c84f0d3d62fec5dc30eda6b460b4134 (patch)
tree6425b9f9d1282e119d4f29f7805480279ba42dbb
parenta74edc9f914bf877b835d4c7c3204d6482a16f12 (diff)
parent762fe268422f73d5f92b8afc7a34756466fd0c89 (diff)
downloadperlweeklychallenge-club-5b35f8c10c84f0d3d62fec5dc30eda6b460b4134.tar.gz
perlweeklychallenge-club-5b35f8c10c84f0d3d62fec5dc30eda6b460b4134.tar.bz2
perlweeklychallenge-club-5b35f8c10c84f0d3d62fec5dc30eda6b460b4134.zip
Merge pull request #6789 from Solathian/branch-for-challenge-183
Solutions for pwc 183
-rw-r--r--challenge-183/solathian/perl/183ch-1.pl67
-rw-r--r--challenge-183/solathian/perl/183ch-2.pl250
2 files changed, 317 insertions, 0 deletions
diff --git a/challenge-183/solathian/perl/183ch-1.pl b/challenge-183/solathian/perl/183ch-1.pl
new file mode 100644
index 0000000000..e49b5328bd
--- /dev/null
+++ b/challenge-183/solathian/perl/183ch-1.pl
@@ -0,0 +1,67 @@
+#!usr/bin/perl -w
+
+# Created for perl weekly challenge - 183 - 1
+
+# You are given list of arrayrefs.
+# Write a script to remove the duplicate arrayrefs from the given list.
+
+use strict;
+use warnings;
+use feature 'say';
+
+no warnings 'experimental::smartmatch';
+
+use constant
+{
+ FALSE => 0,
+ TRUE => 1,
+};
+
+
+#example calls
+# my @list = ([1,2], [3,4], [5,6], [1,2]); # ([1,2], [3,4], [5,6])
+# my @list = ([1,2], [3,4], [5,6], [1,2], [1,2,3], [6,5]);
+# my @list = ([9,1], [3,7], [2,5], [2,5]); # ([9, 1], [3,7], [2,5])
+
+# findDuplicates(\@list);
+
+sub findDuplicates
+{
+ my $arrayRef = shift;
+ my @arrayStorage;
+
+ print "("; # print the starting parenthesis
+
+ foreach my $subArray (@$arrayRef)
+ {
+
+ # check that if current element matches any of the previous ones
+ my $duplicateFlag = FALSE;
+ foreach my $storedElem (@arrayStorage)
+ {
+ if($storedElem ~~ $subArray) # using smartmatch
+ {
+ $duplicateFlag = TRUE;
+ last;
+ }
+
+ }
+
+ # if it is a duplicated element
+ next if($duplicateFlag == TRUE);
+
+ # start printing out the collected elements
+ print(",") if( 0 < @arrayStorage );
+ print("[".join(',',@$subArray)."]");
+
+ # store the element
+ push(@arrayStorage, $subArray);
+
+
+ }
+
+ print(")"); # print the closing parenthesis
+
+ return \@arrayStorage;
+}
+
diff --git a/challenge-183/solathian/perl/183ch-2.pl b/challenge-183/solathian/perl/183ch-2.pl
new file mode 100644
index 0000000000..cbac88953b
--- /dev/null
+++ b/challenge-183/solathian/perl/183ch-2.pl
@@ -0,0 +1,250 @@
+#!usr/bin/perl -w
+
+# Created for perl weekly challenge - 183 - 2
+
+# You are given two dates, $date1 and $date2 in the format YYYY-MM-DD.
+# Write a script to find the difference between the given dates in terms on years and days only.
+
+# The examples are bad since leapdays are not taken into account
+
+use strict;
+use warnings;
+
+use feature 'say';
+use DateTime;
+
+
+
+#example inputs
+
+# my $date1 = "2019-02-10";
+# my $date2 = "2022-11-01"; # Output: 3 years 264 days ->265
+
+# my $date1 = "2020-09-15";
+# my $date2 = "2022-03-29"; # Output: 1 year 195 days -> ok
+
+
+# my $date1 = "2019-12-01";
+# my $date2 = "2019-12-31"; # Output: 30 day -> ok
+
+# my $date1 = "2019-12-31";
+# my $date2 = "2020-12-31"; # Output: 1 year -> and 1 day
+
+# my $date1 = "2019-12-31";
+# my $date2 = "2021-12-31"; # Output: 2 years -> and 1 day
+
+# my $date1 = "2020-09-15";
+# my $date2 = "2021-09-16"; # Output: 2 years -> and 1 day
+
+# my $date1 = "2019-09-15";
+# my $date2 = "2021-09-16"; # Output: 2 years 1day -> and 2day
+
+my $date1 = "2019-09-15";
+my $date2 = "2021-09-16"; # Output: 2 years 1day -> and 2day
+
+
+# my own tests
+# my $date1 = "934-2-10";
+# my $date2 = "934-1-5"; # not valid
+
+# my $date1 = "1932-2-29";
+# my $date2 = "1932-2-29"; #not valid
+
+# my $date1 = "2020-12-32";
+# my $date2 = "2021-2-27"; #not valid
+
+getDuration($date1, $date2);
+
+ sub createDateObj
+{
+ my ($year, $month, $day) = @_;
+
+ my $dateObj = DateTime->new(
+ year => $year,
+ month => $month,
+ day => $day,
+ hour => 0,
+ minute => 0,
+ second => 0,
+ nanosecond => 0,
+ );
+
+ return $dateObj;
+}
+
+sub isLeapYear
+{
+ my $year = shift;
+ my $returnval = 0;
+
+ if( ($year % 4) == 0)
+ {
+ # it is not a leapyear if it can be divied by 100
+ if( ($year % 100) == 0)
+ {
+ #but is a leapyear if it can be divided by 400
+ if( ($year % 400) == 0)
+ {
+ $returnval = 1;
+ }
+ }
+
+ #it is leapyear
+ else
+ {
+ $returnval = 1;
+ }
+ }
+
+ return $returnval;
+
+}
+sub getDuration
+{
+ my($start, $end) = @_;
+
+ my ($startYear, $startMonth, $startDay) = ($start =~ m/^ (\d{1,4}) - (\d{1,2}) - (\d{1,2}) $/x );
+ my ($endYear, $endMonth, $endDay) = ($end =~ m/^ (\d{1,4}) - (\d{1,2}) - (\d{1,2}) $/x );
+
+ if( (not defined $startYear) || (not defined $startMonth) || (not defined $startDay) ||
+ (not defined $endYear) || (not defined $endMonth) || (not defined $endDay))
+ {
+ die("Please give valid date in yyyy-mm-dd format!!");
+ }
+
+ #check if the end date is greater than the start date
+ if( $startYear > $endYear )
+ {
+ die "Start year is greater than previous year";
+ }
+ else
+ {
+ if( ($startYear == $endYear) && ($startMonth > $endMonth))
+ {
+ die "Start month is greater than previous month. Died";
+ }
+ else
+ {
+ if( ($startDay == $endDay) && ($startDay > $endDay))
+ {
+ die "Start day is greater than previous day. Died";
+ }
+ else
+ {
+ # date is valid yay!
+ }
+ }
+ }
+
+ my $startDate = createDateObj($startYear, $startMonth, $startDay);
+ my $endDate = createDateObj($endYear, $endMonth, $endDay);
+
+
+ my $duration = $endDate->delta_days($startDate);
+
+ my $days = $duration->{days};
+
+ say qq(Start $startYear \t $startMonth \t $startDay);
+ say qq(Start $endYear \t $endMonth \t $endDay);
+ say qq(Number of days between the two dates: $days);
+
+ my $durationInYears = int($endYear) - int($startYear);
+ my $returnYear;
+ my $leapDays = 0;
+
+ my $resultYears;
+ my $resultDays;
+
+ #if it is shorter than 1 year do not bothe with leapyear check
+ if($durationInYears < 1 )
+ {
+ $returnYear = 0;
+ $resultDays = $days;
+
+ }
+
+ # leapyear checks
+ else
+ {
+ for(int($startYear)..int($endYear))
+ {
+ $leapDays += isLeapYear($_);
+ }
+
+ # to eliminate the possibility of adding leapdays if the starting day is after 02 28
+ if( isLeapYear(int($startYear)) )
+ {
+ #if it is not february or january
+ if( (int($startMonth)>2) )
+ {
+ $leapDays--;
+ }
+
+ # it is january or february
+ else
+ {
+ if( (int($startMonth) == 2) && (int($startDay) == 29))
+ {
+ $leapDays--;
+ }
+ }
+ }
+
+ # to eliminate the possibility of adding leapdays if the ending day is before 02.28
+ if( isLeapYear(int($endYear)) )
+ {
+ #if it is not february or january
+ if( (int($endMonth)>2) )
+ {
+ $leapDays--;
+ #do nothing
+ }
+
+ # the duration ends after 02.29 a leapday is present
+ else
+ {
+
+ }
+ }
+
+ say qq(Number of leapdays in the duration $leapDays);
+ my $daysWithoutLeapDays = $days - $leapDays;
+
+ $resultYears = int($daysWithoutLeapDays / 365);
+
+ $resultDays = ($days - $resultYears*365);
+
+ }
+
+
+ # plural for for words
+ my $yearPlural= "";
+ if($resultYears > 1)
+ {
+ $yearPlural ="s";
+ }
+
+ my $dayPlural= "";
+ if($resultDays > 1)
+ {
+ $dayPlural ="s";
+ }
+
+
+ if(not defined $resultYears)
+ {
+ say qq($resultDays day$dayPlural );
+ }
+ elsif( $resultDays == 0)
+ {
+ say qq($resultYears year$yearPlural);
+ }
+
+ else
+ {
+
+ say qq($resultYears year$yearPlural and $resultDays day$dayPlural );
+ }
+
+
+} #END OF - sub getDuration \ No newline at end of file