From b896a71040c1e32394257fcb53f3e0c5a845829a Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Fri, 2 Apr 2021 23:50:36 +0100 Subject: Add Perl solution to challenge 106 --- challenge-106/paulo-custodio/perl/ch-1.pl | 40 +++++++++++++++++++++++++++ challenge-106/paulo-custodio/perl/ch-2.pl | 44 ++++++++++++++++++++++++++++++ challenge-106/paulo-custodio/t/test-1.yaml | 30 ++++++++++++++++++++ challenge-106/paulo-custodio/t/test-2.yaml | 20 ++++++++++++++ challenge-106/paulo-custodio/test.pl | 7 +++++ 5 files changed, 141 insertions(+) create mode 100644 challenge-106/paulo-custodio/perl/ch-1.pl create mode 100644 challenge-106/paulo-custodio/perl/ch-2.pl create mode 100644 challenge-106/paulo-custodio/t/test-1.yaml create mode 100644 challenge-106/paulo-custodio/t/test-2.yaml create mode 100644 challenge-106/paulo-custodio/test.pl 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'; -- cgit