aboutsummaryrefslogtreecommitdiff
path: root/challenge-106
diff options
context:
space:
mode:
authorAdam Russell <ac.russell@live.com>2021-04-04 16:11:45 -0400
committerAdam Russell <ac.russell@live.com>2021-04-04 16:11:45 -0400
commit494fbc7656998ee2d8e7709a7cb6f4b288e49a7f (patch)
tree3fc367de3ea291cdfad4ea17b7a24efd0d4d19b6 /challenge-106
parentd5ff10db3ea3e9e6e150316af0d4e4c0e2a37fe1 (diff)
downloadperlweeklychallenge-club-494fbc7656998ee2d8e7709a7cb6f4b288e49a7f.tar.gz
perlweeklychallenge-club-494fbc7656998ee2d8e7709a7cb6f4b288e49a7f.tar.bz2
perlweeklychallenge-club-494fbc7656998ee2d8e7709a7cb6f4b288e49a7f.zip
initial commit
Diffstat (limited to 'challenge-106')
-rw-r--r--challenge-106/adam-russell/blog.txt1
-rw-r--r--challenge-106/adam-russell/perl/ch-1.pl32
-rw-r--r--challenge-106/adam-russell/perl/ch-2.pl71
3 files changed, 104 insertions, 0 deletions
diff --git a/challenge-106/adam-russell/blog.txt b/challenge-106/adam-russell/blog.txt
new file mode 100644
index 0000000000..5ef89335da
--- /dev/null
+++ b/challenge-106/adam-russell/blog.txt
@@ -0,0 +1 @@
+http://www.rabbitfarm.com/cgi-bin/blosxom/perl/2021/04/04
diff --git a/challenge-106/adam-russell/perl/ch-1.pl b/challenge-106/adam-russell/perl/ch-1.pl
new file mode 100644
index 0000000000..58ce11a805
--- /dev/null
+++ b/challenge-106/adam-russell/perl/ch-1.pl
@@ -0,0 +1,32 @@
+use strict;
+use warnings;
+##
+# 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.
+##
+sub max_difference_sorted{
+ my(@sorted) = @_;
+ return 0 if(@sorted == 1);
+ my $x = $sorted[1] - $sorted[0];
+ my $y = max_difference_sorted(@sorted[1 .. @sorted - 1]);
+ return ($x > $y)? $x: $y;
+}
+
+sub max_difference{
+ my (@numbers) = @_;
+ return max_difference_sorted(
+ sort { $a <=> $b } @numbers
+ );
+}
+
+MAIN:{
+ my (@N);
+ @N = (2, 9, 3, 5);
+ print max_difference(@N) . "\n";
+ @N = (1, 3, 8, 2, 0);
+ print max_difference(@N) . "\n";
+ @N = (5);
+ print max_difference(@N) . "\n";
+}
diff --git a/challenge-106/adam-russell/perl/ch-2.pl b/challenge-106/adam-russell/perl/ch-2.pl
new file mode 100644
index 0000000000..7b271ccabe
--- /dev/null
+++ b/challenge-106/adam-russell/perl/ch-2.pl
@@ -0,0 +1,71 @@
+use strict;
+use warnings;
+##
+# 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.
+##
+use boolean;
+
+sub divide{
+ my($n, $d) = @_;
+ my @remainders;
+ my $q = (int($n / $d)) . ".";
+ my $r = $n % $d;
+ push @remainders, $r;
+ my @a;
+ for (0 .. $d){
+ $q .= int($r*10 / $d);
+ $r = $r*10 % $d;
+ @a = grep { $remainders[$_] == $r } (0 .. @remainders - 1);
+ last if(@a);
+ push @remainders, $r;
+ }
+ my $r_i = $a[0];
+ my $i = index($q, ".");
+ my $decimal_part = substr($q, $i+1);
+ return substr($q, 0, $i + 1) . substr($decimal_part, 0, $r_i) . "(" . substr($q, $i + $r_i + 1) . ")";
+}
+
+sub prime_factor{
+ my $x = shift(@_);
+ my @factors;
+ for (my $y = 2; $y <= $x; $y++){
+ next if $x % $y;
+ $x /= $y;
+ push @factors, $y;
+ redo;
+ }
+ return @factors;
+}
+
+sub nd2decimal{
+ my($n, $d) = @_;
+ my $max_repetend = $d - 1;
+ my $repeats = false;
+ my @factors = prime_factor($d);
+ for my $factor (@factors){
+ $repeats = true if($factor != 2 && $factor != 5);
+ }
+ unless($repeats){
+ return sprintf("%0.${max_repetend}g", $n / $d);
+ }
+ else{
+ my $x = divide($n, $d, [], []);
+ return $x;
+ }
+}
+
+MAIN:{
+ my($N, $D);
+ ($N, $D) = (1, 3);
+ print nd2decimal($N, $D) . "\n";
+ ($N, $D) = (1, 2);
+ print nd2decimal($N, $D) . "\n";
+ ($N, $D) = (5, 66);
+ print nd2decimal($N, $D) . "\n";
+ ($N, $D) = (1, 6);
+ print nd2decimal($N, $D) . "\n";
+ ($N, $D) = (1, 8);
+ print nd2decimal($N, $D) . "\n";
+}