aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-12-23 20:28:15 +0000
committerGitHub <noreply@github.com>2020-12-23 20:28:15 +0000
commitc88cde53c6c8063d042402b737e597337a2ad1fa (patch)
tree74cfce9fd3094b311f294a3426e6ac56bff3bd3f
parent69b8500c975e01c797f74b6b0ccf5431e7f465af (diff)
parent3414c0d0389b7c0a9c4a222f0aa1080a5906dce3 (diff)
downloadperlweeklychallenge-club-c88cde53c6c8063d042402b737e597337a2ad1fa.tar.gz
perlweeklychallenge-club-c88cde53c6c8063d042402b737e597337a2ad1fa.tar.bz2
perlweeklychallenge-club-c88cde53c6c8063d042402b737e597337a2ad1fa.zip
Merge pull request #3056 from pauloscustodio/085-perl
Add Perl solution to challenge 085
-rw-r--r--challenge-085/paulo-custodio/README1
-rw-r--r--challenge-085/paulo-custodio/perl/ch-1.pl63
-rw-r--r--challenge-085/paulo-custodio/perl/ch-2.pl62
-rw-r--r--challenge-085/paulo-custodio/test.pl23
4 files changed, 149 insertions, 0 deletions
diff --git a/challenge-085/paulo-custodio/README b/challenge-085/paulo-custodio/README
new file mode 100644
index 0000000000..87dc0b2fbd
--- /dev/null
+++ b/challenge-085/paulo-custodio/README
@@ -0,0 +1 @@
+Solution by Paulo Custodio
diff --git a/challenge-085/paulo-custodio/perl/ch-1.pl b/challenge-085/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..0d99e7ebd6
--- /dev/null
+++ b/challenge-085/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,63 @@
+#!/usr/bin/env perl
+
+# Challenge 085
+#
+# TASK #1 › Triplet Sum
+# Submitted by: Mohammad S Anwar
+# You are given an array of real numbers greater than zero.
+#
+# Write a script to find if there exists a triplet (a,b,c) such that 1 < a+b+c < 2. Print 1 if you succeed otherwise 0.
+#
+# Example 1:
+# Input: @R = (1.2, 0.4, 0.1, 2.5)
+# Output: 1 as 1 < 1.2 + 0.4 + 0.1 < 2
+# Example 2:
+# Input: @R = (0.2, 1.5, 0.9, 1.1)
+# Output: 0
+# Example 3:
+# Input: @R = (0.5, 1.1, 0.3, 0.7)
+# Output: 1 as 1 < 0.5 + 1.1 + 0.3 < 2
+
+use strict;
+use warnings;
+use 5.030;
+
+my @R = @ARGV;
+@R >= 3 or die "Need at least 3 values\n";
+
+say found(@R);
+
+# check sum of a triplet
+sub check {
+ my($result, $a, $b, $c) = @_;
+ my $sum = $a + $b + $c;
+ if (1 < $sum && $sum < 2) {
+ $$result = 1;
+ }
+}
+
+# next combination
+sub next_combination {
+ my($result, $set, $comb, $k) = @_;
+ if ($$result) { # trim tree, already found a solution
+ return;
+ }
+ elsif ($k == 0) { # check one triplet
+ check($result, @$comb);
+ }
+ else { # compute next combination
+ my @comb = @$comb;
+ for my $i (scalar(@comb) .. scalar(@$set)-$k) {
+ push @comb, $set->[$i];
+ next_combination($result, $set, \@comb, $k-1);
+ pop @comb;
+ }
+ }
+}
+
+sub found {
+ my(@set) = @_;
+ my $result = 0;
+ next_combination(\$result, \@set, [], 3); # check triplets
+ return $result;
+}
diff --git a/challenge-085/paulo-custodio/perl/ch-2.pl b/challenge-085/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..a0fd2373bf
--- /dev/null
+++ b/challenge-085/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,62 @@
+#!/usr/bin/env perl
+
+# Challenge 085
+#
+# TASK #2 › Power of Two Integers
+# Submitted by: Mohammad S Anwar
+# You are given a positive integer $N.
+#
+# Write a script to find if it can be expressed as a ** b where a > 0 and b > 1. Print 1 if you succeed otherwise 0.
+#
+# Example 1:
+# Input: 8
+# Output: 1 as 8 = 2 ** 3
+# Example 2:
+# Input: 15
+# Output: 0
+# Example 3:
+# Input: 125
+# Output: 1 as 125 = 5 ** 3
+
+use strict;
+use warnings;
+use 5.030;
+
+# Sieve of Eratosthenes
+my @sieve;
+my @primes;
+
+my($n) = @ARGV;
+say is_perfect_power($n);
+
+
+sub is_perfect_power {
+ my($n) = @_;
+ my $max_factor = sqrt($n)+1;
+ find_primes($max_factor); # fill list of prime numbers up to sqrt(n)+1
+ for my $prime (@primes) {
+ my $exp = 1;
+ my $power;
+ while (($power = $prime ** $exp) <= $n) {
+ if ($power == $n) {
+ return 1;
+ }
+ $exp++;
+ }
+ }
+ return 0;
+}
+
+sub find_primes {
+ my($max) = @_;
+ my $prime = 2;
+ do {
+ push @primes, $prime;
+ for (my $f = $prime*2; $f < $max; $f += $prime) {
+ $sieve[$f] = 1;
+ }
+ do {
+ $prime++
+ } while ($prime < $max && $sieve[$prime]);
+ } while ($prime < $max);
+}
diff --git a/challenge-085/paulo-custodio/test.pl b/challenge-085/paulo-custodio/test.pl
new file mode 100644
index 0000000000..125b8798e9
--- /dev/null
+++ b/challenge-085/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 1.2 0.4 0.1 2.5"), "1\n";
+is capture("perl/ch-1.pl 0.2 1.5 0.9 1.1"), "0\n";
+is capture("perl/ch-1.pl 0.5 1.1 0.3 0.7"), "1\n";
+
+is capture("perl/ch-2.pl 8"), "1\n";
+is capture("perl/ch-2.pl 15"), "0\n";
+is capture("perl/ch-2.pl 125"), "1\n";
+
+done_testing;
+
+sub capture {
+ my($cmd) = @_;
+ my $out = `$cmd`;
+ $out =~ s/[ \t\v\f\r]*\n/\n/g;
+ return $out;
+} \ No newline at end of file