aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Köhler <jean-luc@picard.franken.de>2025-05-09 21:40:01 +0200
committerThomas Köhler <jean-luc@picard.franken.de>2025-05-09 21:40:01 +0200
commit6f4a054298cc3cfba9364a9f64241fcd070b355d (patch)
treeb857e82aec633278b3cca06007eb64366d0d876e
parent4260922ac702246fa5e6743be89f77846ce8d96f (diff)
downloadperlweeklychallenge-club-6f4a054298cc3cfba9364a9f64241fcd070b355d.tar.gz
perlweeklychallenge-club-6f4a054298cc3cfba9364a9f64241fcd070b355d.tar.bz2
perlweeklychallenge-club-6f4a054298cc3cfba9364a9f64241fcd070b355d.zip
Add solution 320.
Signed-off-by: Thomas Köhler <jean-luc@picard.franken.de>
-rw-r--r--challenge-320/jeanluc2020/blog-1.txt1
-rw-r--r--challenge-320/jeanluc2020/blog-2.txt1
-rwxr-xr-xchallenge-320/jeanluc2020/perl/ch-1.pl72
-rwxr-xr-xchallenge-320/jeanluc2020/perl/ch-2.pl70
4 files changed, 144 insertions, 0 deletions
diff --git a/challenge-320/jeanluc2020/blog-1.txt b/challenge-320/jeanluc2020/blog-1.txt
new file mode 100644
index 0000000000..305c891870
--- /dev/null
+++ b/challenge-320/jeanluc2020/blog-1.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-320-1.html
diff --git a/challenge-320/jeanluc2020/blog-2.txt b/challenge-320/jeanluc2020/blog-2.txt
new file mode 100644
index 0000000000..d1550725b1
--- /dev/null
+++ b/challenge-320/jeanluc2020/blog-2.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-320-2.html
diff --git a/challenge-320/jeanluc2020/perl/ch-1.pl b/challenge-320/jeanluc2020/perl/ch-1.pl
new file mode 100755
index 0000000000..113a336843
--- /dev/null
+++ b/challenge-320/jeanluc2020/perl/ch-1.pl
@@ -0,0 +1,72 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-320/#TASK1
+#
+# Task 1: Maximum Count
+# =====================
+#
+# You are given an array of integers.
+#
+# Write a script to return the maximum between the number of positive and
+# negative integers. Zero is neither positive nor negative.
+#
+## Example 1
+##
+## Input: @ints = (-3, -2, -1, 1, 2, 3)
+## Output: 3
+##
+## There are 3 positive integers.
+## There are 3 negative integers.
+## The maximum between 3 and 3 is 3.
+#
+#
+## Example 2
+##
+## Input: @ints = (-2, -1, 0, 0, 1)
+## Output: 2
+##
+## There are 1 positive integers.
+## There are 2 negative integers.
+## The maximum between 2 and 1 is 2.
+#
+#
+## Example 3
+##
+## Input: @ints = (1, 2, 3, 4)
+## Output: 4
+##
+## There are 4 positive integers.
+## There are 0 negative integers.
+## The maximum between 4 and 0 is 4.
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# We just count the positive and the negative integers.
+# Then we look which of these two numbers is bigger.
+
+use v5.36;
+
+maximum_count(-3, -2, -1, 1, 2, 3);
+maximum_count(-2, -1, 0, 0, 1);
+maximum_count(1, 2, 3, 4);
+
+sub maximum_count(@ints) {
+ say "Input: (" . join(", ", @ints) . ")";
+ my $neg = 0;
+ my $pos = 0;
+ foreach my $elem (@ints) {
+ if($elem > 0) {
+ $pos++;
+ } elsif ( $elem < 0 ) {
+ $neg++;
+ }
+ }
+ if($neg > $pos) {
+ say "Output: $neg";
+ } else {
+ say "Output: $pos";
+ }
+}
diff --git a/challenge-320/jeanluc2020/perl/ch-2.pl b/challenge-320/jeanluc2020/perl/ch-2.pl
new file mode 100755
index 0000000000..ad575b21db
--- /dev/null
+++ b/challenge-320/jeanluc2020/perl/ch-2.pl
@@ -0,0 +1,70 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-320/#TASK2
+#
+# Task 2: Sum Difference
+# ======================
+#
+# You are given an array of positive integers.
+#
+# Write a script to return the absolute difference between digit sum and
+# element sum of the given array.
+#
+## Example 1
+##
+## Input: @ints = (1, 23, 4, 5)
+## Output: 18
+##
+## Element sum: 1 + 23 + 4 + 5 => 33
+## Digit sum: 1 + 2 + 3 + 4 + 5 => 15
+## Absolute difference: | 33 - 15 | => 18
+#
+#
+## Example 2
+##
+## Input: @ints = (1, 2, 3, 4, 5)
+## Output: 0
+##
+## Element sum: 1 + 2 + 3 + 4 + 5 => 15
+## Digit sum: 1 + 2 + 3 + 4 + 5 => 15
+## Absolute difference: | 15 - 15 | => 0
+#
+#
+## Example 3
+##
+## Input: @ints = (1, 2, 34)
+## Output: 27
+##
+## Element sum: 1 + 2 + 34 => 37
+## Digit sum: 1 + 2 + 3 + 4 => 10
+## Absolute difference: | 37 - 10 | => 27
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Looking at each element in the list, we just add it to the element sum.
+# Then we split the element into its digits and add all of those to the
+# digit sum. In the end, we calculate the absolute difference of these two
+# sums for the result.
+
+use v5.36;
+
+sum_difference(1, 23, 4, 5);
+sum_difference(1, 2, 3, 4, 5);
+sum_difference(1, 2, 34);
+
+sub sum_difference(@ints) {
+ say "Input: (" . join(", ", @ints) . ")";
+ my $elem_sum = 0;
+ my $digit_sum = 0;
+ foreach my $elem (@ints) {
+ $elem_sum += $elem;
+ my @digits = split //, $elem;
+ foreach my $digit (@digits) {
+ $digit_sum += $digit;
+ }
+ }
+ say "Output: " . abs($elem_sum - $digit_sum);
+}