diff options
| author | wanderdoc <wanderdoc@googlemail.com> | 2021-04-02 14:59:11 +0200 |
|---|---|---|
| committer | wanderdoc <wanderdoc@googlemail.com> | 2021-04-02 14:59:11 +0200 |
| commit | 4d0fd3466e8c56b6f2f7775f2e8dc898e8cef4d9 (patch) | |
| tree | e18c4418cc4be798915cb83c7903f05fb3b1fee9 /challenge-106 | |
| parent | fa32da963e308b7fa2b8ddef03f840f81411f7fd (diff) | |
| download | perlweeklychallenge-club-4d0fd3466e8c56b6f2f7775f2e8dc898e8cef4d9.tar.gz perlweeklychallenge-club-4d0fd3466e8c56b6f2f7775f2e8dc898e8cef4d9.tar.bz2 perlweeklychallenge-club-4d0fd3466e8c56b6f2f7775f2e8dc898e8cef4d9.zip | |
Solutions to challenge-106
Diffstat (limited to 'challenge-106')
| -rw-r--r-- | challenge-106/wanderdoc/perl/ch-1.pl | 36 | ||||
| -rw-r--r-- | challenge-106/wanderdoc/perl/ch-2.pl | 82 |
2 files changed, 118 insertions, 0 deletions
diff --git a/challenge-106/wanderdoc/perl/ch-1.pl b/challenge-106/wanderdoc/perl/ch-1.pl new file mode 100644 index 0000000000..6e19538efd --- /dev/null +++ b/challenge-106/wanderdoc/perl/ch-1.pl @@ -0,0 +1,36 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +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 +=cut + + + + + +use List::Util qw(max); +use Test::More; + + +sub max_gap +{ + my @arr = @_; + return 0 if scalar @arr < 2; + @arr = sort {$b <=> $a} @arr; + + my $gap = max( map { $arr[$_] - $arr[$_ + 1] } 0 .. $#arr - 1 ); + return $gap; +} + + +is(max_gap(2, 9, 3, 5), 4, 'Example 1'); +is(max_gap(1, 3, 8, 2, 0), 5, 'Example 2'); +is(max_gap(5), 0, 'Example 3'); + +done_testing();
\ No newline at end of file diff --git a/challenge-106/wanderdoc/perl/ch-2.pl b/challenge-106/wanderdoc/perl/ch-2.pl new file mode 100644 index 0000000000..a95e701b9e --- /dev/null +++ b/challenge-106/wanderdoc/perl/ch-2.pl @@ -0,0 +1,82 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +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)" +=cut + + + + + + + +use Test::More; + +sub long_div +{ + my ($n, $d) = @_; + + die "Division by zero!$/" if $d == 0; + + my $int = int $n / $d; + my $rest = $n % $d; + return $int if 0 == $rest; + my $precision = $d * 3; + my @output; + + + while ( scalar @output < $precision ) + { + my $temp = $rest * 10; # print "Temp: $temp$/"; + my $dig = int ($temp / $d); + push @output, $dig; + + + $rest = $temp % $d; + return join('.', $int, join('', @output)) if 0 == $rest; + } + + my $float = join('', @output); + my @repeating = $float =~ /([0-9]+?)(?=\g1+)/g; + + my %rep; + + $rep{$_}++ for @repeating; + my @keys = sort { $b <=> $a } keys %rep; + my $group = $keys[0]; + my $group_re = qr/$group/; + + my $head = ''; + if ($float =~ /^([0-9]*?)?${group_re}/ ) + { + + $head = $1; + } + return join('.', $int, join('', $head, "(${group})")); +} + + + + + +is(long_div(1,2), 0.5, '1/2'); +is(long_div(1,3), '0.(3)', '1/3'); +is(long_div(1,6), '0.1(6)', '1/6'); +is(long_div(1,7), '0.(142857)', '1/7'); +is(long_div(1,11), '0.(09)', '1/11'); + +is(long_div(1,19), '0.(052631578947368421)', '1/19'); +is(long_div(1,29), '0.(0344827586206896551724137931)', '1/29'); + +is(long_div(1,46), '0.0(2173913043478260869565)', '1/46'); +is(long_div(1,97), '0.(010309278350515463917525773195876288659793814432989690721649484536082474226804123711340206185567)', '1/97'); +is(long_div(5,66), '0.0(75)', '5/66'); +is(long_div(5,12), '0.41(6)', '5/12'); +is(long_div(109,110), '0.9(90)', '109/110'); +done_testing;
\ No newline at end of file |
