aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-04-03 00:57:57 +0100
committerGitHub <noreply@github.com>2023-04-03 00:57:57 +0100
commit95ce7ec7c094ff347155ee2871446c53bf80ff56 (patch)
treef246592b614aa94f7ccf39f55b9d7d1c382487c3
parentea90dfe69abb7702a279b82ec1a224d9245c868f (diff)
parent46fc8573f75c49daa489ac7803d3a845318a9751 (diff)
downloadperlweeklychallenge-club-95ce7ec7c094ff347155ee2871446c53bf80ff56.tar.gz
perlweeklychallenge-club-95ce7ec7c094ff347155ee2871446c53bf80ff56.tar.bz2
perlweeklychallenge-club-95ce7ec7c094ff347155ee2871446c53bf80ff56.zip
Merge pull request #7831 from Solathian/branch-for-challenge-210
Adding files for challenge!
-rw-r--r--challenge-210/solathian/perl/ch-1.pl35
-rw-r--r--challenge-210/solathian/perl/ch-2.pl80
2 files changed, 115 insertions, 0 deletions
diff --git a/challenge-210/solathian/perl/ch-1.pl b/challenge-210/solathian/perl/ch-1.pl
new file mode 100644
index 0000000000..f9a8f414d2
--- /dev/null
+++ b/challenge-210/solathian/perl/ch-1.pl
@@ -0,0 +1,35 @@
+#!usr/bin/perl
+use v5.36;
+
+# Challenge 210 - 1 - Kill and Win
+
+# You are given a list of integers.
+# Write a script to get the maximum points. You are allowed to take out (kill) any integer and remove from the list.
+# However if you do that then all integers exactly one-less or one-more would also be removed.
+# Find out the total of integers removed.
+
+killAndWin(2, 3, 1);
+# First we delete 2 and that would also delete 1 and 3. So the maximum points we get is 6.
+killAndWin(1, 1, 2, 2, 2, 3);
+# First we delete 2 and that would also delete both the 1's and the 3. Now we have (2, 2).
+# Then we delete another 2 and followed by the third deletion of 2. So the maximum points we get is 11.
+
+
+###
+# according to the current rules we do not have to think since the one-less and one-more elements are also counted
+# and we can take any number of steps
+# so the sum of the elements will be the max with the current rules
+# it would be much more challenging if during the one-less and one-more take away would not be counted for the total
+# sadly did not have time to implement that :(
+###
+
+sub killAndWin
+{
+ my $sum;
+ foreach my $var (@_)
+ {
+ $sum += $var;
+ }
+
+ say $sum;
+}
diff --git a/challenge-210/solathian/perl/ch-2.pl b/challenge-210/solathian/perl/ch-2.pl
new file mode 100644
index 0000000000..69408af123
--- /dev/null
+++ b/challenge-210/solathian/perl/ch-2.pl
@@ -0,0 +1,80 @@
+#!usr/bin/perl
+use v5.36;
+
+use builtin qw(indexed true false);
+no warnings 'experimental';
+
+# Challenge 210 - 2 - Number Collision
+
+
+main(2, 3, -1);
+main(3, 2, -4);
+main(1, -1);
+main(1, -1, 5);
+main(1, -1, 5, -6);
+main(1, -2, 5, -6);
+main(1, -6, 5, -6);
+main(1, -1, 5, -6, 7);
+main(-7, 7);
+main(-7, -6);
+main(-7, -6, 1);
+main(-7, -6, 7);
+
+sub isPos($p){ $p>0 ? return true : return false}
+sub isNeg($p){ $p<0 ? return true : return false}
+
+sub isPosBoth($p1, $p2) { (isPos($p1) && isPos($p2)) ? return true : return false}
+sub isNegBoth($p1, $p2) { (isNeg($p1) && isNeg($p2)) ? return true : return false}
+
+sub noSignChange($arrRef)
+{
+ my $retVal = true;
+ my $firstVal = $arrRef->[0];
+
+ foreach my $val (@$arrRef)
+ {
+ if( isPos($firstVal) && isNeg($val) )
+ {
+ $retVal = false;
+ last;
+ }
+ $firstVal = $val;
+ }
+
+ return $retVal;
+}
+
+
+sub collision( $arrRef)
+{
+ foreach my ($i, $currentVal) (indexed @$arrRef)
+ {
+ last if( ($i + 1) > $#$arrRef); # skip if we are at the last element
+
+ my $nextVal = $arrRef->[$i + 1];
+
+ next if( isPosBoth($currentVal, $nextVal) || isNegBoth($currentVal, $nextVal) ); # skip if both elements have the same sign
+ next if( isNeg($currentVal) && isPos($nextVal) ); # skip if the left element goes left while the right to the right
+
+
+ if( abs($nextVal) == abs($currentVal)){ splice(@$arrRef, $i, 2) }
+ elsif(abs($nextVal) > abs($currentVal)){ splice(@$arrRef, $i, 1) }
+ elsif(abs($nextVal) < abs($currentVal)){ splice(@$arrRef, ($i + 1), 1) }
+
+ }
+}
+
+
+sub main(@array)
+{
+ if( (@array == 0 ) || (noSignChange(\@array)) )
+ {
+ if( @array == 0){ say "Nothing left in the end." }
+ else { say("We are left with: ", join(',', @array))}
+ }
+ else
+ {
+ collision(\@array);
+ main(@array);
+ }
+} \ No newline at end of file