aboutsummaryrefslogtreecommitdiff
path: root/challenge-099/abigail/python
diff options
context:
space:
mode:
author冯昶 <seaker@qq.com>2021-03-15 18:13:51 +0800
committer冯昶 <seaker@qq.com>2021-03-15 18:13:51 +0800
commit8b6be37fe4dac8b4c6489a95e55514b76b298d15 (patch)
treeae36c8ec2c71f606c0e36adaa19dba366a68a0b4 /challenge-099/abigail/python
parent865acfd056fb6f409ec6b1a81d60b931cbcb69fe (diff)
parentc9aec2da6bcb04b488183f09ca94bee488557aff (diff)
downloadperlweeklychallenge-club-8b6be37fe4dac8b4c6489a95e55514b76b298d15.tar.gz
perlweeklychallenge-club-8b6be37fe4dac8b4c6489a95e55514b76b298d15.tar.bz2
perlweeklychallenge-club-8b6be37fe4dac8b4c6489a95e55514b76b298d15.zip
Merge branch 'master' of github.com:seaker/perlweeklychallenge-club
Diffstat (limited to 'challenge-099/abigail/python')
-rw-r--r--challenge-099/abigail/python/ch-1.py38
-rw-r--r--challenge-099/abigail/python/ch-2.py42
2 files changed, 80 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)
diff --git a/challenge-099/abigail/python/ch-2.py b/challenge-099/abigail/python/ch-2.py
new file mode 100644
index 0000000000..c861e7805d
--- /dev/null
+++ b/challenge-099/abigail/python/ch-2.py
@@ -0,0 +1,42 @@
+#!/opt/local/bin/python
+
+#
+# See ../README.md
+#
+
+#
+# Run as python ch-1.py < input-file
+#
+
+import fileinput
+import re
+
+#
+# Recursively count matches:
+# - If either the string or the pattern is empty, there are no matches.
+# - Else, + count the matches if we don't match at the first character
+# if the string.
+# + if the first character of the string equals the first
+# character of the pattern:
+# o add 1 if the pattern is just one character long
+# o else, add the number of matches starting from the
+# then next character in the string, and the next
+# character in the pattern.
+#
+def matches (string, pattern):
+ if len (string) == 0 or len (pattern) == 0:
+ return 0
+
+ count = matches (string [1:], pattern)
+
+ if string [0] == pattern [0]:
+ if len (pattern) == 1:
+ count = count + 1
+ else:
+ count = count + matches (string [1:], pattern [1:])
+
+ return count
+
+for line in fileinput . input ():
+ (string, pattern) = re . split (r' +', line . strip ())
+ print (matches (string, pattern))