python-tutorial icon indicating copy to clipboard operation
python-tutorial copied to clipboard

I/O module, solution of exercise wrong?

Open GCatarina opened this issue 1 year ago • 0 comments

In the exercise "solution_read_write_file", the accepted solution is counting "\n" as a character, in contrast to the example provided.

More clearly, the following solution passes the test:

def solution_read_write_file(input_file: Path, output_file: Path) -> None:
    """
    Write your solution here
    """
    file_r = open(input_file, "r")
    file_w = open(output_file, "w")
    for line in file_r:
        file_w.write(line.strip("\n") + ", " + str(len(line)) + "\n")
        print(line.strip("\n") + ", " + str(len(line)) + "\n") #print output to check
        #file_w.write(line.strip("\n") + ", " + str(len(line)-line.count("\n")) + "\n")
    file_r.close()
    file_w.close()
    return None

and its output is

this, 5 
file, 5 
has multiple, 13 
lines, 6 
how many, 9 
lines, 6 
does this file, 15 
have?, 5

I think the commented line should be the right solution instead.

GCatarina avatar Aug 27 '23 13:08 GCatarina