aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2024-09-09 20:13:14 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2024-09-10 13:32:31 +0100
commitc0a46f3a93e39ab14c334be181685b4ac60f8eb9 (patch)
treea0c0c84ad5c814ef97b7b8d110628aedea02d432
parent0d07992c8e9958afc2c941ca0b5f034bd426cdba (diff)
downloadperlweeklychallenge-club-c0a46f3a93e39ab14c334be181685b4ac60f8eb9.tar.gz
perlweeklychallenge-club-c0a46f3a93e39ab14c334be181685b4ac60f8eb9.tar.bz2
perlweeklychallenge-club-c0a46f3a93e39ab14c334be181685b4ac60f8eb9.zip
Add Perl solution to challenge 286
-rw-r--r--challenge-286/paulo-custodio/Makefile2
-rw-r--r--challenge-286/paulo-custodio/README1
-rw-r--r--challenge-286/paulo-custodio/perl/ch-1.pl33
-rw-r--r--challenge-286/paulo-custodio/perl/ch-2.pl98
-rw-r--r--challenge-286/paulo-custodio/t/test-2.yaml15
5 files changed, 149 insertions, 0 deletions
diff --git a/challenge-286/paulo-custodio/Makefile b/challenge-286/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-286/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-286/paulo-custodio/README b/challenge-286/paulo-custodio/README
new file mode 100644
index 0000000000..87dc0b2fbd
--- /dev/null
+++ b/challenge-286/paulo-custodio/README
@@ -0,0 +1 @@
+Solution by Paulo Custodio
diff --git a/challenge-286/paulo-custodio/perl/ch-1.pl b/challenge-286/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..078cf5ee01
--- /dev/null
+++ b/challenge-286/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,33 @@
+#!/usr/bin/env perl
+
+# Challenge 286
+#
+# Task 1: Self Spammer
+# Submitted by: David Ferrone
+#
+# 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
+
+use Modern::Perl;
+use Path::Tiny;
+use List::Util 'shuffle';
+
+say((shuffle(split " ", path($0)->slurp))[0]);
diff --git a/challenge-286/paulo-custodio/perl/ch-2.pl b/challenge-286/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..13acc3ccbd
--- /dev/null
+++ b/challenge-286/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,98 @@
+#!/usr/bin/env perl
+
+# Challenge 286
+#
+# Task 2: Order Game
+# Submitted by: Mohammad Sajid Anwar
+#
+# 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
+
+use Modern::Perl;
+use List::Util qw( min max );
+
+my @nums = @ARGV;
+@nums = reduce(@nums) while @nums > 1;
+say $nums[0];
+
+sub reduce {
+ my(@nums) = @_;
+ my @out;
+ while (@nums) {
+ if (@nums >= 2) {
+ push @out, min(@nums[0..1]);
+ }
+ if (@nums >= 4) {
+ push @out, max(@nums[2..3]);
+ }
+ splice(@nums, 0, 4);
+ }
+ return @out;
+}
diff --git a/challenge-286/paulo-custodio/t/test-2.yaml b/challenge-286/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..cbb9f147f8
--- /dev/null
+++ b/challenge-286/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,15 @@
+- setup:
+ cleanup:
+ args: 2 1 4 5 6 3 0 2
+ input:
+ output: 1
+- setup:
+ cleanup:
+ args: 0 5 3 2
+ input:
+ output: 0
+- setup:
+ cleanup:
+ args: 9 2 1 4 5 6 0 7 3 1 3 5 7 9 0 8
+ input:
+ output: 2