aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-241/jeanluc2020/blog-1.txt1
-rw-r--r--challenge-241/jeanluc2020/blog-2.txt1
-rwxr-xr-xchallenge-241/jeanluc2020/perl/ch-1.pl69
-rwxr-xr-xchallenge-241/jeanluc2020/perl/ch-2.pl63
-rwxr-xr-xchallenge-241/jeanluc2020/python/ch-1.py60
-rwxr-xr-xchallenge-241/jeanluc2020/python/ch-2.py61
6 files changed, 255 insertions, 0 deletions
diff --git a/challenge-241/jeanluc2020/blog-1.txt b/challenge-241/jeanluc2020/blog-1.txt
new file mode 100644
index 0000000000..a86833251a
--- /dev/null
+++ b/challenge-241/jeanluc2020/blog-1.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-241-1.html
diff --git a/challenge-241/jeanluc2020/blog-2.txt b/challenge-241/jeanluc2020/blog-2.txt
new file mode 100644
index 0000000000..6ada722d0c
--- /dev/null
+++ b/challenge-241/jeanluc2020/blog-2.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-241-2.html
diff --git a/challenge-241/jeanluc2020/perl/ch-1.pl b/challenge-241/jeanluc2020/perl/ch-1.pl
new file mode 100755
index 0000000000..6ce8c5dffa
--- /dev/null
+++ b/challenge-241/jeanluc2020/perl/ch-1.pl
@@ -0,0 +1,69 @@
+#!/usr/bin/perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-241/#TASK1
+#
+# Task 1: Arithmetic Triplets
+# ===========================
+#
+# You are given an array (3 or more members) of integers in increasing order
+# and a positive integer.
+#
+# Write a script to find out the number of unique Arithmetic Triplets
+# satisfying the following rules:
+#
+# a) i < j < k
+# b) nums[j] - nums[i] == diff
+# c) nums[k] - nums[j] == diff
+#
+## Example 1
+##
+## Input: @nums = (0, 1, 4, 6, 7, 10)
+## $diff = 3
+## Output: 2
+##
+## Index (1, 2, 4) is an arithmetic triplet because both 7 - 4 == 3 and 4 - 1 == 3.
+## Index (2, 4, 5) is an arithmetic triplet because both 10 - 7 == 3 and 7 - 4 == 3.
+#
+## Example 2
+##
+## Input: @nums = (4, 5, 6, 7, 8, 9)
+## $diff = 2
+## Output: 2
+##
+## (0, 2, 4) is an arithmetic triplet because both 8 - 6 == 2 and 6 - 4 == 2.
+## (1, 3, 5) is an arithmetic triplet because both 9 - 7 == 2 and 7 - 5 == 2.
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Looping over the indices i, j, k we just check the condition.
+# We can even short-curcuit the inner loop because we only need
+# to execute it if $nums[$j] - $nums[$i] == $diff.
+
+use strict;
+use warnings;
+
+arithmetic_triplets( [0, 1, 4, 6, 7, 10], 3 );
+arithmetic_triplets( [4, 5, 6, 7, 8, 9], 2);
+
+sub arithmetic_triplets {
+ my ($nums, $diff) = @_;
+ my @nums = @$nums;
+ my $result = 0;
+ print "Input: (" . join(", ", @nums) . "), $diff\n";
+ foreach my $i (0..$#nums) {
+ foreach my $j ($i+1..$#nums) {
+ if($nums[$j] - $nums[$i] == $diff) {
+ foreach my $k ($j+1..$#nums) {
+ if($nums[$k] - $nums[$j] == $diff) {
+ $result++;
+ }
+ }
+ }
+ }
+ }
+ print "Output: $result\n";
+}
+
diff --git a/challenge-241/jeanluc2020/perl/ch-2.pl b/challenge-241/jeanluc2020/perl/ch-2.pl
new file mode 100755
index 0000000000..f9a9dcbd36
--- /dev/null
+++ b/challenge-241/jeanluc2020/perl/ch-2.pl
@@ -0,0 +1,63 @@
+#!/usr/bin/perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-241/#TASK2
+#
+# Task 2: Prime Order
+# ===================
+#
+# You are given an array of unique positive integers greater than 2.
+#
+# Write a script to sort them in ascending order of the count of their prime
+# factors, tie-breaking by ascending value.
+#
+## Example 1
+##
+## Input: @int = (11, 8, 27, 4)
+## Output: (11, 4, 8, 27))
+##
+## Prime factors of 11 => 11
+## Prime factors of 4 => 2, 2
+## Prime factors of 8 => 2, 2, 2
+## Prime factors of 27 => 3, 3, 3
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Sort by number of prime factors first, then by the number
+# itself second.
+# We calculate the number of prime factors in an extra recursive
+# function, using a cache of already calculated numbers to mitigate
+# unnecessary recalculations
+#
+use strict;
+use warnings;
+
+prime_order(11, 8, 27, 4);
+
+sub prime_order {
+ my @int = @_;
+ print "Input: (" . join(", ", @int) . ")\n";
+ my @result = sort { prime_factor_count($a) <=> prime_factor_count($b) || $a <=> $b } @int;
+ print "Output: (" . join(", ", @result) . ")\n";
+}
+
+{
+ my $cache = {};
+ sub prime_factor_count {
+ my $number = shift;
+ my $count = 0;
+ unless($cache->{$number}) {
+ foreach my $i (2..$number) {
+ if($number % $i == 0) {
+ $count = 1 + prime_factor_count($number / $i);
+ last;
+ }
+ }
+ $cache->{$number} = $count;
+ }
+
+ return $cache->{$number};
+ }
+}
diff --git a/challenge-241/jeanluc2020/python/ch-1.py b/challenge-241/jeanluc2020/python/ch-1.py
new file mode 100755
index 0000000000..4a96800f8d
--- /dev/null
+++ b/challenge-241/jeanluc2020/python/ch-1.py
@@ -0,0 +1,60 @@
+#!/usr/bin/python3
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-241/#TASK1
+#
+# Task 1: Arithmetic Triplets
+# ===========================
+#
+# You are given an array (3 or more members) of integers in increasing order
+# and a positive integer.
+#
+# Write a script to find out the number of unique Arithmetic Triplets
+# satisfying the following rules:
+#
+# a) i < j < k
+# b) nums[j] - nums[i] == diff
+# c) nums[k] - nums[j] == diff
+#
+## Example 1
+##
+## Input: @nums = (0, 1, 4, 6, 7, 10)
+## $diff = 3
+## Output: 2
+##
+## Index (1, 2, 4) is an arithmetic triplet because both 7 - 4 == 3 and 4 - 1 == 3.
+## Index (2, 4, 5) is an arithmetic triplet because both 10 - 7 == 3 and 7 - 4 == 3.
+#
+## Example 2
+##
+## Input: @nums = (4, 5, 6, 7, 8, 9)
+## $diff = 2
+## Output: 2
+##
+## (0, 2, 4) is an arithmetic triplet because both 8 - 6 == 2 and 6 - 4 == 2.
+## (1, 3, 5) is an arithmetic triplet because both 9 - 7 == 2 and 7 - 5 == 2.
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Looping over the indices i, j, k we just check the condition.
+# We can even short-curcuit the inner loop because we only need
+# to execute it if $nums[$j] - $nums[$i] == $diff.
+
+
+def arithmetic_triplets(nums: list, diff: int):
+ print("Input: (", ", ".join(str(x) for x in nums), "), ", diff, sep="")
+ result = 0
+ for i in range(len(nums)):
+ for j in range(i+1, len(nums)):
+ if nums[j] - nums[i] == diff:
+ for k in range(j+1, len(nums)):
+ if nums[k] - nums[j] == diff:
+ result += 1
+ print("Output:", result)
+
+arithmetic_triplets( [0, 1, 4, 6, 7, 10], 3 );
+arithmetic_triplets( [4, 5, 6, 7, 8, 9], 2);
+
+
diff --git a/challenge-241/jeanluc2020/python/ch-2.py b/challenge-241/jeanluc2020/python/ch-2.py
new file mode 100755
index 0000000000..507b88260b
--- /dev/null
+++ b/challenge-241/jeanluc2020/python/ch-2.py
@@ -0,0 +1,61 @@
+#!/usr/bin/python3
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-241/#TASK2
+#
+# Task 2: Prime Order
+# ===================
+#
+# You are given an array of unique positive integers greater than 2.
+#
+# Write a script to sort them in ascending order of the count of their prime
+# factors, tie-breaking by ascending value.
+#
+## Example 1
+##
+## Input: @int = (11, 8, 27, 4)
+## Output: (11, 4, 8, 27))
+##
+## Prime factors of 11 => 11
+## Prime factors of 4 => 2, 2
+## Prime factors of 8 => 2, 2, 2
+## Prime factors of 27 => 3, 3, 3
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Sort by number of prime factors first, then by the number
+# itself second.
+# We calculate the number of prime factors in an extra recursive
+# function, using a cache of already calculated numbers to mitigate
+# unnecessary recalculations
+#
+
+from operator import itemgetter
+
+def prime_factor_count(number: int) -> int:
+ cache = {}
+ def prime_factor_count_interal(num: int) -> int:
+ count = 0
+ if num not in cache.keys():
+ for i in range(2, num+1):
+ if num % i == 0:
+ count = 1 + prime_factor_count_interal(int(num / i))
+ break
+ cache[num] = count
+ return cache[num]
+ return prime_factor_count_interal(number)
+
+
+def prime_order(nums: list):
+ print("Input: (", ", ".join(str(x) for x in nums), ")", sep='')
+ decorated = [ (prime_factor_count(x), x) for x in nums ]
+ decorated_result = sorted(decorated, key=lambda x: (x[0], x[1]))
+ result = [ x[1] for x in decorated_result ]
+ print("Output: (", ", ".join(str(x) for x in result), ")", sep='')
+
+
+prime_order([11, 8, 27, 4])
+prime_order([11, 27, 8, 4])
+