Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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.txtStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started