Answer to Question #336468 in Python for Kiran

Question #336468

Write a program to print the following, Given a word W and pattern P, you need to check whether the pattern matches the given word.The word will only contain letters(can be Uppercase and Lowercase). The pattern can contain letters, ? and *.

? : Can be matched with any single letter.
* : Can be matched with any sequence of letters (including an empty sequence).


If the pattern matches with the given word, print True else False.


Sample Input1

3

Hello *l?

Hell He?ll

Hell ?*

Sample Output1

True

False

True


Sample Input2

3

Hello Hell*

Hell He*ll

Hell hell*

Sample Output2

True

True

False


1
Expert's answer
2022-05-02T13:36:48-0400
import re
num = int(input())
ans = []
for _ in range(num):
    w, p = input().split()
    p = p.replace('?', '.').replace('*', '\w*')
    ans.append(bool(re.fullmatch(p, w)))
print(*ans, sep='\n')


Sample Input1:

3

Hello *l?

Hell He?ll

Hell ?*


Sample Output1:

True

False

True


Sample Input2:

3

Hello Hell*

Hell He*ll

Hell hell*


Sample Output2:

True

True

False


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS