aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Köhler <jean-luc@picard.franken.de>2024-03-25 19:20:22 +0100
committerThomas Köhler <jean-luc@picard.franken.de>2024-03-25 19:20:22 +0100
commit8bbac946f3c0bb443b7dfec8edced02d552d60a4 (patch)
tree4dc17ef47354c15d40bd66f80a0d216e3ed9591a
parent041fe9129e3ef4d86df461a0feeee1b3740d5758 (diff)
downloadperlweeklychallenge-club-8bbac946f3c0bb443b7dfec8edced02d552d60a4.tar.gz
perlweeklychallenge-club-8bbac946f3c0bb443b7dfec8edced02d552d60a4.tar.bz2
perlweeklychallenge-club-8bbac946f3c0bb443b7dfec8edced02d552d60a4.zip
Add solution 262.
Signed-off-by: Thomas Köhler <jean-luc@picard.franken.de>
-rw-r--r--challenge-262/jeanluc2020/blog-1.txt1
-rw-r--r--challenge-262/jeanluc2020/blog-2.txt1
-rwxr-xr-xchallenge-262/jeanluc2020/perl/ch-1.pl70
-rwxr-xr-xchallenge-262/jeanluc2020/perl/ch-2.pl60
-rwxr-xr-xchallenge-262/jeanluc2020/python/ch-1.py63
-rwxr-xr-xchallenge-262/jeanluc2020/python/ch-2.py53
6 files changed, 248 insertions, 0 deletions
diff --git a/challenge-262/jeanluc2020/blog-1.txt b/challenge-262/jeanluc2020/blog-1.txt
new file mode 100644
index 0000000000..0545a47517
--- /dev/null
+++ b/challenge-262/jeanluc2020/blog-1.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-262-1.html
diff --git a/challenge-262/jeanluc2020/blog-2.txt b/challenge-262/jeanluc2020/blog-2.txt
new file mode 100644
index 0000000000..68f966dedd
--- /dev/null
+++ b/challenge-262/jeanluc2020/blog-2.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-262-2.html
diff --git a/challenge-262/jeanluc2020/perl/ch-1.pl b/challenge-262/jeanluc2020/perl/ch-1.pl
new file mode 100755
index 0000000000..ec18f5558a
--- /dev/null
+++ b/challenge-262/jeanluc2020/perl/ch-1.pl
@@ -0,0 +1,70 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-262/#TASK1
+#
+# Task 1: Max Positive Negative
+# =============================
+#
+# You are given an array of integers, @ints.
+#
+# Write a script to return the maximum number of either positive or negative
+# integers in the given array.
+#
+## Example 1
+##
+## Input: @ints = (-3, 1, 2, -1, 3, -2, 4)
+## Output: 4
+##
+## Count of positive integers: 4
+## Count of negative integers: 3
+## Maximum of count of positive and negative integers: 4
+#
+## Example 2
+##
+## Input: @ints = (-1, -2, -3, 1)
+## Output: 3
+##
+## Count of positive integers: 1
+## Count of negative integers: 3
+## Maximum of count of positive and negative integers: 3
+#
+## Example 3
+##
+## Input: @ints = (1,2)
+## Output: 2
+##
+## Count of positive integers: 2
+## Count of negative integers: 0
+## Maximum of count of positive and negative integers: 2
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Simply count the negatives and the positives, then return
+# the bigger of the two numbers.
+
+use strict;
+use warnings;
+
+max_positive_negative(-3, 1, 2, -1, 3, -2, 4);
+max_positive_negative(-1, -2, -3, 1);
+max_positive_negative(1,2);
+
+sub max_positive_negative {
+ my @ints = @_;
+ print "Input: (" . join(", ", @ints) . ")\n";
+ my $pos = 0;
+ my $neg = 0;
+ foreach my $num (@ints) {
+ if($num < 0) {
+ $neg++;
+ } else {
+ $pos++;
+ }
+ }
+ my $result = ($pos > $neg) ? $pos : $neg;
+ print "Output: $result\n";
+}
+
diff --git a/challenge-262/jeanluc2020/perl/ch-2.pl b/challenge-262/jeanluc2020/perl/ch-2.pl
new file mode 100755
index 0000000000..96bf2fdec2
--- /dev/null
+++ b/challenge-262/jeanluc2020/perl/ch-2.pl
@@ -0,0 +1,60 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-262/#TASK2
+#
+# Task 2: Count Equal Divisible
+# =============================
+#
+# You are given an array of integers, @ints and an integer $k.
+#
+# Write a script to return the number of pairs (i, j) where
+#
+# a) 0 <= i < j < size of @ints
+# b) ints[i] == ints[j]
+# c) i x j is divisible by k
+#
+## Example 1
+##
+## Input: @ints = (3,1,2,2,2,1,3) and $k = 2
+## Output: 4
+##
+## (0, 6) => ints[0] == ints[6] and 0 x 6 is divisible by 2
+## (2, 3) => ints[2] == ints[3] and 2 x 3 is divisible by 2
+## (2, 4) => ints[2] == ints[4] and 2 x 4 is divisible by 2
+## (3, 4) => ints[3] == ints[4] and 3 x 4 is divisible by 2
+#
+## Example 2
+##
+## Input: @ints = (1,2,3) and $k = 1
+## Output: 0
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Have one index variable go from 0 to size of @ints - 1, then
+# another walk from the first index + 1 to size of @ints - 1. In
+# case ints[i] == ints[j], check if the product of i and j is
+# divisible by $k. Return the sum of all instances where this is true.
+
+use strict;
+use warnings;
+
+count_equal_divisible( [3,1,2,2,2,1,3], 2);
+count_equal_divisible( [1,2,3], 1);
+
+sub count_equal_divisible {
+ my ($tmp, $k) = @_;
+ my @ints = @$tmp;
+ print "Input: (" . join(", ", @ints) . ")\n";
+ my $result = 0;
+ foreach my $i (0 .. $#ints) {
+ foreach my $j ($i+1 .. $#ints) {
+ if($ints[$i] == $ints[$j]) {
+ $result++ unless $i * $j % $k;
+ }
+ }
+ }
+ print "Output: $result\n";
+}
diff --git a/challenge-262/jeanluc2020/python/ch-1.py b/challenge-262/jeanluc2020/python/ch-1.py
new file mode 100755
index 0000000000..9f66edcdca
--- /dev/null
+++ b/challenge-262/jeanluc2020/python/ch-1.py
@@ -0,0 +1,63 @@
+#!/usr/bin/python3
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-262/#TASK1
+#
+# Task 1: Max Positive Negative
+# =============================
+#
+# You are given an array of integers, @ints.
+#
+# Write a script to return the maximum number of either positive or negative
+# integers in the given array.
+#
+## Example 1
+##
+## Input: @ints = (-3, 1, 2, -1, 3, -2, 4)
+## Output: 4
+##
+## Count of positive integers: 4
+## Count of negative integers: 3
+## Maximum of count of positive and negative integers: 4
+#
+## Example 2
+##
+## Input: @ints = (-1, -2, -3, 1)
+## Output: 3
+##
+## Count of positive integers: 1
+## Count of negative integers: 3
+## Maximum of count of positive and negative integers: 3
+#
+## Example 3
+##
+## Input: @ints = (1,2)
+## Output: 2
+##
+## Count of positive integers: 2
+## Count of negative integers: 0
+## Maximum of count of positive and negative integers: 2
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Simply count the negatives and the positives, then return
+# the bigger of the two numbers.
+
+def max_positive_negative(ints: list) -> None:
+ print("Input: (", ", ".join(str(x) for x in ints), ")", sep="")
+ pos = 0
+ neg = 0
+ for num in ints:
+ if num < 0:
+ neg += 1
+ else:
+ pos += 1
+ result = pos if pos > neg else neg
+ print(f"Output: {result}")
+
+max_positive_negative([-3, 1, 2, -1, 3, -2, 4]);
+max_positive_negative([-1, -2, -3, 1]);
+max_positive_negative([1,2]);
+
diff --git a/challenge-262/jeanluc2020/python/ch-2.py b/challenge-262/jeanluc2020/python/ch-2.py
new file mode 100755
index 0000000000..c391bd37e6
--- /dev/null
+++ b/challenge-262/jeanluc2020/python/ch-2.py
@@ -0,0 +1,53 @@
+#!/usr/bin/python3
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-262/#TASK2
+#
+# Task 2: Count Equal Divisible
+# =============================
+#
+# You are given an array of integers, @ints and an integer $k.
+#
+# Write a script to return the number of pairs (i, j) where
+#
+# a) 0 <= i < j < size of @ints
+# b) ints[i] == ints[j]
+# c) i x j is divisible by k
+#
+## Example 1
+##
+## Input: @ints = (3,1,2,2,2,1,3) and $k = 2
+## Output: 4
+##
+## (0, 6) => ints[0] == ints[6] and 0 x 6 is divisible by 2
+## (2, 3) => ints[2] == ints[3] and 2 x 3 is divisible by 2
+## (2, 4) => ints[2] == ints[4] and 2 x 4 is divisible by 2
+## (3, 4) => ints[3] == ints[4] and 3 x 4 is divisible by 2
+#
+## Example 2
+##
+## Input: @ints = (1,2,3) and $k = 1
+## Output: 0
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Have one index variable go from 0 to size of @ints - 1, then
+# another walk from the first index + 1 to size of @ints - 1. In
+# case ints[i] == ints[j], check if the product of i and j is
+# divisible by $k. Return the sum of all instances where this is true.
+
+def count_equal_divisible(ints: list, k: int) -> None:
+ print("Input: (", ", ".join(str(x) for x in ints), ")", sep="")
+ result = 0
+ for i in range(len(ints)):
+ for j in range(i+1, len(ints)):
+ if ints[i] == ints[j]:
+ if i * j % k == 0:
+ result += 1
+ print(f"Output: {result}")
+
+count_equal_divisible( [3,1,2,2,2,1,3], 2);
+count_equal_divisible( [1,2,3], 1);
+