aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-198/jeanluc2020/blog-1.txt1
-rw-r--r--challenge-198/jeanluc2020/blog-2.txt1
-rwxr-xr-xchallenge-198/jeanluc2020/perl/ch-1.pl62
-rwxr-xr-xchallenge-198/jeanluc2020/perl/ch-2.pl65
4 files changed, 129 insertions, 0 deletions
diff --git a/challenge-198/jeanluc2020/blog-1.txt b/challenge-198/jeanluc2020/blog-1.txt
new file mode 100644
index 0000000000..4c11c54f7a
--- /dev/null
+++ b/challenge-198/jeanluc2020/blog-1.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-198-1.html
diff --git a/challenge-198/jeanluc2020/blog-2.txt b/challenge-198/jeanluc2020/blog-2.txt
new file mode 100644
index 0000000000..0b29407527
--- /dev/null
+++ b/challenge-198/jeanluc2020/blog-2.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-198-2.html
diff --git a/challenge-198/jeanluc2020/perl/ch-1.pl b/challenge-198/jeanluc2020/perl/ch-1.pl
new file mode 100755
index 0000000000..0f9d904851
--- /dev/null
+++ b/challenge-198/jeanluc2020/perl/ch-1.pl
@@ -0,0 +1,62 @@
+#!/usr/bin/perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-198/#TASK1
+#
+# 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.
+#
+## 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)
+#
+## Example 2
+##
+## Input: @list = (3)
+## Output: 0
+#
+### This is a pretty straightforward problem. Let's just walk the array,
+### calculate the gap of 2 consecutive values, remember this instance, and in
+### the end display the value for the maximum of the gaps
+
+use strict;
+use warnings;
+use feature 'say';
+use List::Util qw(max);
+
+# some examples of lists
+my $lists = [
+ [ 2, 5, 8, 1 ],
+ [ 3 ],
+ [ 1, 2, 3, 5, 7, 9, 13, 17],
+ [ 1, 3, 7, 4, 9, 11 ]
+];
+
+# let's generate the output for all examples
+foreach my $list ( @$lists ) {
+ say "(" . join(", ", @$list) . ") returns " . max_gap(@$list);
+}
+
+# let's calculate the gaps
+sub max_gap {
+ my @list = sort { $a <=> $b } @_;
+ my $gaps;
+ # if the list contains less than 2 elements then return 0
+ return 0 if scalar(@list) < 2;
+ my $last = undef;
+ foreach my $elem (@list) {
+ if(defined($last)) {
+ # we're at least at the second element, so let's calculate the gap
+ # to the previous element and count this instance
+ $gaps->{ $elem - $last }++;
+ }
+ $last = $elem;
+ }
+ # return the number of instances for the max gap
+ return $gaps->{ max(keys %$gaps) };
+}
+
diff --git a/challenge-198/jeanluc2020/perl/ch-2.pl b/challenge-198/jeanluc2020/perl/ch-2.pl
new file mode 100755
index 0000000000..1da1592347
--- /dev/null
+++ b/challenge-198/jeanluc2020/perl/ch-2.pl
@@ -0,0 +1,65 @@
+#!/usr/bin/perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-198/#TASK2
+#
+# You are given an integer $n > 0.
+#
+# Write a script to print the count of primes less than $n.
+#
+## Example 1
+##
+## Input: $n = 10
+## Output: 4 as in there are 4 primes less than 10 are 2, 3, 5 ,7.
+#
+## Example 2
+##
+## Input: $n = 15
+## Output: 6
+#
+## Example 3
+##
+## Input: $n = 1
+## Output: 0
+#
+## Example 4
+##
+## Input: $n = 25
+## Output: 9
+#
+### This is a nice little task, so let's just simply count all the primes
+### up to $n-1 (since we want to count primes that are less than $n)
+### To do that, just check each of the integers up to $n whether it is
+### prime or not and increment a counter if it is
+
+use strict;
+use warnings;
+use feature 'say';
+
+my @examples = (10, 15, 1, 25, 23, 30, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000);
+
+foreach my $n (@examples) {
+ say "There are " . count_primes($n) . " primes < $n";
+}
+
+# count all the primes < $n
+sub count_primes {
+ my $n = shift;
+ my $count = 0;
+ # since we're only interested in primes less than $n, we only look for
+ # primes up to $n-1. Should $n be a prime itself, we don't count it
+ foreach my $i (1..$n-1) {
+ $count++ if is_prime($i); # increment $count in case we have a prime
+ }
+ return $count;
+}
+
+# helper function to check wether an integer is a prime
+sub is_prime {
+ my $n = shift;
+ return 0 if $n < 2; # 1 isn't prime
+ foreach(2..sqrt($n)) {
+ # if any integer < sqrt($n) divides $n, we don't have a prime
+ return 0 unless $n % $_;
+ }
+ # since we didn't find any integer that divides $n, we have a prime
+ return 1;
+}