aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Köhler <jean-luc@picard.franken.de>2024-09-09 19:00:21 +0200
committerThomas Köhler <jean-luc@picard.franken.de>2024-09-09 19:00:21 +0200
commit301982a6a3de3ac34e750c7e1b81bfba3c2cebc8 (patch)
treebce90999b2cda960c6bfd90af2a8eb3bffc0fc52
parent148ad068f27ef5bbdcac38b14685b2273b0d02ec (diff)
downloadperlweeklychallenge-club-301982a6a3de3ac34e750c7e1b81bfba3c2cebc8.tar.gz
perlweeklychallenge-club-301982a6a3de3ac34e750c7e1b81bfba3c2cebc8.tar.bz2
perlweeklychallenge-club-301982a6a3de3ac34e750c7e1b81bfba3c2cebc8.zip
Add solution 286.
Signed-off-by: Thomas Köhler <jean-luc@picard.franken.de>
-rw-r--r--challenge-286/jeanluc2020/blog-1.txt1
-rw-r--r--challenge-286/jeanluc2020/blog-2.txt1
-rwxr-xr-xchallenge-286/jeanluc2020/perl/ch-1.pl57
-rwxr-xr-xchallenge-286/jeanluc2020/perl/ch-2.pl120
4 files changed, 179 insertions, 0 deletions
diff --git a/challenge-286/jeanluc2020/blog-1.txt b/challenge-286/jeanluc2020/blog-1.txt
new file mode 100644
index 0000000000..09fed5eb1d
--- /dev/null
+++ b/challenge-286/jeanluc2020/blog-1.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-286-1.html
diff --git a/challenge-286/jeanluc2020/blog-2.txt b/challenge-286/jeanluc2020/blog-2.txt
new file mode 100644
index 0000000000..1f70098746
--- /dev/null
+++ b/challenge-286/jeanluc2020/blog-2.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-286-2.html
diff --git a/challenge-286/jeanluc2020/perl/ch-1.pl b/challenge-286/jeanluc2020/perl/ch-1.pl
new file mode 100755
index 0000000000..ece9103be1
--- /dev/null
+++ b/challenge-286/jeanluc2020/perl/ch-1.pl
@@ -0,0 +1,57 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-286/#TASK1
+#
+# Task 1: Self Spammer
+# ====================
+#
+# Write a program which outputs one word of its own script / source code at
+# random. A word is anything between whitespace, including symbols.
+#
+## Example 1
+##
+## If the source code contains a line such as: 'open my $fh, "<", "ch-1.pl" or die;'
+## then the program would output each of the words { open, my, $fh,, "<",, "ch-1.pl", or, die; }
+## (along with other words in the source) with some positive probability.
+#
+## Example 2
+##
+## Technically 'print(" hello ");' is *not* an example program, because it does not
+## assign positive probability to the other two words in the script.
+## It will never display print(" or ");
+#
+## Example 3
+##
+## An empty script is one trivial solution, and here is another:
+## echo "42" > ch-1.pl && perl -p -e '' ch-1.pl
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# We read the program code by opening $0 for reading, then reading everything
+# line by line. We randomly select one line, which we then split into tokens
+# at whitespace. Then we randomly select one token and print it.
+# Obviously, this also contains comments, which we could exclude by grepping the
+# program for lines that don't start in '#'.
+
+use strict;
+use warnings;
+
+self_spammer($0);
+
+sub self_spammer {
+ my $program = shift;
+ my @lines = ();
+ open(my $IN, "<", $program) or die "Can't open $program for reading: $!";
+ while(my $line = <$IN>) {
+ chomp($line);
+ push @lines, $line;
+ }
+ my $l = $lines[int(rand(1+$#lines))];
+ my @tokens = split /\s+/, $l;
+ my $t = $tokens[int(rand(1+$#tokens))];
+ print "$t\n";
+}
+
diff --git a/challenge-286/jeanluc2020/perl/ch-2.pl b/challenge-286/jeanluc2020/perl/ch-2.pl
new file mode 100755
index 0000000000..0893e5f37b
--- /dev/null
+++ b/challenge-286/jeanluc2020/perl/ch-2.pl
@@ -0,0 +1,120 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-286/#TASK2
+#
+# Task 2: Order Game
+# ==================
+#
+# You are given an array of integers, @ints, whose length is a power of 2.
+#
+# Write a script to play the order game (min and max) and return the last element.
+#
+## Example 1
+##
+## Input: @ints = (2, 1, 4, 5, 6, 3, 0, 2)
+## Output: 1
+##
+## Operation 1:
+##
+## min(2, 1) = 1
+## max(4, 5) = 5
+## min(6, 3) = 3
+## max(0, 2) = 2
+##
+## Operation 2:
+##
+## min(1, 5) = 1
+## max(3, 2) = 3
+##
+## Operation 3:
+##
+## min(1, 3) = 1
+#
+## Example 2
+##
+## Input: @ints = (0, 5, 3, 2)
+## Output: 0
+##
+## Operation 1:
+##
+## min(0, 5) = 0
+## max(3, 2) = 3
+##
+## Operation 2:
+##
+## min(0, 3) = 0
+#
+## Example 3
+##
+## Input: @ints = (9, 2, 1, 4, 5, 6, 0, 7, 3, 1, 3, 5, 7, 9, 0, 8)
+## Output: 2
+##
+## Operation 1:
+##
+## min(9, 2) = 2
+## max(1, 4) = 4
+## min(5, 6) = 5
+## max(0, 7) = 7
+## min(3, 1) = 1
+## max(3, 5) = 5
+## min(7, 9) = 7
+## max(0, 8) = 8
+##
+## Operation 2:
+##
+## min(2, 4) = 2
+## max(5, 7) = 7
+## min(1, 5) = 1
+## max(7, 8) = 8
+##
+## Operation 3:
+##
+## min(2, 7) = 2
+## max(1, 8) = 8
+##
+## Operation 4:
+##
+## min(2, 8) = 2
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# As long as @ints has more than one element, replace it with
+# an array that contains the min/max of each pair of numbers,
+# properly switching from min to max and vice versa on each
+# step.
+
+use strict;
+use warnings;
+use List::Util qw(min max);
+
+order_game(2, 1, 4, 5, 6, 3, 0, 2);
+order_game(0, 5, 3, 2);
+order_game(9, 2, 1, 4, 5, 6, 0, 7, 3, 1, 3, 5, 7, 9, 0, 8);
+
+sub order_game {
+ my @ints = @_;
+ print "Input: (" . join(", ", @ints) . ")\n";
+ while(scalar(@ints) > 1) {
+ @ints = min_max(@ints);
+ }
+ print "Output: $ints[0]\n";
+}
+
+sub min_max {
+ my @ints = @_;
+ my @result = ();
+ my $which = 0;
+ for (my $i = 0; $i < $#ints; $i+=2) {
+ if($which) {
+ $which = 0;
+ push @result, max($ints[$i], $ints[$i+1]);
+ } else {
+ $which = 1;
+ push @result, min($ints[$i], $ints[$i+1]);
+ }
+ }
+ return @result;
+}