aboutsummaryrefslogtreecommitdiff
path: root/challenge-099/paulo-custodio/python/ch-1.py
blob: 930c9dc328790b446cd31c62dead77125d8b63bc (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python

# Challenge 099
#
# TASK #1 > Pattern Match
# Submitted by: Mohammad S Anwar
# You are given a string $S and a pattern $P.
#
# Write a script to check if given pattern validate the entire string.
# Print 1 if pass otherwise 0.
#
# The patterns can also have the following characters:
#
# ? - Match any single character.
# * - Match any sequence of characters.
# Example 1:
# Input: $S = "abcde" $P = "a*e"
# Output: 1
# Example 2:
# Input: $S = "abcde" $P = "a*d"
# Output: 0
# Example 3:
# Input: $S = "abcde" $P = "?b*d"
# Output: 0
# Example 4:
# Input: $S = "abcde" $P = "a*c?e"
# Output: 1

import sys

def match(s, p):
    while True:
        if s=="" and p=="":
            return True
        elif s=="" or p=="":
            return False
        elif p[0]=="?":
            s=s[1:]
            p=p[1:]
        elif p[0]=="*":
            p=p[1:]
            for i in range(0, len(s)):
                if match(s[i:], p):
                    return True
            return False
        elif s[0]!=p[0]:
            return False
        else:
            s=s[1:]
            p=p[1:]

if match(sys.argv[1], sys.argv[2]):
    print(1)
else:
    print(0)