aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-01-08 16:53:56 +0000
committerGitHub <noreply@github.com>2023-01-08 16:53:56 +0000
commit667abb2fd66e7410d84a5750df7d4e078990b49a (patch)
tree061a13b194bb89848d76df3b3ec98a32f3049bfd
parent30bc90811147ec3a3ccfefa2dff445fee9225d9d (diff)
parent786133bea7817204b86839b48bda3a90c3885a2d (diff)
downloadperlweeklychallenge-club-667abb2fd66e7410d84a5750df7d4e078990b49a.tar.gz
perlweeklychallenge-club-667abb2fd66e7410d84a5750df7d4e078990b49a.tar.bz2
perlweeklychallenge-club-667abb2fd66e7410d84a5750df7d4e078990b49a.zip
Merge pull request #7370 from Solathian/branch-for-challenge-198
adding files for challenge 198
-rw-r--r--challenge-198/solathian/perl/ch-1.pl45
-rw-r--r--challenge-198/solathian/perl/ch-2.pl40
2 files changed, 85 insertions, 0 deletions
diff --git a/challenge-198/solathian/perl/ch-1.pl b/challenge-198/solathian/perl/ch-1.pl
new file mode 100644
index 0000000000..59cc0e5f35
--- /dev/null
+++ b/challenge-198/solathian/perl/ch-1.pl
@@ -0,0 +1,45 @@
+#!usr/bin/perl
+use v5.32;
+use warnings;
+
+use feature 'signatures';
+no warnings 'experimental'; # signatures, smartmatch
+
+# Challange 198 - 1 - Max Gap
+# 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.
+
+
+maxGap((2,5,8,1)); # (2,5) and (5,8)
+maxGap((2,5,8,1,20)); # (8, 20)
+maxGap((3)); # 0
+
+sub maxGap(@list)
+{
+ @list = sort{$a <=> $b} @list;
+
+ if(@list < 2)
+ {
+ say "0";
+ }
+ else
+ {
+ my $maxGap = 0;
+
+ for(my $i = 0; $i < $#list; $i++)
+ {
+ my $gap = $list[$i+1] - $list[$i];
+ $maxGap = $gap if($gap > $maxGap);
+ }
+
+ for(my $i = 0; $i < $#list; $i++)
+ {
+ my $gap = $list[$i+1] - $list[$i];
+
+ print("($list[$i], $list[$i+1])") if($gap == $maxGap);
+ }
+
+ }
+ say"";
+} \ No newline at end of file
diff --git a/challenge-198/solathian/perl/ch-2.pl b/challenge-198/solathian/perl/ch-2.pl
new file mode 100644
index 0000000000..2802ff6c67
--- /dev/null
+++ b/challenge-198/solathian/perl/ch-2.pl
@@ -0,0 +1,40 @@
+#!usr/bin/perl
+use v5.32;
+use warnings;
+
+use Math::Prime::Util 'is_prime'; # to import all functions ':all'
+
+use feature 'signatures';
+no warnings 'experimental'; # signatures, smartmatch
+
+# Challange 198 - 2 - Prime Count
+# You are given an integer $n > 0.
+# Write a script to print the count of primes less than $n.
+
+
+primeCount(10); # 4
+primeCount(15); # 6
+primeCount(1); # 0
+primeCount(25); # 9
+
+
+sub primeCount($limit)
+{
+ my @primeList;
+
+ for(my $i = 1; $i < $limit; $i++)
+ {
+ push(@primeList, $i) if(is_prime($i))
+ }
+
+ if(@primeList > 0)
+ {
+ say join("","The ", 0+@primeList , " primes which are less than $limit are: ", join(",",@primeList));
+ return scalar @primeList; # just for plain simple return
+ }
+ else
+ {
+ say "0";
+ }
+
+} \ No newline at end of file