aboutsummaryrefslogtreecommitdiff
path: root/challenge-083
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2020-12-23 21:29:44 +0000
committerPaulo Custodio <pauloscustodio@gmail.com>2020-12-23 21:29:44 +0000
commit6e3df1e120ae31c7951ab4e458de0d4704f8efc5 (patch)
tree262e67ed52f8c6868d5431ac6cc852ae02a4a67e /challenge-083
parent470f76659c0503d5396f800d608fed5d7679dd9c (diff)
downloadperlweeklychallenge-club-6e3df1e120ae31c7951ab4e458de0d4704f8efc5.tar.gz
perlweeklychallenge-club-6e3df1e120ae31c7951ab4e458de0d4704f8efc5.tar.bz2
perlweeklychallenge-club-6e3df1e120ae31c7951ab4e458de0d4704f8efc5.zip
Add Perl solution to challenge 083
Diffstat (limited to 'challenge-083')
-rw-r--r--challenge-083/paulo-custodio/README1
-rw-r--r--challenge-083/paulo-custodio/perl/ch-1.pl26
-rw-r--r--challenge-083/paulo-custodio/perl/ch-2.pl82
-rw-r--r--challenge-083/paulo-custodio/test.pl23
4 files changed, 132 insertions, 0 deletions
diff --git a/challenge-083/paulo-custodio/README b/challenge-083/paulo-custodio/README
new file mode 100644
index 0000000000..87dc0b2fbd
--- /dev/null
+++ b/challenge-083/paulo-custodio/README
@@ -0,0 +1 @@
+Solution by Paulo Custodio
diff --git a/challenge-083/paulo-custodio/perl/ch-1.pl b/challenge-083/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..d516558128
--- /dev/null
+++ b/challenge-083/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,26 @@
+#!/usr/bin/env perl
+
+# Challenge 083
+#
+# TASK #1 › Words Length
+# Submitted by: Mohammad S Anwar
+# You are given a string $S with 3 or more words.
+#
+# Write a script to find the length of the string except the first and last
+# words ignoring whitespace.
+#
+# Example 1:
+# Input: $S = "The Weekly Challenge"
+#
+# Output: 6
+# Example 2:
+# Input: $S = "The purpose of our lives is to be happy"
+#
+# Output: 23
+
+use strict;
+use warnings;
+use 5.030;
+
+@ARGV >= 3 or die "need at least 3 words\n";
+say length(join('', @ARGV[1 .. $#ARGV-1]));
diff --git a/challenge-083/paulo-custodio/perl/ch-2.pl b/challenge-083/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..b191a5a7b7
--- /dev/null
+++ b/challenge-083/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,82 @@
+#!/usr/bin/env perl
+
+# Challenge 083
+#
+# TASK #2 › Flip Array
+# Submitted by: Mohammad S Anwar
+# You are given an array @A of positive numbers.
+#
+# Write a script to flip the sign of some members of the given array so that
+# the sum of the all members is minimum non-negative.
+#
+# Given an array of positive elements, you have to flip the sign of some of
+# its elements such that the resultant sum of the elements of array should be
+# minimum non-negative(as close to zero as possible). Return the minimum no. of
+# elements whose sign needs to be flipped such that the resultant sum is minimum
+# non-negative.
+#
+# Example 1:
+# Input: @A = (3, 10, 8)
+# Output: 1
+# Explanation:
+# Flipping the sign of just one element 10 gives the result 1 i.e. (3) + (-10) + (8) = 1
+# Example 2:
+# Input: @A = (12, 2, 10)
+# Output: 1
+# Explanation:
+# Flipping the sign of just one element 12 gives the result 0 i.e. (-12) + (2) + (10) = 0
+
+use strict;
+use warnings;
+use 5.030;
+
+my @A = @ARGV;
+say count_flips(@A);
+
+
+sub count_flips {
+ my(@a) = @_;
+
+ # setup initial conditions
+ my @sign = (1) x scalar(@a);
+ my $min_flips = 0;
+ my $min_sum = sumprod(\@a, \@sign);
+
+ while (next_flip(\@sign)) {
+ my $sum = sumprod(\@a, \@sign);
+ if ($sum >= 0) {
+ my $flips = scalar(grep {$_==-1} @sign);
+ if ($sum < $min_sum) {
+ ($min_sum, $min_flips) = ($sum, $flips);
+ }
+ elsif ($sum == $min_sum && $flips < $min_flips) {
+ ($min_sum, $min_flips) = ($sum, $flips);
+ }
+ }
+ }
+ return $min_flips;
+}
+
+# odometer-style sign flipper
+sub next_flip {
+ my($sign) = @_;
+ for my $i (0 .. $#{$sign}) {
+ if ($sign->[$i] == 1) {
+ $sign->[$i] = -1;
+ return 1;
+ }
+ else {
+ $sign->[$i] = 1;
+ }
+ }
+ return 0;
+}
+
+sub sumprod {
+ my($a, $sign) = @_;
+ my $sum = 0;
+ for my $i (0 .. $#{$a}) {
+ $sum += $a->[$i]*$sign->[$i];
+ }
+ return $sum;
+}
diff --git a/challenge-083/paulo-custodio/test.pl b/challenge-083/paulo-custodio/test.pl
new file mode 100644
index 0000000000..e6ecd1dc0f
--- /dev/null
+++ b/challenge-083/paulo-custodio/test.pl
@@ -0,0 +1,23 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use Test::More;
+use 5.030;
+
+is capture("perl/ch-1.pl The Weekly Challenge"), "6\n";
+is capture("perl/ch-1.pl The purpose of our lives is to be happy"), "23\n";
+
+
+is capture("perl/ch-2.pl 3 10 8"), "1\n";
+is capture("perl/ch-2.pl 12 2 10"), "1\n";
+
+
+done_testing;
+
+sub capture {
+ my($cmd) = @_;
+ my $out = `$cmd`;
+ $out =~ s/[ \t\v\f\r]*\n/\n/g;
+ return $out;
+}