aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-06-14 19:35:28 +0100
committerGitHub <noreply@github.com>2019-06-14 19:35:28 +0100
commit1b766bd780c8923551a8af9e5325bbfb79dbe88b (patch)
tree1aa56c3083b80e6e1cadf42251bc87401ce2218c
parent87fe6dc3b7314634e6d598628b0702c95bd4e9cb (diff)
parent730eb94c9c960b1db8f9602436244c8e7cf6be74 (diff)
downloadperlweeklychallenge-club-1b766bd780c8923551a8af9e5325bbfb79dbe88b.tar.gz
perlweeklychallenge-club-1b766bd780c8923551a8af9e5325bbfb79dbe88b.tar.bz2
perlweeklychallenge-club-1b766bd780c8923551a8af9e5325bbfb79dbe88b.zip
Merge pull request #249 from bracteatus/master
Solutions to Challenge 012
-rw-r--r--challenge-012/jaime/README45
-rw-r--r--challenge-012/jaime/perl5/ch-1.pl24
-rw-r--r--challenge-012/jaime/perl5/ch-2.pl37
3 files changed, 93 insertions, 13 deletions
diff --git a/challenge-012/jaime/README b/challenge-012/jaime/README
index 46e62dafa4..6b98f9b732 100644
--- a/challenge-012/jaime/README
+++ b/challenge-012/jaime/README
@@ -1,23 +1,42 @@
Solution by Jaime Corchado, (@tortsnare)[https://twitter.com/tortsnare].
-#Challenge #1
-Write a program which prints out all anagrams for a given word.
+# Challenge #1
+
+The numbers formed by adding one to the products of the smallest
+primes are called the Euclid Numbers (see wiki). Write a script that
+finds the smallest Euclid Number that is not prime.
## Solution
-Lookup anagrams in a hash of Engligh words.
-The script does initialize a list of words from Webster's Second International dictionary,
- but I removed any hyphenated or capitalized words.
+Calculate the product of prime numbers and check if each successor is a prime.
+
+# Challenge #2
-All words are keyed by alphabetical order. `join "", sort split //`
-Words that are anagrams of one another share the same key.
-Words that are not anagrams, do not share a key.
+Write a script that finds the common directory path, given a
+collection of paths and directory separator. For example, if the
+following paths are supplied.
-The script reports the anagrams of its arguments by looking up all words that share the key.
+```
+/a/b/c/d
+/a/b/cd
+/a/b/cc
+/a/b/c/d/e
+```
-#Challenge #2
-Write a program to find the sequence of characters that has the most.
+and the path separator is `/`. Your script should return `/a/b` as common
+directory path.
## Solution
-The English words are hashed as before.
-The script tracks the key with the most additions.
+
+```
+perl -- ch-2.pl <*delimiter*> <*list of strings*>
+```
+
+The path *delimiter* is a string like `/`.
+The *list of strings* is a string like `/usr/bin:/usr/sbin`.
+
+The solution is to find the consecutive intersection of all paths.
+
+# Challenge #3
+
+Find out the synonyms of a given word using the Synonyms API.
diff --git a/challenge-012/jaime/perl5/ch-1.pl b/challenge-012/jaime/perl5/ch-1.pl
new file mode 100644
index 0000000000..935c585fd5
--- /dev/null
+++ b/challenge-012/jaime/perl5/ch-1.pl
@@ -0,0 +1,24 @@
+# Solution by Jaime Corchado, (@tortsnare)[https://twitter.com/tortsnare].
+#
+# Challenge #1
+#
+# The numbers formed by adding one to the products of the smallest
+# primes are called the Euclid Numbers (see wiki). Write a script that
+# finds the smallest Euclid Number that is not prime.
+
+sub is_prime { # is a given positive integer an odd prime?
+ my ($n) = @_;
+ for (my $i = 3; $i < $n; $i += 2) { # determine if odd number is prime.
+ return 0 unless $n % $i;
+ }
+ return 1; # odd number is prime.
+}
+
+my $p = 2; # start with second primorial
+for (my $i = 3; ;$i += 2) {
+ next unless is_prime $i;
+ $p *= $i; # next primorial
+ last unless is_prime 1+$p;
+}
+
+print 1+$p;
diff --git a/challenge-012/jaime/perl5/ch-2.pl b/challenge-012/jaime/perl5/ch-2.pl
new file mode 100644
index 0000000000..15c1312e46
--- /dev/null
+++ b/challenge-012/jaime/perl5/ch-2.pl
@@ -0,0 +1,37 @@
+# Solution by Jaime Corchado, (@tortsnare)[https://twitter.com/tortsnare].
+#
+# Challenge #2
+#
+# Write a script that finds the common directory path, given a
+# collection of paths and directory separator. For example, if the
+# following paths are supplied.
+#
+# ```
+# /a/b/c/d
+# /a/b/cd
+# /a/b/cc
+# /a/b/c/d/e
+# ```
+#
+# and the path separator is `/`. Your script should return `/a/b` as common
+# directory path.
+
+my $delim = shift; # delimit a path, for example `/`.
+
+my $pelim = ":"; # delimit paths, for example unix uses `:`.
+my @paths = split /$pelim/, shift; # list of paths, for example `/bin:/sbin`.
+
+# at best, the first path is completely common within all paths
+my $common = @paths[0];
+
+PATH: for my $path (@paths) {
+ next PATH if $path =~ /^$common/; # $common at the start of $path.
+ while (($common) = ($common =~ /^(.*)$delim/)) {
+ last if $path =~ /^$common/; # otherwise find intersection of $common and $path.
+ }
+ last PATH unless $common; # stop if a $path is completely unique.
+}
+
+($common) = (@paths[0] =~ /^($delim)/) unless $common; # if given, root is always common
+
+print "$common\n";