aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-04-05 00:26:24 +0100
committerGitHub <noreply@github.com>2021-04-05 00:26:24 +0100
commit4cc8e11d6ab81a75d138a8bd3639d49b31ab26c6 (patch)
tree50707bb62d95078312d269a227926bdfa11f3a42
parentca47f7426951f71103aa20492f1fecef05c013c5 (diff)
parent0594f4a7e117026ec4eee4bf2de65cecf03f4396 (diff)
downloadperlweeklychallenge-club-4cc8e11d6ab81a75d138a8bd3639d49b31ab26c6.tar.gz
perlweeklychallenge-club-4cc8e11d6ab81a75d138a8bd3639d49b31ab26c6.tar.bz2
perlweeklychallenge-club-4cc8e11d6ab81a75d138a8bd3639d49b31ab26c6.zip
Merge pull request #3827 from E7-87-83/newt
challenges
-rw-r--r--challenge-105/cheok-yin-fung/perl/ch-1.pl2
-rw-r--r--challenge-106/cheok-yin-fung/perl/ch-1.pl41
-rw-r--r--challenge-106/cheok-yin-fung/perl/ch-2.pl97
3 files changed, 139 insertions, 1 deletions
diff --git a/challenge-105/cheok-yin-fung/perl/ch-1.pl b/challenge-105/cheok-yin-fung/perl/ch-1.pl
index 3a8ab8c2f5..b5b5f930e9 100644
--- a/challenge-105/cheok-yin-fung/perl/ch-1.pl
+++ b/challenge-105/cheok-yin-fung/perl/ch-1.pl
@@ -13,7 +13,7 @@ my $pow = 1;
my $N = int $ARGV[0];
my $k = $ARGV[1];
-print "WARN: Recommend to take the result from Newton's method if two methods dispute";
+print "WARN: Recommend to take the result from Newton's method if two methods dispute\n";
print "WARN: N is large; probably dispute between two methods \n"
if $N > 9; # parameter chosen by testing
print "WARN: N is too large; probably inaccurate result(s) \n"
diff --git a/challenge-106/cheok-yin-fung/perl/ch-1.pl b/challenge-106/cheok-yin-fung/perl/ch-1.pl
new file mode 100644
index 0000000000..31d753c32b
--- /dev/null
+++ b/challenge-106/cheok-yin-fung/perl/ch-1.pl
@@ -0,0 +1,41 @@
+#!/usr/bin/perl
+# The Weekly Challenge 106
+# Task 1 Maximum Gap
+# Usage: ch-1.pl @N
+
+use strict;
+use warnings;
+
+my @N = @ARGV;
+
+my $max_dif = 0;
+
+if ($#N != 0) {selection_sort();}
+
+print $max_dif, "\n";
+
+sub max_ind {
+ my @arr = @_;
+ my $max = $arr[0];
+ my $max_i = 0;
+ for my $i (1..$#arr) {
+ if ($arr[$i] > $max) {
+ $max_i = $i;
+ $max = $arr[$i];
+ }
+ }
+ return $max_i;
+}
+
+sub selection_sort {
+ swap_arr_N(0, max_ind(@N));
+ for my $i (1..$#N-1) {
+ swap_arr_N($i, $i+max_ind(@N[$i..$#N]));
+ my $dif = $N[$i-1] - $N[$i];
+ $max_dif = $dif if $dif > $max_dif;
+ }
+}
+
+sub swap_arr_N {
+ ($N[$_[0]], $N[$_[1]]) = ($N[$_[1]], $N[$_[0]]) if $_[0]!=$_[1];
+}
diff --git a/challenge-106/cheok-yin-fung/perl/ch-2.pl b/challenge-106/cheok-yin-fung/perl/ch-2.pl
new file mode 100644
index 0000000000..943e9a8e38
--- /dev/null
+++ b/challenge-106/cheok-yin-fung/perl/ch-2.pl
@@ -0,0 +1,97 @@
+#!/usr/bin/perl
+# The Weekly Challenge Task 2
+# Decimal String
+# Usage: ch-2.pl $N $D
+use strict;
+use warnings;
+
+sub min {
+ return $_[0] > $_[1] ? $_[1] : $_[0];
+}
+sub max {
+ return $_[0] < $_[1] ? $_[1] : $_[0];
+}
+
+#089 task except
+sub gcd {
+ my ($a,$b) = ($_[0], $_[1]);
+ ($a, $b) = ($b, $a) if $a < $b;
+ return !($a % $b) ? $b : gcd($b , $a % $b);
+}
+
+my $pow_two = 0;
+my $pow_five = 0;
+my $z;
+
+#049 task except modified
+sub is_terminating {
+ $z = $_[0];
+ while ( $z % 2 == 0) {
+ $z /= 2;
+ $pow_two++;
+ }
+ while ( $z % 5 == 0) {
+ $z /= 5;
+ $pow_five++;
+ }
+ return ($z == 1);
+}
+
+my ($N, $D) = ($ARGV[0], $ARGV[1]);
+
+die "Please input numerator and denominator." if (!defined $N);
+print "0\n" if $N == 0;
+exit if $N == 0;
+die "Please input denominator." if (!defined $D);
+die "NaN\n" if $D == 0;
+
+my $common = gcd($N,$D);
+my ($n, $d) = ($N/$common, $D/$common);
+
+if (is_terminating($d)) {print(($n)/($d+0.0),"\n"); exit;}
+
+my $pow_max = max($pow_two,$pow_five);
+my $pow_min = min($pow_two,$pow_five);
+my $pow_dif = $pow_max-$pow_min;
+my $remain = 0;
+my $after_dot_zeros = "0" x $pow_min;
+$d /= 10**$pow_min;
+
+my $_n = $n;
+my $extend = 1;
+if ($pow_dif > 0) {
+ $extend = (2**($pow_two-$pow_min)) * (5**($pow_five-$pow_min));
+ $n *= $extend;
+ $d *= $extend;
+ $_n *= $extend;
+}
+
+my $integral_part = int ($n / $d);
+
+my $new = $n % $d;
+
+my $checker = undef;
+my @cd = ($new,);
+my $i = 0;
+my $j;
+my @after_dot;
+
+do {
+ $new = $new * 10;
+ push @after_dot , int $new / $d;
+ $remain = $new % $d;
+ push @cd, $remain;
+ $new = $remain;
+ $i++;
+ $j = 0;
+ while ($j < $i && !$checker) {
+ $checker = ($cd[$j] == $remain) || $checker;
+ $j++ if !$checker;
+ }
+} while (!$checker);
+
+
+print $integral_part;
+print "." , $after_dot_zeros;
+if ($j != 0) {print @after_dot[0..$j-1];}
+print "(",@after_dot[$j..$i-1],")", "\n";