aboutsummaryrefslogtreecommitdiff
path: root/challenge-012
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2019-06-16 18:45:57 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2019-06-16 18:45:57 +0100
commite722e3a2172e57f2a47f9153faa3a8874e997abe (patch)
treeb7396685ff37a2160bc72397abec07777226ee48 /challenge-012
parentff88a3ac610b26e40ddd65839411ce257f3502b5 (diff)
downloadperlweeklychallenge-club-e722e3a2172e57f2a47f9153faa3a8874e997abe.tar.gz
perlweeklychallenge-club-e722e3a2172e57f2a47f9153faa3a8874e997abe.tar.bz2
perlweeklychallenge-club-e722e3a2172e57f2a47f9153faa3a8874e997abe.zip
- Added solutions by Mark Senn.
Diffstat (limited to 'challenge-012')
-rw-r--r--challenge-012/mark-senn/blog.txt1
-rw-r--r--challenge-012/mark-senn/perl6/ch-1.p626
-rw-r--r--challenge-012/mark-senn/perl6/ch-2.p641
3 files changed, 68 insertions, 0 deletions
diff --git a/challenge-012/mark-senn/blog.txt b/challenge-012/mark-senn/blog.txt
new file mode 100644
index 0000000000..054c6ac091
--- /dev/null
+++ b/challenge-012/mark-senn/blog.txt
@@ -0,0 +1 @@
+https://engineering.purdue.edu/~mark/pwc-012.pdf
diff --git a/challenge-012/mark-senn/perl6/ch-1.p6 b/challenge-012/mark-senn/perl6/ch-1.p6
new file mode 100644
index 0000000000..1867bceb46
--- /dev/null
+++ b/challenge-012/mark-senn/perl6/ch-1.p6
@@ -0,0 +1,26 @@
+# Perl Weekly Challenge - 012
+# Challenge #1
+#
+# See
+# engineering.purdue.edu/~mark/pwc-012.pdf
+# for more information.
+
+# Run using Perl 6.
+use v6;
+
+# Get prime numbers from 2 to 1,000.
+# This doesn't read left-to-right
+# my @prime = grep &is-prime, (2..1_000);
+# as well as this does:
+my @prime = (2..1_000).grep(&is-prime);
+
+for (^@prime.elems) -> $i
+{
+ # This could be optimized by keeping a running product of the
+ # first n primes and then just multiplying by the (n+1)st prime
+ # but
+ # Premature optimization is the root of all evil.
+ # ---Donald Ervin Knuth
+ my $e = ([*] @prime[0..$i]) + 1;
+ ($e.is-prime) or $e.say, last;
+}
diff --git a/challenge-012/mark-senn/perl6/ch-2.p6 b/challenge-012/mark-senn/perl6/ch-2.p6
new file mode 100644
index 0000000000..354f66ead9
--- /dev/null
+++ b/challenge-012/mark-senn/perl6/ch-2.p6
@@ -0,0 +1,41 @@
+# Perl Weekly Challenge - 012
+# Challenge #2
+#
+# See
+# engineering.purdue.edu/~mark/pwc-012.pdf
+# for more information.
+
+# Run using Perl 6.
+use v6;
+
+# Define @path and $sep.
+my Str @path = <</a/b/c/d /a/b/cd /a/b/cc /a/b/c/d/e>>;
+my $sep = '/';
+
+# Split @path into its separate parts and put
+# the results in the two-dimensional @part array.
+# 0 1 2 3 4 5
+# 0 a b c d
+# 1 a b cd
+# 2 a b cc
+# 3 a b c d e
+my @part;
+for @path
+{
+ push @part, split(/$sep/, $_);
+}
+
+# Compute index of last directory part of @part with fewest number of parts.
+my $lastindex = @part.map({.elems}).min - 2;
+
+# Set $index to one past the last directory part that matched.
+# Searh for unequal directory parts or ``fall out the bottom''
+# of the loop if all directory parts match for this $index.
+my $index = 0;
+while ($index <= $lastindex && [eq] @part[*;$index])
+{
+ $index++;
+}
+
+# Print the output.
+say join '/', @part[0;^$index];