aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-03-06 18:47:02 +0100
committerAbigail <abigail@abigail.be>2021-03-06 18:47:02 +0100
commit85799c6783f520b36021acf7e2cd93283056fe28 (patch)
tree5e6abe3f7ecdce8feffc065ec5334a72037a2f3d
parent53f8a2c0c28815d2ae4eb67f0fdd378462c74147 (diff)
downloadperlweeklychallenge-club-85799c6783f520b36021acf7e2cd93283056fe28.tar.gz
perlweeklychallenge-club-85799c6783f520b36021acf7e2cd93283056fe28.tar.bz2
perlweeklychallenge-club-85799c6783f520b36021acf7e2cd93283056fe28.zip
Python solution for week 4, part 2
-rw-r--r--challenge-004/abigail/README.md1
-rw-r--r--challenge-004/abigail/python/ch-2.py40
2 files changed, 41 insertions, 0 deletions
diff --git a/challenge-004/abigail/README.md b/challenge-004/abigail/README.md
index 382ede17cd..07a11c980b 100644
--- a/challenge-004/abigail/README.md
+++ b/challenge-004/abigail/README.md
@@ -51,3 +51,4 @@ The sets of letters are read from standard input.
* [Lua](lua/ch-2.lua)
* [Node.js](node/ch-2.js)
* [Perl](perl/ch-2.pl)
+* [Pyton](python/ch-2.py)
diff --git a/challenge-004/abigail/python/ch-2.py b/challenge-004/abigail/python/ch-2.py
new file mode 100644
index 0000000000..4e94f7fe7d
--- /dev/null
+++ b/challenge-004/abigail/python/ch-2.py
@@ -0,0 +1,40 @@
+#!/opt/local/bin/python
+
+#
+# See ../README.md
+#
+
+#
+# Run as python ch-2.py < input-file
+#
+
+import fileinput
+import getopt
+import sys
+
+#
+# Parse options
+#
+opts, args = getopt . getopt (sys . argv [1:], 'f:')
+for opt, val in opts:
+ if opt == "-f":
+ filename = val
+
+
+#
+# Find the words in 'filename' which can be constructed
+# from the letters in 'letters'.
+#
+def find_words (filename, letters):
+ letters = list (letters . lower () . strip ())
+ for word in open (filename):
+ word = word . strip ()
+ copy = word . lower ()
+ for letter in letters:
+ copy = copy . replace (letter, "", 1)
+ if copy == "":
+ print (word)
+
+
+for line in fileinput . input ('-'):
+ find_words (filename, line . strip ())