I have an assignment where I'm asked to modify some code. The original function is this:
def selectivelyCopy(inputFile,outputFile,predicate):
linesCopied = 0
for line in inputFile:
if predicate(line):#test the line with the predicate
outputFile.write(line)
linesCopied+=1
inputFile.close()
return linesCopied
Now I am suppose to add the parameter transform, a function that takes in as its parameter a string, and returns a string according to the transformation specified by the user. If transform is omitted from the function call, lines from the input file are written unchanged.
Here is what I have so far:
def selectivelyCopy2(inputFile,outputFile,predicate, transform):
def transform(x = lambda x: x):
return(x)
linesCopied = 0
for line in inputFile:
if predicate(line): #test the line with the predicate
outputFile.write(line)
linesCopied+=1
inputFile.close()
return linesCopied
I'm not sure where to proceed from here. I think I'm suppose to read the input file line, but write the transformed line...or something?