aboutsummaryrefslogtreecommitdiff
path: root/challenge-106
diff options
context:
space:
mode:
authorE7-87-83 <fungcheokyin@gmail.com>2021-04-05 04:14:31 +0800
committerE7-87-83 <fungcheokyin@gmail.com>2021-04-05 04:14:31 +0800
commit0594f4a7e117026ec4eee4bf2de65cecf03f4396 (patch)
tree56216f9ea9b6211fedc0f44d1b1be68cb0e1c63e /challenge-106
parentd5ff10db3ea3e9e6e150316af0d4e4c0e2a37fe1 (diff)
downloadperlweeklychallenge-club-0594f4a7e117026ec4eee4bf2de65cecf03f4396.tar.gz
perlweeklychallenge-club-0594f4a7e117026ec4eee4bf2de65cecf03f4396.tar.bz2
perlweeklychallenge-club-0594f4a7e117026ec4eee4bf2de65cecf03f4396.zip
challenges
Diffstat (limited to 'challenge-106')
-rw-r--r--challenge-106/cheok-yin-fung/perl/ch-1.pl41
-rw-r--r--challenge-106/cheok-yin-fung/perl/ch-2.pl97
2 files changed, 138 insertions, 0 deletions
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";