Find A Sub-Word

  • + 0 comments

    Pthon code:

    import re
    
    # Enter your code here. Read input from STDIN. Print output to STDOUT
    n = int(input())
    if not (1 <= n <= 100):
        raise ValueError("Number of lines must be between 1 and 100")
    lines = []
    for i in range(n):
        lines.append(input())
    q = int(input())
    if not (1 <= q <= 100):
        raise ValueError("Queries must be between 1 and 10")
    queries = []
    for i in range(q):
        queries.append(input())
    
    # print(n)
    # print(lines)
    # print(q)
    # print(queries)
    
    patterns = []
    for query in queries:
        pattern = '[A-Za-z0-9_]'+query+'[A-Za-z0-9_]'
        patterns.append(pattern)
    
    sum = 0
    for patten in patterns:
        for line in lines:
            x = re.findall(patten,line)
            sum = sum + len(x)
        print(sum)
        sum = 0