aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSolathian <horvath6@gmail.com>2022-10-23 08:42:50 +0200
committerSolathian <horvath6@gmail.com>2022-10-23 08:42:50 +0200
commit142fed59cfc6ca9ce40637080a89bc1d2086012d (patch)
tree2ebde1f4a67d281bc8bb6ee6d7aaedef8dda0c1e
parentb7ab21d6c681cb4ae35a38fec010a5b57c2b5b25 (diff)
downloadperlweeklychallenge-club-142fed59cfc6ca9ce40637080a89bc1d2086012d.tar.gz
perlweeklychallenge-club-142fed59cfc6ca9ce40637080a89bc1d2086012d.tar.bz2
perlweeklychallenge-club-142fed59cfc6ca9ce40637080a89bc1d2086012d.zip
Added files for challange 187
-rw-r--r--challenge-187/solathian/perl/ch-1.pl80
-rw-r--r--challenge-187/solathian/perl/ch-2.pl81
2 files changed, 161 insertions, 0 deletions
diff --git a/challenge-187/solathian/perl/ch-1.pl b/challenge-187/solathian/perl/ch-1.pl
new file mode 100644
index 0000000000..fd7a6fa4b8
--- /dev/null
+++ b/challenge-187/solathian/perl/ch-1.pl
@@ -0,0 +1,80 @@
+#!usr/bin/perl -w
+
+# Challange 187 - 1 - Days Together
+# Two friends, Foo and Bar gone on holidays seperately to the same city. You are given their schedule i.e. start date and end date.
+# To keep the task simple, the date is in the form DD-MM and all dates belong to the same calendar year i.e. between 01-01 and 31-12.
+# Also the year is non-leap year and both dates are inclusive.
+# Write a script to find out for the given schedule, how many days they spent together in the city, if at all.
+
+use strict;
+use warnings;
+no warnings 'experimental';
+
+use feature ('say', 'signatures');
+
+use constant
+{
+ MONTHS => [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
+};
+
+
+# my $input0 = { Foo => { SD =>'12-01', ED => '20-01'}, Bar => { SD =>'15-01', ED => '18-01'} };
+# say ("Output:", days($input0));
+# Output: 4 days
+
+# my $input1 = { Foo => { SD =>'02-03', ED => '12-03'}, Bar => { SD =>'13-03', ED => '14-03'} };
+# say ("Output:", days($input1));
+# Output: 0 day
+
+
+# my $input2 = { Foo => { SD =>'02-03', ED => '12-03'}, Bar => { SD =>'11-03', ED => '15-03'} };
+# say ("Output:", days($input2));
+# Output: 2 days
+
+
+# my $input3 = { Foo => { SD =>'30-03', ED => '05-04'}, Bar => { SD =>'28-03', ED => '02-04'} };
+# say ("Output:", days($input3));
+# Output: 4 days
+
+# convert month-day into index, assume that is in correct format
+sub convert($key)
+{
+ my ($day, $month) = $key =~ /(\d{2})-(\d{2})/ ;
+
+ my $i = 0;
+ my $days = $day;
+
+ while ($i < $month - 1)
+ {
+ $days += MONTHS->[$i];
+ $i++;
+ }
+
+ return $days;
+}
+
+sub days($hashRef)
+{
+ my $retVal = 0;
+
+ my $startFoo = convert($hashRef->{Foo}->{SD});
+ my $endFoo = convert($hashRef->{Foo}->{ED});
+
+ my $startBar = convert($hashRef->{Bar}->{SD});
+ my $endBar = convert($hashRef->{Bar}->{ED});
+
+ # if they do not meet
+ if( ($endFoo < $startBar) || ($endBar < $startFoo))
+ {
+ # do nothing
+ }
+ else
+ {
+ my @tempArr = ($startFoo, $endFoo, $startBar, $endBar);
+ @tempArr = sort @tempArr;
+
+ $retVal = $tempArr[2] - $tempArr[1] + 1 ;
+ }
+
+ return $retVal;
+} \ No newline at end of file
diff --git a/challenge-187/solathian/perl/ch-2.pl b/challenge-187/solathian/perl/ch-2.pl
new file mode 100644
index 0000000000..088601c55f
--- /dev/null
+++ b/challenge-187/solathian/perl/ch-2.pl
@@ -0,0 +1,81 @@
+#!usr/bin/perl -w
+use strict;
+use warnings;
+no warnings 'experimental';
+
+use feature ('say', 'state', 'signatures');
+use Data::Dumper;
+
+use constant
+{
+ TRUE => 1,
+ FALSE => 0,
+};
+
+# Challange 187 - 2 - Magical Triplets
+# You are given a list of positive numbers, @n, having at least 3 numbers.
+# Write a script to find the triplets (a, b, c) from the given list that satisfies the following rules.
+ # 1. a + b > c
+ # 2. b + c > a
+ # 3. a + c > b
+ # 4. a + b + c is maximum.
+# In case, you end up with more than one triplets having the maximum then pick the triplet where a >= b >= c.
+
+# my @inp0 = (1, 2, 3, 2);
+# magic(@inp0);
+ # Output: (3, 2, 2)
+
+# my @inp1 = (1, 3, 2);
+# magic(@inp1);
+ # Output: ()
+
+# my @inp2 = (1, 1, 2, 3);
+# magic(@inp2);
+ # Output: ()
+
+# my @inp3 = (2, 4, 3);
+# magic(@inp3);
+ # Output: (4, 3, 2)
+
+# my @inp4 = qw(47 49 46 38 39 61);
+# magic(@inp4);
+
+# my @inp5 = qw(100 44 32 38 39 98 20);
+# magic(@inp5);
+# (100,98,44)
+
+
+sub isItMagic($a, $b, $c)
+{
+ state $max = 0;
+ my $returnVal = FALSE;
+
+ if(($a + $b > $c) && ($b + $c > $a) && ($a + $c > $b) && ($a + $b + $c > $max)) # heck the rules
+ {
+ $max = $a + $b + $c;
+ $returnVal = TRUE;
+ }
+ return $returnVal;
+}
+
+sub magic(@array)
+{
+
+ my $retVal = "()";
+ @array = sort{$b <=> $a} @array; # this way we will find the greatest a b c combination and do no have to deal with the a >= b >= c.
+
+ for(my $i = 0, my $length = @array; $i < $length; $i++)
+ {
+ my $j = 0;
+ while(($j + 2) < $length) # if the list is long traverse elements far back
+ {
+ if(isItMagic($array[$i], $array[($i + $j + 1) % $length], $array[($i + $j + 2) % $length]) == TRUE)
+ {
+ $retVal = "Output: (".join(',', $array[$i], $array[($i + $j + 1) % $length], $array[($i + $j + 2) % $length]).")";
+ }
+ $j++;
+ }
+
+ }
+ say $retVal;
+} \ No newline at end of file