-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwildcard.py
More file actions
67 lines (64 loc) · 2.42 KB
/
wildcard.py
File metadata and controls
67 lines (64 loc) · 2.42 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
def wildcard(l1, l2):
index1 = 0
index2 = 0
while (index1 < len(l1) and index2 < len(l2)):
if l1[index1] == l2[index2]:
if index1 == len(l1) - 1 and index2 < len(l2) - 1:
index2 += 1
continue
if index2 == len(l2) - 1 and index1 < len(l1) - 1:
index1 += 1
continue
index1 += 1
index2 += 1
continue
if l1[index1] == "_":
if len(l2) < len(l1):
print "false"
return False
if index1 == len(l1) - 1 and index2 < len(l2):
print "true"
return True
if index1 < len(l1) and index2 >= len(l2):
print "False"
return False
index1 += 1
index2 += 1
for i in range(index2, len(l2)):
if l1[index1] == l2[i]:
index2 = i + 1
index1 += 1
break
if i == len(l2) - 1:
print "false"
return False
if l2[index2] == "_":
if len(l1) < len(l2):
print "false"
return False
if index2 == len(l2) - 1 and index1 < len(l1):
print "true"
return True
if index2 < len(l2) and index1 >= len(l1):
print "False"
return False
index1 += 1
index2 += 1
for i in range(index1, len(l1)):
if l2[index2] == l1[i]:
index1 = i + 1
index2 += 1
break
if i == len(l1) - 1:
print "false"
return False
if index1 == len(l1) - 1 or index2 == len(l2) - 1:
print "true"
return True
wildcard(["_", "bottom", "jeans"], ["apple", "pear", "bottom", "jeans"]) #-> true
wildcard(["apple", "bottom", "_"], ["apple", "bottom"]) #-> false
wildcard(["apple", "_", "bottom", "_", "jeans"], ["apple", "pear", "chipotle", "bottom", "shirts", "jeans"])
wildcard(["apple", "bottom", "_"], ["apple", "bottom", "jeans", "pants"]) #-> true
wildcard(["apple", "bottom", "jeans"], ["apple", "_", "jeans"]) #-> true
wildcard(["apple", "_", "jeans"], ["apple", "jeans"]) #-> false
wildcard(["apple", "_", "jeans"], ["apple", "pear", "bottom", "jeans"]) #-> true