I have a problem statement:
splitfile(filename,numberoffiles)
A file of 13 lines, split in 3, would have output files of length 4, 4 and 5 if they cannot be evenly distributed. ( Can not have a difference greater than 1 line if they cant be evenly distributed)
I started learning python and I have to create a function that will split a file into smaller ones as specified in the parameters.
The thing I am having trouble with is I do not know how to approach this scenario due to it being based off of number of files and the concept of having a difference greater than 1 being non permissible.
解决方案
The essence of the question (as I understand it) is how to determine the number of lines that each output file will contain. This is what I came up with for Python 3.4.3:
def get_line_counts(total_lines, number_of_files):
base_size = total_lines // number_of_files
line_count_list = [base_size for i in range(number_of_files)]
files_with_an_extra_line = total_lines % number_of_files
for i in range(files_with_an_extra_line):
line_count_list[len(line_count_list) - (i + 1)] += 1
return line_count_list
for i, n in enumerate(get_line_counts(13, 3)):
print("file {0} will contain {1} line(s)".format(i, n))
resulting in
file 0 will contain 4 line(s)
file 1 will contain 4 line(s)
file 2 will contain 5 line(s)
The rest of the code would just be basic file I/O: read n lines from an input text file and write them to an output text file.