aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-04-03 01:13:37 +0100
committerGitHub <noreply@github.com>2023-04-03 01:13:37 +0100
commit204a7afb2f22492eaccf4eed767affcdbfa39f6c (patch)
tree350df341ad269b7f329a08968553596e53e5d622
parent8db5cffaf71e1a4ed18bbbfb54aa01aecc010d62 (diff)
parentaaae5ce1a26657f6bb9152d7231f6167239bdc9d (diff)
downloadperlweeklychallenge-club-204a7afb2f22492eaccf4eed767affcdbfa39f6c.tar.gz
perlweeklychallenge-club-204a7afb2f22492eaccf4eed767affcdbfa39f6c.tar.bz2
perlweeklychallenge-club-204a7afb2f22492eaccf4eed767affcdbfa39f6c.zip
Merge pull request #7840 from pip/branch-for-challenge-210
Pip Stuart's submission for challenge-210.
-rw-r--r--challenge-210/pip/perl/ch-1.pl33
-rw-r--r--challenge-210/pip/perl/ch-2.pl45
-rw-r--r--challenge-210/pip/raku/ch-1.raku33
-rw-r--r--challenge-210/pip/raku/ch-2.raku45
4 files changed, 156 insertions, 0 deletions
diff --git a/challenge-210/pip/perl/ch-1.pl b/challenge-210/pip/perl/ch-1.pl
new file mode 100644
index 0000000000..894cbcc3a4
--- /dev/null
+++ b/challenge-210/pip/perl/ch-1.pl
@@ -0,0 +1,33 @@
+#!/usr/bin/perl
+# HTTPS://TheWeeklyChallenge.Org - Perl/Raku Weekly Challenge #210 - Pip Stuart
+# Task1: Kill and Win: Submitted by: Mohammad S Anwar; 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.
+# Example1:
+# In-put: @int = (2, 3, 1)
+# Output: 6 First we delete 2 and that would also delete 1 and 3. So the maximum points we get is 6.
+# Example2:
+# In-put: @int = (1, 1, 2, 2, 2, 3)
+# Output: 11 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.
+use strict;use warnings;use utf8;use v5.12;my $d8VS='N42MDxxD';
+sub KlWn {my @intz = @_;my $ttli = 0; my @sntz = sort { $a <=> $b } @intz;
+ while (@sntz) { if (@sntz - 1) { my $fndx = 0; # are we just looking to kill one greater than the lowest, if available?
+ while ($fndx < (@sntz - 1) && $sntz[0] == $sntz[$fndx]) { $fndx++; }
+ my $kval = $sntz[$fndx ];$ttli += $kval;splice(@sntz, $fndx , 1);my $szm1 = @sntz - 1; #say "pivot fndx:$fndx kval:$kval;";
+ for ( 0 .. $szm1) { #say "_:$_ neg_:" . ($szm1 - $_) . " snxd:$sntz[$szm1 - $_] sntz:@sntz;";
+ if ( $sntz[$szm1 - $_] == $kval + 1 ||
+ $sntz[$szm1 - $_] == $kval - 1) {
+ my $logv = $sntz[$szm1 - $_];$ttli += $logv;splice(@sntz, $szm1 - $_, 1); #say " log sndx:$_"." logv:$logv;";
+ } }
+ } else { my $lval = pop(@sntz); $ttli += $lval; #say " lst lndx:0" ." lval:$lval;";
+ } }
+ printf("(%-16s) => %s;\n", join(', ', @intz), $ttli);
+ return($ttli); # I don't see how this total of removed integers is ever going to be any different from just the sum of all the original integers.
+}
+if (@ARGV) {
+ KlWn(@ARGV);
+} else {
+ KlWn(2, 3, 1 ); # => 6;
+ KlWn(1, 1, 2, 2, 2, 3); # => 11;
+}
diff --git a/challenge-210/pip/perl/ch-2.pl b/challenge-210/pip/perl/ch-2.pl
new file mode 100644
index 0000000000..52b141f342
--- /dev/null
+++ b/challenge-210/pip/perl/ch-2.pl
@@ -0,0 +1,45 @@
+#!/usr/bin/perl
+# HTTPS://TheWeeklyChallenge.Org - Perl/Raku Weekly Challenge #210 - Pip Stuart
+# Task2: Number Collision: Submitted by: Mohammad S Anwar; You are given an array of integers which can move in right direction if it is positive and left
+# direction when negative. If two numbers collide then the smaller one will explode. And if both are same then they both explode. We take the absolute value
+# in consideration when comparing. All numbers move at the same speed, therefore any 2 numbers moving in the same direction will never collide.
+# Write a script to find out who survives the collision.
+# Example1:
+# In-put: @list = (2, 3, -1)
+# Output: (2, 3 ) The numbers 3 and -1 collide and -1 explodes in the end. So we are left with (2, 3).
+# Example2:
+# In-put: @list = (3, 2, -4)
+# Output: ( -4) The numbers 2 and -4 collide and 2 explodes in the end. That gives us (3, -4).
+# Now the numbers 3 and -4 collide and 3 explodes. Finally we are left with -4.
+# Example3:
+# In-put: @list = (1, -1)
+# Output: ( ) The numbers 1 and -1 both collide and explode. Nothing left in the end.
+# Last date to submit the solution 23:59 (UK Time) Sunday 2nd April 2023.
+use strict;use warnings;use utf8;use v5.12;my $d8VS='N42MEcmx';
+sub NCli { my @list = @_;my @mvls = @list;my $done = 0;
+ while (!$done) {
+ for (0 .. (@mvls - 2)) {
+ if ($mvls[$_] >= 0 && $mvls[$_ + 1] < 0) {
+ #say "ndxz:$_ and +1 posi:$mvls[$_] collides with nega:$mvls[$_ + 1]...;";
+ if (abs($mvls[$_]) == abs($mvls[$_ + 1])) { splice(@mvls, $_ , 2); }
+ elsif (abs($mvls[$_]) < abs($mvls[$_ + 1])) { splice(@mvls, $_ , 1); }
+ else { splice(@mvls, $_ + 1, 1); }
+ last; } } # any collision and splice should leave the loop to start over before finding the next collision
+ $done = 1;
+ for (0 .. (@mvls - 1)) {
+ if ($mvls[$_] < 0) { # look for any positives to the left of any negatives
+ for my $nndx ( 0 .. ($_ - 1)) {
+ if ( $mvls[$nndx] >= 0) { $done = 0;last; } }
+ } else { # && look for any negatives to the right of any positives
+ for my $pndx (($_ + 1) .. (@mvls - 1)) {
+ if ( $mvls[$pndx] < 0) { $done = 0;last; } }
+ } } }
+ printf("(%-8s) => (%4s);\n", join(', ', @list), join(', ', @mvls));
+ return(@mvls); }
+if (@ARGV) {
+ NCli(@ARGV);
+} else {
+ NCli(2, 3, -1); # => (2, 3);
+ NCli(3, 2, -4); # => (-4);
+ NCli(1, -1); # => ();
+}
diff --git a/challenge-210/pip/raku/ch-1.raku b/challenge-210/pip/raku/ch-1.raku
new file mode 100644
index 0000000000..7a738a4c19
--- /dev/null
+++ b/challenge-210/pip/raku/ch-1.raku
@@ -0,0 +1,33 @@
+#!/usr/bin/env raku
+# HTTPS://TheWeeklyChallenge.Org - Perl/Raku Weekly Challenge #210 - Pip Stuart
+# Task1: Kill and Win: Submitted by: Mohammad S Anwar; 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.
+# Example1:
+# In-put: @int = (2, 3, 1)
+# Output: 6 First we delete 2 and that would also delete 1 and 3. So the maximum points we get is 6.
+# Example2:
+# In-put: @int = (1, 1, 2, 2, 2, 3)
+# Output: 11 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.
+use v6;my $d8VS='N42MEt4Z';
+sub KlWn {my @intz = @_;my $ttli = 0; my @sntz = sort +*, @intz;
+ while (@sntz) { if (@sntz.elems - 1) { my $fndx = 0; # are we just looking to kill one greater than the lowest, if available?
+ while ($fndx < (@sntz.elems - 1) && @sntz[0] == @sntz[$fndx]) { $fndx++; }
+ my $kval = @sntz[$fndx ];$ttli += $kval;splice(@sntz, $fndx , 1);my $szm1 = @sntz.elems - 1; #say "pivot fndx:$fndx kval:$kval;";
+ for ( 0 .. $szm1) { #say "_:$_ neg_:" ~ ($szm1 - $_) ~ " snxd:$sntz[$szm1 - $_] sntz:@sntz;";
+ if ( @sntz[$szm1 - $_] == $kval + 1 ||
+ @sntz[$szm1 - $_] == $kval - 1) {
+ my $logv = @sntz[$szm1 - $_];$ttli += $logv;splice(@sntz, $szm1 - $_, 1); #say " log sndx:$_"~" logv:$logv;";
+ } }
+ } else { my $lval = pop(@sntz); $ttli += $lval; #say " lst lndx:0" ~" lval:$lval;";
+ } }
+ printf("(%-16s) => %s;\n", join(', ', @intz), $ttli);
+ return($ttli); # I don't see how this total of removed integers is ever going to be any different from just the sum of all the original integers.
+}
+if (@*ARGS) {
+ KlWn(@*ARGS);
+} else {
+ KlWn(2, 3, 1 ); # => 6;
+ KlWn(1, 1, 2, 2, 2, 3); # => 11;
+}
diff --git a/challenge-210/pip/raku/ch-2.raku b/challenge-210/pip/raku/ch-2.raku
new file mode 100644
index 0000000000..05d6fe5f75
--- /dev/null
+++ b/challenge-210/pip/raku/ch-2.raku
@@ -0,0 +1,45 @@
+#!/usr/bin/env raku
+# HTTPS://TheWeeklyChallenge.Org - Perl/Raku Weekly Challenge #210 - Pip Stuart
+# Task2: Number Collision: Submitted by: Mohammad S Anwar; You are given an array of integers which can move in right direction if it is positive and left
+# direction when negative. If two numbers collide then the smaller one will explode. And if both are same then they both explode. We take the absolute value
+# in consideration when comparing. All numbers move at the same speed, therefore any 2 numbers moving in the same direction will never collide.
+# Write a script to find out who survives the collision.
+# Example1:
+# In-put: @list = (2, 3, -1)
+# Output: (2, 3 ) The numbers 3 and -1 collide and -1 explodes in the end. So we are left with (2, 3).
+# Example2:
+# In-put: @list = (3, 2, -4)
+# Output: ( -4) The numbers 2 and -4 collide and 2 explodes in the end. That gives us (3, -4).
+# Now the numbers 3 and -4 collide and 3 explodes. Finally we are left with -4.
+# Example3:
+# In-put: @list = (1, -1)
+# Output: ( ) The numbers 1 and -1 both collide and explode. Nothing left in the end.
+# Last date to submit the solution 23:59 (UK Time) Sunday 2nd April 2023.
+use v6;my $d8VS='N42MEw1A';
+sub NCli { my @list = @_;my @mvls = @list;my $done = 0;
+ while (!$done) {
+ for (0 .. (@mvls.elems - 2)) {
+ if (@mvls[$_] >= 0 && @mvls[$_ + 1] < 0) {
+ #say "ndxz:$_ and +1 posi:@mvls[$_] collides with nega:@mvls[$_ + 1]...;";
+ if (abs(@mvls[$_]) == abs(@mvls[$_ + 1])) { splice(@mvls, $_ , 2); }
+ elsif (abs(@mvls[$_]) < abs(@mvls[$_ + 1])) { splice(@mvls, $_ , 1); }
+ else { splice(@mvls, $_ + 1, 1); }
+ last; } } # any collision and splice should leave the loop to start over before finding the next collision
+ $done = 1;
+ for (0 .. (@mvls.elems - 1)) {
+ if (@mvls[$_] < 0) { # look for any positives to the left of any negatives
+ for ( 0 .. ($_ - 1)) -> $nndx {
+ if ( @mvls[$nndx] >= 0) { $done = 0;last; } }
+ } else { # && look for any negatives to the right of any positives
+ for (($_ + 1) .. (@mvls.elems - 1)) -> $pndx {
+ if ( @mvls[$pndx] < 0) { $done = 0;last; } }
+ } } }
+ printf("(%-8s) => (%4s);\n", join(', ', @list), join(', ', @mvls));
+ return(@mvls); }
+if (@*ARGS) {
+ NCli(@*ARGS);
+} else {
+ NCli(2, 3, -1); # => (2, 3);
+ NCli(3, 2, -4); # => (-4);
+ NCli(1, -1); # => ();
+}