From 85799c6783f520b36021acf7e2cd93283056fe28 Mon Sep 17 00:00:00 2001 From: Abigail Date: Sat, 6 Mar 2021 18:47:02 +0100 Subject: Python solution for week 4, part 2 --- challenge-004/abigail/python/ch-2.py | 40 ++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 challenge-004/abigail/python/ch-2.py (limited to 'challenge-004/abigail/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 ()) -- cgit