aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2021-11-12 08:40:21 +0000
committerPaulo Custodio <pauloscustodio@gmail.com>2021-11-12 08:40:21 +0000
commitd00e3b49c7580928b6e32080bd3ce9f4f7d7bbac (patch)
tree5d7cf05f1d0058cbca462fd9134ba60b856cb4d6
parent60e9cb0fcbf3a557ffa17a2243e309e8489847c3 (diff)
downloadperlweeklychallenge-club-d00e3b49c7580928b6e32080bd3ce9f4f7d7bbac.tar.gz
perlweeklychallenge-club-d00e3b49c7580928b6e32080bd3ce9f4f7d7bbac.tar.bz2
perlweeklychallenge-club-d00e3b49c7580928b6e32080bd3ce9f4f7d7bbac.zip
Add Python solution to challenge 83
-rw-r--r--challenge-083/paulo-custodio/Makefile2
-rw-r--r--challenge-083/paulo-custodio/perl/ch-1.pl8
-rw-r--r--challenge-083/paulo-custodio/perl/ch-2.pl10
-rw-r--r--challenge-083/paulo-custodio/python/ch-1.py23
-rw-r--r--challenge-083/paulo-custodio/python/ch-2.py63
-rw-r--r--challenge-083/paulo-custodio/t/test-1.yaml10
-rw-r--r--challenge-083/paulo-custodio/t/test-2.yaml10
-rw-r--r--challenge-083/paulo-custodio/test.pl21
8 files changed, 118 insertions, 29 deletions
diff --git a/challenge-083/paulo-custodio/Makefile b/challenge-083/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-083/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-083/paulo-custodio/perl/ch-1.pl b/challenge-083/paulo-custodio/perl/ch-1.pl
index 597e65e4ea..31b52edb15 100644
--- a/challenge-083/paulo-custodio/perl/ch-1.pl
+++ b/challenge-083/paulo-custodio/perl/ch-1.pl
@@ -1,8 +1,8 @@
-#!/usr/bin/perl
+#!/usr/bin/env perl
# Challenge 083
#
-# TASK #1 › Words Length
+# TASK #1 > Words Length
# Submitted by: Mohammad S Anwar
# You are given a string $S with 3 or more words.
#
@@ -16,9 +16,9 @@
# Example 2:
# Input: $S = "The purpose of our lives is to be happy"
#
-# Output: 23
+# Output: 29
use Modern::Perl;
@ARGV >= 3 or die "need at least 3 words\n";
-say length(join('', @ARGV[1 .. $#ARGV-1]));
+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
index 0332afc373..76e58cb9c9 100644
--- a/challenge-083/paulo-custodio/perl/ch-2.pl
+++ b/challenge-083/paulo-custodio/perl/ch-2.pl
@@ -1,8 +1,8 @@
-#!/usr/bin/perl
+#!/usr/bin/env perl
# Challenge 083
#
-# TASK #2 › Flip Array
+# TASK #2 > Flip Array
# Submitted by: Mohammad S Anwar
# You are given an array @A of positive numbers.
#
@@ -19,12 +19,14 @@
# 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
+# 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
+# Flipping the sign of just one element 12 gives the result 0 i.e.
+# (-12) + (2) + (10) = 0
use Modern::Perl;
diff --git a/challenge-083/paulo-custodio/python/ch-1.py b/challenge-083/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..8fcedd0de9
--- /dev/null
+++ b/challenge-083/paulo-custodio/python/ch-1.py
@@ -0,0 +1,23 @@
+#!/usr/bin/env python3
+
+# 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: 29
+
+import sys
+
+print(len(" ".join(sys.argv[2:-1])))
diff --git a/challenge-083/paulo-custodio/python/ch-2.py b/challenge-083/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..9ae984375c
--- /dev/null
+++ b/challenge-083/paulo-custodio/python/ch-2.py
@@ -0,0 +1,63 @@
+#!/usr/bin/env python3
+
+# 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
+
+import sys
+
+def sumprod(a, b):
+ sum = 0
+ for i in range(len(a)):
+ sum += a[i]*b[i]
+ return sum
+
+# odometer-style sign flipper
+def next_flip(sign):
+ for i in range(len(sign)):
+ if sign[i]==1:
+ sign[i]=-1
+ return True
+ else:
+ sign[i]=1
+ return False
+
+def count_flips(a):
+ # setup initial conditions
+ sign = [1 for x in a]
+ min_flips = 0
+ min_sum = sumprod(a, sign)
+
+ while next_flip(sign):
+ sum = sumprod(a, sign)
+ if sum >= 0:
+ flips = len(list(filter(lambda x: x==-1, sign)))
+ if sum < min_sum or (sum == min_sum and flips < min_flips):
+ min_sum, min_flips = sum, flips
+ return min_flips
+
+print(count_flips([int(x) for x in sys.argv[1:]]))
diff --git a/challenge-083/paulo-custodio/t/test-1.yaml b/challenge-083/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..36c48df573
--- /dev/null
+++ b/challenge-083/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,10 @@
+- setup:
+ cleanup:
+ args: The Weekly Challenge
+ input:
+ output: 6
+- setup:
+ cleanup:
+ args: The purpose of our lives is to be happy
+ input:
+ output: 29
diff --git a/challenge-083/paulo-custodio/t/test-2.yaml b/challenge-083/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..464f89fab1
--- /dev/null
+++ b/challenge-083/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,10 @@
+- setup:
+ cleanup:
+ args: 3 10 8
+ input:
+ output: 1
+- setup:
+ cleanup:
+ args: 12 2 10
+ input:
+ output: 1
diff --git a/challenge-083/paulo-custodio/test.pl b/challenge-083/paulo-custodio/test.pl
deleted file mode 100644
index a990681a37..0000000000
--- a/challenge-083/paulo-custodio/test.pl
+++ /dev/null
@@ -1,21 +0,0 @@
-#!/usr/bin/perl
-
-use Modern::Perl;
-use Test::More;
-
-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;
-}