diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-04-12 21:17:21 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-04-12 21:17:21 +0100 |
| commit | 621e9c06533a574e60805826d2fb85ce59c9b073 (patch) | |
| tree | 6ae73e8d0b094430c7cb4543e344ed4e3d9354b4 | |
| parent | eeb4cf0caad65ceee5fdddb161be04383ad53428 (diff) | |
| download | perlweeklychallenge-club-621e9c06533a574e60805826d2fb85ce59c9b073.tar.gz perlweeklychallenge-club-621e9c06533a574e60805826d2fb85ce59c9b073.tar.bz2 perlweeklychallenge-club-621e9c06533a574e60805826d2fb85ce59c9b073.zip | |
Add Perl solution to challenge 108
| -rw-r--r-- | challenge-106/paulo-custodio/perl/ch-1.pl | 12 | ||||
| -rw-r--r-- | challenge-106/paulo-custodio/perl/ch-2.pl | 12 | ||||
| -rw-r--r-- | challenge-106/paulo-custodio/t/test-1.yaml | 2 | ||||
| -rw-r--r-- | challenge-108/paulo-custodio/perl/ch-1.pl | 14 | ||||
| -rw-r--r-- | challenge-108/paulo-custodio/perl/ch-2.pl | 67 | ||||
| -rw-r--r-- | challenge-108/paulo-custodio/t/test-2.yaml | 5 | ||||
| -rw-r--r-- | challenge-108/paulo-custodio/test.pl | 7 |
7 files changed, 106 insertions, 13 deletions
diff --git a/challenge-106/paulo-custodio/perl/ch-1.pl b/challenge-106/paulo-custodio/perl/ch-1.pl index 3ffc4020ff..90324d33ea 100644 --- a/challenge-106/paulo-custodio/perl/ch-1.pl +++ b/challenge-106/paulo-custodio/perl/ch-1.pl @@ -5,19 +5,19 @@ # 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 @@ -30,7 +30,7 @@ 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]; diff --git a/challenge-106/paulo-custodio/perl/ch-2.pl b/challenge-106/paulo-custodio/perl/ch-2.pl index 020663f23f..390fa68cb1 100644 --- a/challenge-106/paulo-custodio/perl/ch-2.pl +++ b/challenge-106/paulo-custodio/perl/ch-2.pl @@ -3,17 +3,17 @@ # 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)" @@ -28,12 +28,12 @@ sub decimal { 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\\)/;"; diff --git a/challenge-106/paulo-custodio/t/test-1.yaml b/challenge-106/paulo-custodio/t/test-1.yaml index 7f4993223e..23d76e1249 100644 --- a/challenge-106/paulo-custodio/t/test-1.yaml +++ b/challenge-106/paulo-custodio/t/test-1.yaml @@ -1,6 +1,6 @@ - setup: cleanup: - args: + args: input: output: 0 - setup: diff --git a/challenge-108/paulo-custodio/perl/ch-1.pl b/challenge-108/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..333532278a --- /dev/null +++ b/challenge-108/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,14 @@ +#!/usr/bin/perl + +# Challenge 108 +# +# TASK #1 Locate Memory +# Submitted by: Mohammad S Anwar +# +# Write a script to declare a variable or constant and print its location in +# the memory. + +use Modern::Perl; + +my $var = "hello world"; +say \$var; diff --git a/challenge-108/paulo-custodio/perl/ch-2.pl b/challenge-108/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..d76209a4e8 --- /dev/null +++ b/challenge-108/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,67 @@ +#!/usr/bin/perl + +# Challenge 108 +# +# TASK #2 Bell Numbers +# Submitted by: Mohammad S Anwar +# +# Write a script to display top 10 Bell Numbers. Please refer to wikipedia page +# for more informations. +# +# Example: +# B0: 1 as you can only have one partition of zero element set +# B1: 1 as you can only have one partition of one element set {a}. +# B2: 2 +# {a}{b} +# {a,b} +# B3: 5 +# {a}{b}{c} +# {a,b}{c} +# {a}{b,c} +# {a,c}{b} +# {a,b,c} +# B4: 15 +# {a}{b}{c}{d} +# {a,b,c,d} +# {a,b}{c,d} +# {a,c}{b,d} +# {a,d}{b,c} +# {a,b}{c}{d} +# {a,c}{b}{d} +# {a,d}{b}{c} +# {b,c}{a}{d} +# {b,d}{a}{c} +# {c,d}{a}{b} +# {a}{b,c,d} +# {b}{a,c,d} +# {c}{a,b,d} +# {d}{a,b,c} + +use Modern::Perl; +use Data::Dump 'dump'; + +my $N = shift || 10; +my $bell = bell(); +for (1..$N) { + print $bell->(), " "; +} +print "\n"; + +sub bell { + my $n = -1; + my @bell; + return sub { + $n++; + if ($n==0) { + push @bell, [1]; + return 1; + } + else { + push @bell, [$bell[$n-1][$n-1]]; + for my $i (1..$n) { + $bell[$n][$i] = $bell[$n-1][$i-1] + $bell[$n][$i-1]; + } + return $bell[$n][0]; + } + }; +} diff --git a/challenge-108/paulo-custodio/t/test-2.yaml b/challenge-108/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..db839db250 --- /dev/null +++ b/challenge-108/paulo-custodio/t/test-2.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: + input: + output: 1 1 2 5 15 52 203 877 4140 21147 diff --git a/challenge-108/paulo-custodio/test.pl b/challenge-108/paulo-custodio/test.pl new file mode 100644 index 0000000000..01ed2b83cd --- /dev/null +++ b/challenge-108/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'; |
