aboutsummaryrefslogtreecommitdiff
path: root/challenge-099/abigail/python/ch-1.py
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-099/abigail/python/ch-1.py')
-rw-r--r--challenge-099/abigail/python/ch-1.py38
1 files changed, 38 insertions, 0 deletions
diff --git a/challenge-099/abigail/python/ch-1.py b/challenge-099/abigail/python/ch-1.py
new file mode 100644
index 0000000000..092404b0d7
--- /dev/null
+++ b/challenge-099/abigail/python/ch-1.py
@@ -0,0 +1,38 @@
+#!/opt/local/bin/python
+
+#
+# See ../README.md
+#
+
+#
+# Run as python ch-1.py < input-file
+#
+
+import fileinput
+import re
+
+#
+# Read a string and a pattern. Turn the pattern into a regular expression:
+# - '?' becames '.'
+# - '*' becomes '.*'
+# - any non-word character will be escaped
+#
+# Then map the string against the pattern, anchored
+#
+for line in fileinput . input ():
+ (string, pattern) = re . split (r' +', line . strip ())
+ pat = "^"
+ for c in pattern:
+ if c == "?":
+ pat = pat + "."
+ elif c == "*":
+ pat = pat + ".*"
+ elif re . match (r'\W', c):
+ pat = pat + "\\" + c
+ else:
+ pat = pat + c
+ pat = pat + "$"
+ if re . match (pat, string):
+ print (1)
+ else:
+ print (0)