I have a file and suppose I need to split it in up to N smaller files and the smallest chunk should have at least X bytes and all the files should have (almost) the same size:
So using e.g. a string ‘abcdefghij’ with N=4 and X=3 will return [‘abcd’, ‘efg’, ‘hij’] because:
3 chunks < 4 chunks
4 chars > 3 chars
I wrote a split function, but it sometimes create one extra string so I should probably pass the x value instead of calculating there.
def split(string, n):
x = len(string)//n
return [string[i:i+x] for i in range(0, len(string), x)]
The real problem is how to calculate the number of chunks to cut the file with a minimum number of bytes.
def calculate(length, max_n, min_x):
n, x = ...
return n, x
Is there a simple known algorithm to do this kind of action?
Actually: the files doesn’t need to differ in 1 byte because I want to maximize the number of chunks.
Edit: