aboutsummaryrefslogtreecommitdiff
path: root/challenge-099/abigail/python/ch-1.py
blob: 092404b0d73fb1f98e863f660e8c7cfca5d24c1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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)