aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-076/paulo-custodio/Makefile2
-rw-r--r--challenge-076/paulo-custodio/perl/ch-1.pl5
-rw-r--r--challenge-076/paulo-custodio/perl/ch-2.pl19
-rw-r--r--challenge-076/paulo-custodio/python/ch-1.py53
-rw-r--r--challenge-076/paulo-custodio/python/ch-2.py93
-rw-r--r--challenge-076/paulo-custodio/t/test-1.yaml60
-rw-r--r--challenge-076/paulo-custodio/t/test-2.yaml5
-rw-r--r--challenge-076/paulo-custodio/test.pl35
8 files changed, 230 insertions, 42 deletions
diff --git a/challenge-076/paulo-custodio/Makefile b/challenge-076/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-076/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-076/paulo-custodio/perl/ch-1.pl b/challenge-076/paulo-custodio/perl/ch-1.pl
index ad023788c6..75a4d3aefb 100644
--- a/challenge-076/paulo-custodio/perl/ch-1.pl
+++ b/challenge-076/paulo-custodio/perl/ch-1.pl
@@ -2,10 +2,11 @@
# Challenge 076
#
-# TASK #1 › Prime Sum
+# TASK #1 > Prime Sum
# Submitted by: Mohammad S Anwar
# Reviewed by: Ryan Thompson
-# You are given a number $N. Write a script to find the minimum number of prime numbers required, whose summation gives you $N.
+# You are given a number $N. Write a script to find the minimum number of
+# prime numbers required, whose summation gives you $N.
#
# For the sake of this task, please assume 1 is not a prime number.
#
diff --git a/challenge-076/paulo-custodio/perl/ch-2.pl b/challenge-076/paulo-custodio/perl/ch-2.pl
index ca138ae18e..0f6f4c848c 100644
--- a/challenge-076/paulo-custodio/perl/ch-2.pl
+++ b/challenge-076/paulo-custodio/perl/ch-2.pl
@@ -2,12 +2,15 @@
# Challenge 076
#
-# TASK #2 › Word Search
+# TASK #2 > Word Search
# Submitted by: Neil Bowers
# Reviewed by: Ryan Thompson
-# Write a script that takes two file names. The first file would contain word search grid as shown below. The second file contains list of words, one word per line. You could even use local dictionary file.
+# Write a script that takes two file names. The first file would contain word
+# search grid as shown below. The second file contains list of words, one word
+# per line. You could even use local dictionary file.
#
-# Print out a list of all words seen on the grid, looking both orthogonally and diagonally, backwards as well as forwards.
+# Print out a list of all words seen on the grid, looking both orthogonally
+# and diagonally, backwards as well as forwards.
#
# Search Grid
# B I D E M I A T S U C C O R S T
@@ -30,9 +33,15 @@
# R S E C I S N A B O S C N E R A
# D R S M P C U U N E L T E S I L
# Output
-# Found 54 words of length 5 or more when checked against the local dictionary. You may or may not get the same result but that is fine.
+# Found 54 words of length 5 or more when checked against the local dictionary.
+# You may or may not get the same result but that is fine.
#
-# aimed, align, antes, argos, arose, ashed, blunt, blunts, broad, buries, clove, cloven, constitution, constitutions, croon, depart, departed, enter, filch, garlic, goats, grieve, grieves, hazard, liens, malign, malignant, malls, margo, midst, ought, ovary, parted, patna, pudgiest, quash, quashed, raped, ruses, shrine, shrines, social, socializing, spasm, spasmodic, succor, succors, theorem, theorems, traci, tracie, virus, viruses, wigged
+# aimed, align, antes, argos, arose, ashed, blunt, blunts, broad, buries,
+# clove, cloven, constitution, constitutions, croon, depart, departed, enter,
+# filch, garlic, goats, grieve, grieves, hazard, liens, malign, malignant,
+# malls, margo, midst, ought, ovary, parted, patna, pudgiest, quash, quashed,
+# raped, ruses, shrine, shrines, social, socializing, spasm, spasmodic, succor,
+# succors, theorem, theorems, traci, tracie, virus, viruses, wigged
use Modern::Perl;
use Path::Tiny;
diff --git a/challenge-076/paulo-custodio/python/ch-1.py b/challenge-076/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..da2216e3ea
--- /dev/null
+++ b/challenge-076/paulo-custodio/python/ch-1.py
@@ -0,0 +1,53 @@
+#!/usr/bin/python3
+
+# Challenge 076
+#
+# TASK #1 > Prime Sum
+# Submitted by: Mohammad S Anwar
+# Reviewed by: Ryan Thompson
+# You are given a number $N. Write a script to find the minimum number of
+# prime numbers required, whose summation gives you $N.
+#
+# For the sake of this task, please assume 1 is not a prime number.
+#
+# Example:
+# Input:
+# $N = 9
+#
+# Ouput:
+# 2 as sum of 2 prime numbers i.e. 2 and 7 is same as the input number.
+# 2 + 7 = 9.
+
+import sys
+from primePy import primes
+from itertools import combinations
+
+def next_prime(n):
+ if n <= 1:
+ return 2
+ else:
+ n += 1
+ while not primes.check(n):
+ n += 1
+ return n
+
+# check all combinations for the shortest that adds up to N
+def find_set(n):
+ # get all primes up to N, 1 not included;
+ # append primes 2 and 3 to be able to solve special cases 4 and 6
+ terms = primes.upto(n)
+ terms.extend([2, 3])
+
+ solution = []
+ for k in range(1, len(terms)+1):
+ for combin in combinations(terms, k):
+ if sum(combin)==n:
+ if len(solution)==0 or len(solution)>len(combin):
+ solution = sorted(list(combin))
+ if len(solution)==1:
+ return solution # special case - is prime
+ return solution
+
+n = int(sys.argv[1])
+solution = find_set(n)
+print(" + ".join([str(x) for x in solution])+" = "+str(n))
diff --git a/challenge-076/paulo-custodio/python/ch-2.py b/challenge-076/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..053034ccb5
--- /dev/null
+++ b/challenge-076/paulo-custodio/python/ch-2.py
@@ -0,0 +1,93 @@
+#!/usr/bin/python3
+
+# Challenge 076
+#
+# TASK #2 > Word Search
+# Submitted by: Neil Bowers
+# Reviewed by: Ryan Thompson
+# Write a script that takes two file names. The first file would contain word
+# search grid as shown below. The second file contains list of words, one word
+# per line. You could even use local dictionary file.
+#
+# Print out a list of all words seen on the grid, looking both orthogonally
+# and diagonally, backwards as well as forwards.
+#
+# Search Grid
+# B I D E M I A T S U C C O R S T
+# L D E G G I W Q H O D E E H D P
+# U S E I R U B U T E A S L A G U
+# N G N I Z I L A I C O S C N U D
+# T G M I D S T S A R A R E I F G
+# S R E N M D C H A S I V E E L I
+# S C S H A E U E B R O A D M T E
+# H W O V L P E D D L A I U L S S
+# R Y O N L A S F C S T A O G O T
+# I G U S S R R U G O V A R Y O C
+# N R G P A T N A N G I L A M O O
+# E I H A C E I V I R U S E S E D
+# S E T S U D T T G A R L I C N H
+# H V R M X L W I U M S N S O T B
+# A E A O F I L C H T O D C A E U
+# Z S C D F E C A A I I R L N R F
+# A R I I A N Y U T O O O U T P F
+# R S E C I S N A B O S C N E R A
+# D R S M P C U U N E L T E S I L
+# Output
+# Found 54 words of length 5 or more when checked against the local dictionary.
+# You may or may not get the same result but that is fine.
+#
+# aimed, align, antes, argos, arose, ashed, blunt, blunts, broad, buries,
+# clove, cloven, constitution, constitutions, croon, depart, departed, enter,
+# filch, garlic, goats, grieve, grieves, hazard, liens, malign, malignant,
+# malls, margo, midst, ought, ovary, parted, patna, pudgiest, quash, quashed,
+# raped, ruses, shrine, shrines, social, socializing, spasm, spasmodic, succor,
+# succors, theorem, theorems, traci, tracie, virus, viruses, wigged
+
+import sys
+import re
+
+# parse grid file, return matrix m x n or letters
+def parse_grid(file):
+ with open(file) as f:
+ grid = []
+ for line in f.readlines():
+ line = re.sub(r"\s+", "", line)
+ grid.append([x for x in line])
+ return grid
+
+# extract all possible words with the given minimum length
+# from the grid in all 8 directions
+def grid_words(min_len, grid):
+ words = set()
+ for r0 in range(0, len(grid)):
+ for c0 in range(0, len(grid[0])):
+ for dr in range(-1, 2):
+ for dc in range(-1, 2):
+ if dr!=0 or dc!=0:
+ word = ""
+ l = 0
+ while True:
+ r, c = r0+l*dr, c0+l*dc
+ if r<0 or r>=len(grid) or \
+ c<0 or c>=len(grid[0]):
+ break
+ word += grid[r][c]
+ if len(word)>=min_len:
+ words.add(word.lower())
+ l += 1
+ return words
+
+# return all wards from dictionary that exist in the given set
+def find_dict(file, words):
+ found = []
+ with open(file) as f:
+ for word in f.readlines():
+ word = word.strip()
+ if word in words:
+ found.append(word)
+ return sorted(found)
+
+grid = parse_grid(sys.argv[1])
+words = grid_words(5, grid)
+found = find_dict(sys.argv[2], words)
+print(*found,sep=", ")
diff --git a/challenge-076/paulo-custodio/t/test-1.yaml b/challenge-076/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..1a55590603
--- /dev/null
+++ b/challenge-076/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,60 @@
+- setup:
+ cleanup:
+ args: 2
+ input:
+ output: 2 = 2
+- setup:
+ cleanup:
+ args: 3
+ input:
+ output: 3 = 3
+- setup:
+ cleanup:
+ args: 4
+ input:
+ output: 2 + 2 = 4
+- setup:
+ cleanup:
+ args: 5
+ input:
+ output: 5 = 5
+- setup:
+ cleanup:
+ args: 6
+ input:
+ output: 3 + 3 = 6
+- setup:
+ cleanup:
+ args: 7
+ input:
+ output: 7 = 7
+- setup:
+ cleanup:
+ args: 8
+ input:
+ output: 3 + 5 = 8
+- setup:
+ cleanup:
+ args: 9
+ input:
+ output: 2 + 7 = 9
+- setup:
+ cleanup:
+ args: 10
+ input:
+ output: 3 + 7 = 10
+- setup:
+ cleanup:
+ args: 11
+ input:
+ output: 11 = 11
+- setup:
+ cleanup:
+ args: 12
+ input:
+ output: 5 + 7 = 12
+- setup:
+ cleanup:
+ args: 13
+ input:
+ output: 13 = 13
diff --git a/challenge-076/paulo-custodio/t/test-2.yaml b/challenge-076/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..f56647da12
--- /dev/null
+++ b/challenge-076/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,5 @@
+- setup: 0==system("aspell -d en dump master | aspell -l en expand > words.txt")
+ cleanup: unlink("words.txt")
+ args: grid.txt words.txt
+ input:
+ output: aimed, align, antes, arose, ashed, blunt, blunts, broad, buries, clove, cloven, constitution, constitutions, croon, depart, departed, enter, filch, garlic, goats, grieve, grieves, hazard, liens, malign, malignant, malls, midst, ought, ovary, parted, pudgiest, quash, quashed, raped, ruses, shrine, shrines, social, socializing, spasm, spasmodic, succor, succors, theorem, theorems, virus, viruses, wigged
diff --git a/challenge-076/paulo-custodio/test.pl b/challenge-076/paulo-custodio/test.pl
deleted file mode 100644
index 755968df91..0000000000
--- a/challenge-076/paulo-custodio/test.pl
+++ /dev/null
@@ -1,35 +0,0 @@
-#!/usr/bin/perl
-
-use strict;
-use Modern::Perl;
-
-is capture("perl perl/ch-1.pl 2"), "2 = 2\n";
-is capture("perl perl/ch-1.pl 3"), "3 = 3\n";
-is capture("perl perl/ch-1.pl 4"), "2 + 2 = 4\n";
-is capture("perl perl/ch-1.pl 5"), "5 = 5\n";
-is capture("perl perl/ch-1.pl 6"), "3 + 3 = 6\n";
-is capture("perl perl/ch-1.pl 7"), "7 = 7\n";
-is capture("perl perl/ch-1.pl 8"), "3 + 5 = 8\n";
-is capture("perl perl/ch-1.pl 9"), "2 + 7 = 9\n";
-is capture("perl perl/ch-1.pl 10"), "3 + 7 = 10\n";
-is capture("perl perl/ch-1.pl 11"), "11 = 11\n";
-is capture("perl perl/ch-1.pl 12"), "5 + 7 = 12\n";
-is capture("perl perl/ch-1.pl 13"), "13 = 13\n";
-
-
-# build list of words for testing
-ok 0==system("aspell -d en dump master | aspell -l en expand > words.txt");
-
-is capture("perl perl/ch-2.pl grid.txt words.txt"), <<END;
-aimed, align, antes, arose, ashed, blunt, blunts, broad, buries, clove, cloven, constitution, constitutions, croon, depart, departed, enter, filch, garlic, goats, grieve, grieves, hazard, liens, malign, malignant, malls, midst, ought, ovary, parted, pudgiest, quash, quashed, raped, ruses, shrine, shrines, social, socializing, spasm, spasmodic, succor, succors, theorem, theorems, virus, viruses, wigged
-END
-
-unlink "words.txt";
-done_testing;
-
-sub capture {
- my($cmd) = @_;
- my $out = `$cmd`;
- $out =~ s/[ \t\v\f\r]*\n/\n/g;
- return $out;
-}