Question: The code needs to be expressed as a function. def split_by_n(fname,n=3): assert isinstance(n,int) assert isinstance(fname,basestring) # YOUR CODE HERE raise NotImplementedError() You have a file

The code needs to be expressed as a function.
def split_by_n(fname,n=3): assert isinstance(n,int) assert isinstance(fname,basestring) # YOUR CODE HERE raise NotImplementedError()
You have a file that needs to be divided into n chunks. While it would be straightforward to split the file into equal-bytes sizes and then write those chunks to file, you cannot write any incomplete lines to the files. This means that all of the n files that you create must have no truncated lines and must (as closely as possible) be equally sized. If a split of a certain byte-size would result in a truncated line, then you can back off and only write the previous complete line. You can save the rest of it for the next chunk. You can download Metamorphosis, by Franz Kafka as the sample text. The file is of size 141420 bytes. Splitting into three pieces gives the following files and their respective sizes: filename 47102 pg5200.txt_00.txt 47153 pg5200.txt 01.txt 47165 pg5200.txt 02.txt size The last line of the pg5200.txt_ee.txt is the following: her, she hurried out again and even turned the key in the lock so The last line of the pg5200.txt 01.txt is the following: and the hand that held it. The woman most likely meant to fetch the As a final hint, splitting the same file into eight parts gives the following: size filename 17644 pg5200.txt_0.tt 17655 pg5200.txt 1.txt 17694 pg5200.txt_2.txt 17699 pg5200.txt_3.txt You have a file that needs to be divided into n chunks. While it would be straightforward to split the file into equal-bytes sizes and then write those chunks to file, you cannot write any incomplete lines to the files. This means that all of the n files that you create must have no truncated lines and must (as closely as possible) be equally sized. If a split of a certain byte-size would result in a truncated line, then you can back off and only write the previous complete line. You can save the rest of it for the next chunk. You can download Metamorphosis, by Franz Kafka as the sample text. The file is of size 141420 bytes. Splitting into three pieces gives the following files and their respective sizes: filename 47102 pg5200.txt_00.txt 47153 pg5200.txt 01.txt 47165 pg5200.txt 02.txt size The last line of the pg5200.txt_ee.txt is the following: her, she hurried out again and even turned the key in the lock so The last line of the pg5200.txt 01.txt is the following: and the hand that held it. The woman most likely meant to fetch the As a final hint, splitting the same file into eight parts gives the following: size filename 17644 pg5200.txt_0.tt 17655 pg5200.txt 1.txt 17694 pg5200.txt_2.txt 17699 pg5200.txt_3.txt
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
