aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-04-03 05:38:25 +0100
committerGitHub <noreply@github.com>2021-04-03 05:38:25 +0100
commit83f0a0880120054ee505e767d25bab0796a4aa36 (patch)
treeee5089ed41d241fb287a79f908e62d2db67e8e80
parent74d9ff3b455d296a2979929c2271c7ad749e4370 (diff)
parentb896a71040c1e32394257fcb53f3e0c5a845829a (diff)
downloadperlweeklychallenge-club-83f0a0880120054ee505e767d25bab0796a4aa36.tar.gz
perlweeklychallenge-club-83f0a0880120054ee505e767d25bab0796a4aa36.tar.bz2
perlweeklychallenge-club-83f0a0880120054ee505e767d25bab0796a4aa36.zip
Merge pull request #3815 from pauloscustodio/paulo-custodio
Add Perl solution to challenge 106
-rw-r--r--challenge-106/paulo-custodio/perl/ch-1.pl40
-rw-r--r--challenge-106/paulo-custodio/perl/ch-2.pl44
-rw-r--r--challenge-106/paulo-custodio/t/test-1.yaml30
-rw-r--r--challenge-106/paulo-custodio/t/test-2.yaml20
-rw-r--r--challenge-106/paulo-custodio/test.pl7
5 files changed, 141 insertions, 0 deletions
diff --git a/challenge-106/paulo-custodio/perl/ch-1.pl b/challenge-106/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..3ffc4020ff
--- /dev/null
+++ b/challenge-106/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,40 @@
+#!/usr/bin/perl
+
+# Challenge 106
+#
+# TASK #1 › Maximum Gap
+# Submitted by: Mohammad S Anwar
+# You are given an array of integers @N.
+#
+# Write a script to display the maximum difference between two successive
+# elements once the array is sorted.
+#
+# If the array contains only 1 element then display 0.
+#
+# Example
+# Input: @N = (2, 9, 3, 5)
+# Output: 4
+#
+# Input: @N = (1, 3, 8, 2, 0)
+# Output: 5
+#
+# Input: @N = (5)
+# Output: 0
+
+use Modern::Perl;
+
+my @N = @ARGV;
+say max_gap(@N);
+
+sub max_gap {
+ my(@n) = @_;
+ return 0 if @n < 2;
+ @n = sort @n;
+
+ my $max_gap = 0;
+ for my $i (0..$#n-1) {
+ my $gap = $n[$i+1] - $n[$i];
+ $max_gap = $gap if $gap > $max_gap;
+ }
+ return $max_gap;
+}
diff --git a/challenge-106/paulo-custodio/perl/ch-2.pl b/challenge-106/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..020663f23f
--- /dev/null
+++ b/challenge-106/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,44 @@
+#!/usr/bin/perl
+
+# TASK #2 › Decimal String
+# Submitted by: Mohammad S Anwar
+# You are given numerator and denominator i.e. $N and $D.
+#
+# Write a script to convert the fraction into decimal string. If the fractional
+# part is recurring then put it in parenthesis.
+#
+# Example
+# Input: $N = 1, $D = 3
+# Output: "0.(3)"
+#
+# Input: $N = 1, $D = 2
+# Output: "0.5"
+#
+# Input: $N = 5, $D = 66
+# Output: "0.0(75)"
+
+use Modern::Perl;
+use Math::BigFloat;
+
+my($N, $D) = @ARGV;
+say decimal($N, $D);
+
+sub decimal {
+ my($n,$d) = @_;
+
+ Math::BigFloat->round_mode('trunc'); # so that 1/6=0.16666
+ Math::BigFloat->accuracy(1000); # very long list of digits
+
+ my $N = Math::BigFloat->new($n);
+ my $D = Math::BigFloat->new($d);
+ my $Q = $N->copy()->bdiv($D);
+ $Q =~ s/(\.\d+?)0+$/$1/; # remove 00000 from 2.30000
+
+ # naive solution: finds repetitions by string match
+ for my $rept (1..100) {
+ my $code = "return \$Q if \$Q =~ s/((\\d{$rept})\\2+)\\d*?\$/\\(\$2\\)/;";
+ eval $code;
+ }
+ # no repetitions
+ return $Q;
+}
diff --git a/challenge-106/paulo-custodio/t/test-1.yaml b/challenge-106/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..7f4993223e
--- /dev/null
+++ b/challenge-106/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,30 @@
+- setup:
+ cleanup:
+ args:
+ input:
+ output: 0
+- setup:
+ cleanup:
+ args: 2
+ input:
+ output: 0
+- setup:
+ cleanup:
+ args: 2 9
+ input:
+ output: 7
+- setup:
+ cleanup:
+ args: 2 9 3
+ input:
+ output: 6
+- setup:
+ cleanup:
+ args: 2 9 3 5
+ input:
+ output: 4
+- setup:
+ cleanup:
+ args: 1 3 8 2 0
+ input:
+ output: 5
diff --git a/challenge-106/paulo-custodio/t/test-2.yaml b/challenge-106/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..ae19b9024d
--- /dev/null
+++ b/challenge-106/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,20 @@
+- setup:
+ cleanup:
+ args: 1 3
+ input:
+ output: 0.(3)
+- setup:
+ cleanup:
+ args: 1 2
+ input:
+ output: 0.5
+- setup:
+ cleanup:
+ args: 5 66
+ input:
+ output: 0.0(75)
+- setup:
+ cleanup:
+ args: 89 7
+ input:
+ output: 12.(714285)
diff --git a/challenge-106/paulo-custodio/test.pl b/challenge-106/paulo-custodio/test.pl
new file mode 100644
index 0000000000..01ed2b83cd
--- /dev/null
+++ b/challenge-106/paulo-custodio/test.pl
@@ -0,0 +1,7 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use 5.030;
+
+require '../../challenge-001/paulo-custodio/test.pl';