Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8519199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:11:02+00:00 2026-06-11T06:11:02+00:00

I have a file and suppose I need to split it in up to

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T06:11:03+00:00Added an answer on June 11, 2026 at 6:11 am
    def calculate(L, N, X):
        n = min(L//X, N)
        return n, L//n
    

    Edit:

    def spread(seq, N=None, X=1):
        """Yield successive subsequences of seq having at least X elements.
    
        If N is specified, the number of subsequences yielded will not exceed N.
    
        The first L % X subsequences yielded (where L = len(seq)) will be longer
        by 1 than the remaining ones.
    
        >>> list(spread('abcdefghij', 4, 3))
        ['abcd', 'efg', 'hij']
        >>> list(spread('abcdefghijklmnopqrstuvwxyz', 4, 7))
        ['abcdefghi', 'jklmnopqr', 'stuvwxyz']
    
        seq    any object supporting len(...) and slice-indexing
        N      a positive integer (default: L)
        X      a positive integer not greater than L (default: 1)
        """
    
        # All error-checking code omitted
    
        L = len(seq)       # length of seq
        assert 0 < X <= L
    
        if N is None: N = L
        assert 0 < N
    
        # A total of n subsequences will be yielded, the first r of which will 
        # have length x + 1, and the remaining ones will have length x.
    
        # if we insist on using calculate()...
        # n, x = calculate(L, N, X)
        # r = L % n
    
        # ...but this entails separate computations of L//n and L%n; may as well
        # do both with a single divmod(L, n)
        n = min(L//X, N)
        x, r = divmod(L, n)
    
        start = 0
        stride = x + 1    # stride will revert to x when i == r
        for i in range(n):
            if i == r: stride = x
            finish = start + stride
            yield seq[start:finish]
            start = finish
        assert start == L
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to split a file(suppose a mp3) into four parts.I have tried this
suppose I have a file: its format should be : number, string1 , [string2]
Suppose that we have file like this: sometext11 sometext12 sometext13 sometext21 sometext22 sometext23 Texts
Suppose I have file which goes like this : a void measure() { a
Suppose I have a file containing some lines: line 1 ... line 2 ...
Suppose I have a file X.h which defines a class X, whose methods are
Suppose I have the following list: ID3_tag = ['Title','Artist','Album','Track'] And I have a file
I am interested in one topic, suppose that we have eight file, each containing
I'm interested in knowing how revision-control systems do merging. Suppose you have a file
Suppose I have a Javascript file function js_main(args){ /* some code */ var x

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.