Use python to do this problem Write a function named multise

Use python to do this problem

Write a function named multi_search, that takes two open files as arguments and returns a diet whose (a) keys are strings that are regular expression patterns read from the first file, and whose (b) associated values are a list of line numbers from the second file: lines that each key pattern matches (actually, use the search not match function: so the match is not required to begin at the start of the line). The list should show these line numbers in ascending order (which can be done without sorting). There are many ways to solve this problem. For example if the files pats1.txt and texts1.txt store the information shown below, then calling the function as multi_search(open(\'pats. txt\'), open (\'texts. txt\')) returns the following diet: {\'^[a-Z]\': [1, 2, 5, 6], [1, 3, 5, 6], \'\\\\w*(es|s)\\\\W\': [5, 6]}

Solution

pats.txt

^[A-Z]
\\.$
[a-z]\\w*(es|s)\\W


solution.py


import re
from goody import irange
from collections import defaultdict

# Before running the driver on the bsc.txt file, ensure you have put a regular
#   expression pattern in the files repattern1a.txt, repattern1b.txt, and
#   repattern2a.txt.

#result in ascending order (duplicates allowed)
def pages (page_spec : str) -> [int]:
    result = [ ]
    assert \',,\' not in page_spec
    pages_spec = page_spec.split(\',\')
    for item in pages_spec:
        assert not item.split(\':\')[0].startswith(\'0\') and not item.split(\':\')[-1].startswith(\'0\')
        assert not item.split(\'-\')[-1].startswith(\'0\') and not item.split(\'-\')[0].startswith(\'0\')
        assert not \':-\' or not \'-:\' in item
        if \'-\' in item:
            item = item.split(\'-\')
            assert item[-1] > item[0]
            difference = int(item[-1]) - int(item[0])
            result.append(int(item[0]))
            for i in range(difference):
                next = int(item[0]) + (i+1)
                result.append(next)
        elif \':\' in item:
            item = item.split(\':\')
            result.append(int(item[0]))
            total = int(item[-1]) + int(item[0])
            difference = total - int(item[0])
            for i in range(difference):
                next = int(item[0]) + (i+1)
                result.append(next)
        else:
            result.append(int(item))
    result.sort()
    return result

def multi_search(pat_file : open, text_file : open) -> [(int,str,{int})]:
    prefix = [(i,re.compile(p.rstrip())) for i,p in enumerate(pat_file,1)]
    result = []
    for number,line in enumerate(text_file,1):
        line = line.rstrip()
        pattern = [i for i,p in prefix if p.search(line)]
        if pattern:
            result.append((number,line,pattern))
    return result


  


if __name__ == \'__main__\':
    print(multi_search(open(\"pats.txt\"),open(\"texts.txt\")))
    import driver
#     print(multi_search(open(\'pats.txt)\')))
#     driver.default_show_traceback = True
#     driver.default_show_exception = True
#     driver.default_show_exception_message = True
    driver.driver()

tests.txt
See Spot.
See Snoopy
run.
(pause)
Run dogs run.
Dogs are great.

Use python to do this problem Write a function named multi_search, that takes two open files as arguments and returns a diet whose (a) keys are strings that are
Use python to do this problem Write a function named multi_search, that takes two open files as arguments and returns a diet whose (a) keys are strings that are

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site