aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpme <hauptadler@gmail.com>2024-09-15 10:38:39 +0200
committerpme <hauptadler@gmail.com>2024-09-15 10:38:39 +0200
commitcb680d712240bdcd154d60c8f2f32b448df1f9d7 (patch)
tree8823a875a3e42ad4ba7c4072b10794e531540acb
parent58a895e96457e85910cfb9c58b2c329eabc41502 (diff)
downloadperlweeklychallenge-club-cb680d712240bdcd154d60c8f2f32b448df1f9d7.tar.gz
perlweeklychallenge-club-cb680d712240bdcd154d60c8f2f32b448df1f9d7.tar.bz2
perlweeklychallenge-club-cb680d712240bdcd154d60c8f2f32b448df1f9d7.zip
challenge-198
-rwxr-xr-xchallenge-198/peter-meszaros/perl/ch-1.pl55
-rwxr-xr-xchallenge-198/peter-meszaros/perl/ch-2.pl65
2 files changed, 120 insertions, 0 deletions
diff --git a/challenge-198/peter-meszaros/perl/ch-1.pl b/challenge-198/peter-meszaros/perl/ch-1.pl
new file mode 100755
index 0000000000..4b19255cac
--- /dev/null
+++ b/challenge-198/peter-meszaros/perl/ch-1.pl
@@ -0,0 +1,55 @@
+#!/usr/bin/env perl
+#
+=head1 Task 1: Max Gap
+
+Submitted by: Mohammad S Anwar
+
+You are given a list of integers, @list.
+
+Write a script to find the total pairs in the sorted list where 2 consecutive
+elements has the max gap. If the list contains less then 2 elements then return
+0.
+
+=head2 Example 1
+
+ Input: @list = (2,5,8,1)
+ Output: 2
+
+ Since the sorted list (1,2,5,8) has 2 such pairs (2,5) and (5,8)
+
+=head2 Example 2
+
+ Input: @list = (3)
+ Output: 0
+
+=cut
+
+use strict;
+use warnings;
+use Test2::V0 -no_srand => 1;
+use Data::Dumper;
+use List::Util qw/max/;
+
+my $cases = [
+ [[2, 5, 8, 1], 2, 'Example 1'],
+ [[3], 0, 'Example 2'],
+];
+
+sub max_gap
+{
+ my $l = shift;
+
+ my %h;
+ my @ls = sort {$a <=> $b} @$l;
+ for my $i (1 .. $#ls) {
+ ++$h{$ls[$i] - $ls[$i-1]};
+ }
+ return %h ? $h{max(keys %h)} : 0;
+}
+
+for (@$cases) {
+ is(max_gap($_->[0]), $_->[1], $_->[2]);
+}
+done_testing();
+
+exit 0;
diff --git a/challenge-198/peter-meszaros/perl/ch-2.pl b/challenge-198/peter-meszaros/perl/ch-2.pl
new file mode 100755
index 0000000000..69e59433ee
--- /dev/null
+++ b/challenge-198/peter-meszaros/perl/ch-2.pl
@@ -0,0 +1,65 @@
+#!/usr/bin/env perl
+#
+=head1 Task 2: Prime Count
+
+Submitted by: Mohammad S Anwar
+
+You are given an integer $n > 0.
+
+Write a script to print the count of primes less than $n.
+
+=head2 Example 1
+
+ Input: $n = 10
+ Output: 4 as in there are 4 primes less than 10 are 2, 3, 5 ,7.
+
+=head2 Example 2
+
+ Input: $n = 15
+ Output: 6
+
+=head2 Example 3
+
+ Input: $n = 1
+ Output: 0
+
+=head2 Example 4
+
+ Input: $n = 25
+ Output: 9
+
+=cut
+
+use strict;
+use warnings;
+use Test2::V0 -no_srand => 1;
+use Data::Dumper;
+
+my $cases = [
+ [10, 4, 'Example 1'],
+ [15, 6, 'Example 2'],
+ [ 1, 0, 'Example 3'],
+ [25, 9, 'Example 4'],
+];
+
+# https://rosettacode.org/wiki/Sieve_of_Eratosthenes#Perl
+sub gen_prime {
+ my $p = shift;
+ return $p, $p**2 > $_[$#_] ? @_ : gen_prime(grep $_ % $p, @_)
+}
+
+sub prime_count
+{
+ my $n = shift;
+
+ my @p = gen_prime 2 .. $n if $n > 2;
+
+ return scalar @p;
+}
+
+for (@$cases) {
+ is(prime_count($_->[0]), $_->[1], $_->[2]);
+}
+done_testing();
+
+exit 0;