I write a helpers.py as following. ok to compare to files with lines. but i don't know how to link it with compare.py. I know the staff said no need to open the file, the compare did it. But how do i load the string (a, b) from compare.py? Thanks.

from sys import argv
import sys

file1 = sys.argv[1]
file2 = sys.argv[2]

def lines(a, b):

"""Return lines in both a and b"""

    return result
alines = set()
with open(file1, "r") as file1:
    for line in file1:

blines = set()
with open(file2, "r") as file2:
    for lines2 in file2:

result = alines & blines
print(lines(alines, blines))

So I'll look into compare.

from helpers import lines, sentences, substrings

This line imports the three functions you are meant to implement. helpers.py gets executed, and the content of variables lines, sentences, and substrings are imported into compare's namespace, making them available by their name.

Unlike bleep, it's not your job to open the files, compare does this for you, assigning the file content to variables file1 and file2. Those are then passed to your functions:

    # Compare files
    if args["lines"]:
        matches = lines(file1, file2)
    elif args["sentences"]:
        matches = sentences(file1, file2)
    elif args["substrings"]:
        matches = substrings(file1, file2, args["substrings"])

So implement the three functions, and have them return the lists as per specification.

You would never call helpers.py directly, but only by calling compare as described in the problem description, passing the names of two files containing the content to compare. It's much like in speller, where you wrote your implementation of a trie or hash table in another file that was not meant to be executable on its own.

  • Thank you very much~! – user21342 Nov 23 '19 at 12:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .