aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-198/perlboy1967/perl/ch-1.pl46
-rwxr-xr-xchallenge-198/perlboy1967/perl/ch-2.pl38
2 files changed, 84 insertions, 0 deletions
diff --git a/challenge-198/perlboy1967/perl/ch-1.pl b/challenge-198/perlboy1967/perl/ch-1.pl
new file mode 100755
index 0000000000..23ae52616c
--- /dev/null
+++ b/challenge-198/perlboy1967/perl/ch-1.pl
@@ -0,0 +1,46 @@
+#!/bin/perl
+
+=pod
+
+The Weekly Challenge - 197
+- https://theweeklychallenge.org/blog/perl-weekly-challenge-197/#TASK1
+
+Author: Niels 'PerlBoy' van Dijke
+
+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.
+
+=cut
+
+use v5.16;
+use common::sense;
+
+use List::Util qw(max);
+use List::MoreUtils qw(slide);
+
+use Test::More;
+
+
+sub maxGap {
+ return 0 if (scalar @_ < 2);
+
+ my @l = sort { $a <=> $b } @_;
+
+ # Find maxGap size
+ my $maxGap = max slide { $b - $a } @l;
+
+ # Find and count maxGap pairs
+ scalar grep { $_ } slide { $b - $a == $maxGap } @l;
+}
+
+
+is(maxGap(2,5,8,1),2);
+is(maxGap(3),0);
+is(maxGap(1,1,2,2,3,3),2);
+
+done_testing;
diff --git a/challenge-198/perlboy1967/perl/ch-2.pl b/challenge-198/perlboy1967/perl/ch-2.pl
new file mode 100755
index 0000000000..5be954ce0c
--- /dev/null
+++ b/challenge-198/perlboy1967/perl/ch-2.pl
@@ -0,0 +1,38 @@
+#!/bin/perl
+
+=pod
+
+The Weekly Challenge - 198
+- https://theweeklychallenge.org/blog/perl-weekly-challenge-198/#TASK2
+
+Author: Niels 'PerlBoy' van Dijke
+
+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.
+
+=cut
+
+use v5.16;
+use common::sense;
+
+use Math::Prime::XS qw(primes);
+
+use Test::More;
+
+
+sub primeCount ($) {
+ my @p = primes($_[0] - 1);
+ return scalar @p;
+}
+
+
+is(primeCount(10),4);
+is(primeCount(15),6);
+is(primeCount(1),0);
+is(primeCount(25),9);
+
+done_testing;