aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Köhler <jean-luc@picard.franken.de>2023-10-11 21:57:22 +0200
committerThomas Köhler <jean-luc@picard.franken.de>2023-10-11 21:57:22 +0200
commit7fd4d9df83269a9de352d8a9b30eff594f92b483 (patch)
treed84e42464646f30a530a677c6228e4682d4ac599
parent3143f9657ea324e7588d575d67c35eb28bc276f3 (diff)
downloadperlweeklychallenge-club-7fd4d9df83269a9de352d8a9b30eff594f92b483.tar.gz
perlweeklychallenge-club-7fd4d9df83269a9de352d8a9b30eff594f92b483.tar.bz2
perlweeklychallenge-club-7fd4d9df83269a9de352d8a9b30eff594f92b483.zip
Add solution 238.
Signed-off-by: Thomas Köhler <jean-luc@picard.franken.de>
-rw-r--r--challenge-238/jeanluc2020/blog-1.txt1
-rw-r--r--challenge-238/jeanluc2020/blog-2.txt1
-rwxr-xr-xchallenge-238/jeanluc2020/perl/ch-1.pl52
-rwxr-xr-xchallenge-238/jeanluc2020/perl/ch-2.pl64
-rwxr-xr-xchallenge-238/jeanluc2020/python/ch-1.py49
-rwxr-xr-xchallenge-238/jeanluc2020/python/ch-2.py67
6 files changed, 234 insertions, 0 deletions
diff --git a/challenge-238/jeanluc2020/blog-1.txt b/challenge-238/jeanluc2020/blog-1.txt
new file mode 100644
index 0000000000..8018c6f569
--- /dev/null
+++ b/challenge-238/jeanluc2020/blog-1.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-238-1.html
diff --git a/challenge-238/jeanluc2020/blog-2.txt b/challenge-238/jeanluc2020/blog-2.txt
new file mode 100644
index 0000000000..0ad8bd733e
--- /dev/null
+++ b/challenge-238/jeanluc2020/blog-2.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-238-2.html
diff --git a/challenge-238/jeanluc2020/perl/ch-1.pl b/challenge-238/jeanluc2020/perl/ch-1.pl
new file mode 100755
index 0000000000..2923478436
--- /dev/null
+++ b/challenge-238/jeanluc2020/perl/ch-1.pl
@@ -0,0 +1,52 @@
+#!/usr/bin/perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-238/#TASK1
+#
+# Task 1: Running Sum
+# ===================
+#
+# You are given an array of integers.
+#
+# Write a script to return the running sum of the given array. The running sum
+# can be calculated as sum[i] = num[0] + num[1] + …. + num[i].
+#
+## Example 1
+##
+## Input: @int = (1, 2, 3, 4, 5)
+## Output: (1, 3, 6, 10, 15)
+#
+## Example 2
+##
+## Input: @int = (1, 1, 1, 1, 1)
+## Output: (1, 2, 3, 4, 5)
+#
+## Example 3
+##
+## Input: @int = (0, -1, 1, 2)
+## Output: (0, -1, 0, 2)
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# This one is straight forward: For each element, add it to
+# the current sum and add the sum to the result array.
+use strict;
+use warnings;
+
+running_sum(1, 2, 3, 4, 5);
+running_sum(1, 1, 1, 1, 1);
+running_sum(0, -1, 1, 2);
+
+sub running_sum {
+ my @int = @_;
+ my $sum = 0;
+ my @result = ();
+ print "Input: (" . join(", ", @int) . ")\n";
+ foreach my $elem (@int) {
+ $sum += $elem;
+ push @result, $sum;
+ }
+ print "Output: (" . join(", ", @result) . ")\n";
+}
diff --git a/challenge-238/jeanluc2020/perl/ch-2.pl b/challenge-238/jeanluc2020/perl/ch-2.pl
new file mode 100755
index 0000000000..5322059f60
--- /dev/null
+++ b/challenge-238/jeanluc2020/perl/ch-2.pl
@@ -0,0 +1,64 @@
+#!/usr/bin/perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-238/#TASK2
+#
+# Task 2: Persistence Sort
+# ========================
+#
+# You are given an array of positive integers.
+#
+# Write a script to sort the given array in increasing order with respect to
+# the count of steps required to obtain a single-digit number by multiplying
+# its digits recursively for each array element. If any two numbers have the
+# same count of steps, then print the smaller number first.
+#
+## Example 1
+##
+## Input: @int = (15, 99, 1, 34)
+## Output: (1, 15, 34, 99)
+##
+## 15 => 1 x 5 => 5 (1 step)
+## 99 => 9 x 9 => 81 => 8 x 1 => 8 (2 steps)
+## 1 => 0 step
+## 34 => 3 x 4 => 12 => 1 x 2 => 2 (2 steps)
+#
+## Example 2
+##
+## Input: @int = (50, 25, 33, 22)
+## Output: (22, 33, 50, 25)
+##
+## 50 => 5 x 0 => 0 (1 step)
+## 25 => 2 x 5 => 10 => 1 x 0 => 0 (2 steps)
+## 33 => 3 x 3 => 6 (1 step)
+## 22 => 2 x 2 => 4 (1 step)
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# By creating a helper function that calculates the persistence,
+# the actual function is basically calling the right sort function
+use strict;
+use warnings;
+
+persistence_sort(15, 99, 1, 34);
+persistence_sort(50, 25, 33, 22);
+
+sub persistence_sort {
+ my @int = @_;
+ print "Input: (" . join(", ", @int) . ")\n";
+ my @result = sort { persistence($a) <=> persistence($b) || $a <=> $b } @int;
+ print "Output: (" . join(", ", @result) . ")\n";
+}
+
+sub persistence {
+ my $number = shift;
+ my @digits = split //, $number;
+ return 0 unless @digits > 1;
+ my $prod = 1;
+ foreach my $d (@digits) {
+ $prod *= $d;
+ }
+ return 1 + persistence($prod);
+}
diff --git a/challenge-238/jeanluc2020/python/ch-1.py b/challenge-238/jeanluc2020/python/ch-1.py
new file mode 100755
index 0000000000..de9447fd3e
--- /dev/null
+++ b/challenge-238/jeanluc2020/python/ch-1.py
@@ -0,0 +1,49 @@
+#!/usr/bin/python3
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-238/#TASK1
+#
+# Task 1: Running Sum
+# ===================
+#
+# You are given an array of integers.
+#
+# Write a script to return the running sum of the given array. The running sum
+# can be calculated as sum[i] = num[0] + num[1] + …. + num[i].
+#
+## Example 1
+##
+## Input: @int = (1, 2, 3, 4, 5)
+## Output: (1, 3, 6, 10, 15)
+#
+## Example 2
+##
+## Input: @int = (1, 1, 1, 1, 1)
+## Output: (1, 2, 3, 4, 5)
+#
+## Example 3
+##
+## Input: @int = (0, -1, 1, 2)
+## Output: (0, -1, 0, 2)
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# This one is straight forward: For each element, add it to
+# the current sum and add the sum to the result array.
+
+
+def running_sum(ints: list):
+ sum = 0
+ result = []
+ print("Input: (", ", ".join(str(x) for x in ints), ")")
+ for elem in ints:
+ sum += elem
+ result.append(sum)
+ print("Output: (", ", ".join(str(x) for x in result), ")")
+
+
+running_sum([1, 2, 3, 4, 5])
+running_sum([1, 1, 1, 1, 1])
+running_sum([0, -1, 1, 2])
diff --git a/challenge-238/jeanluc2020/python/ch-2.py b/challenge-238/jeanluc2020/python/ch-2.py
new file mode 100755
index 0000000000..558a0c6183
--- /dev/null
+++ b/challenge-238/jeanluc2020/python/ch-2.py
@@ -0,0 +1,67 @@
+#!/usr/bin/python3
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-238/#TASK2
+#
+# Task 2: Persistence Sort
+# ========================
+#
+# You are given an array of positive integers.
+#
+# Write a script to sort the given array in increasing order with respect to
+# the count of steps required to obtain a single-digit number by multiplying
+# its digits recursively for each array element. If any two numbers have the
+# same count of steps, then print the smaller number first.
+#
+## Example 1
+##
+## Input: @int = (15, 99, 1, 34)
+## Output: (1, 15, 34, 99)
+##
+## 15 => 1 x 5 => 5 (1 step)
+## 99 => 9 x 9 => 81 => 8 x 1 => 8 (2 steps)
+## 1 => 0 step
+## 34 => 3 x 4 => 12 => 1 x 2 => 2 (2 steps)
+#
+## Example 2
+##
+## Input: @int = (50, 25, 33, 22)
+## Output: (22, 33, 50, 25)
+##
+## 50 => 5 x 0 => 0 (1 step)
+## 25 => 2 x 5 => 10 => 1 x 0 => 0 (2 steps)
+## 33 => 3 x 3 => 6 (1 step)
+## 22 => 2 x 2 => 4 (1 step)
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Create a temporary list that contains a list for each
+# element. The list consists of the int and the persistence
+# Then we just need to sort by persistence, then by the number
+
+from operator import itemgetter
+
+def persistence(number: int) -> int:
+ digits = [int(d) for d in str(number)]
+ if len(digits) <= 1:
+ return 0
+ prod = 1
+ for d in digits:
+ prod *= d
+ return 1 + persistence(prod)
+
+
+def persistence_sort(ints: list):
+ print("Input: (" , ", ".join(str(x) for x in ints), ")")
+ tmp = []
+ for x in ints:
+ tmp.append( (x, persistence(x)) )
+ s = sorted(tmp, key=itemgetter(1))
+ s = sorted(s, key=itemgetter(0))
+ #s = sorted(s)
+ print("Output: (" , ", ".join(str(x[0]) for x in s), ")")
+
+persistence_sort([15, 99, 1, 34])
+persistence_sort([50, 25, 33, 22])