-3

How to make certain frame range (ie. 1-100) break into 4 equal frame ranges (like 1-25, 26-50, 51-75, 75-100 or anything similiar). I need first and last digit from every chunked frame range.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Mateusz Wójt
  • 109
  • 2
  • 15
  • 2
    Welcome to Stack Overflow! I'm sorry, but I find it very hard to figure out what you are asking here. It would help if you included some code to show what you have tried, it'll make it much easier for us to help you. Perhaps you could also take a look at http://whathaveyoutried.com for a great article on how to ask good questions? – Martijn Pieters Sep 11 '12 at 13:40
  • did you have a look at http://stackoverflow.com/questions/2130016/splitting-a-list-of-arbitrary-size-into-only-roughly-n-equal-parts or http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks-in-python or http://stackoverflow.com/questions/925172/best-method-for-dividing-list-into-equal-parts ? – Dr. Jan-Philip Gehrcke Sep 11 '12 at 13:45

1 Answers1

4
def chunk_range(first, last, howmany):
    size = ((last - first + 1) + (howmany - 1)) // howmany
    while first <= last:
        next = first + size
        yield first, min(next - 1, last)
        first = next

list(chunk_range(1, 100, 4))

returns

[(1, 25), (26, 50), (51, 75), (76, 100)]

Do note that this makes all the segments of equal length except the last one - for instance,

list(chunk_range(1, 7, 3))

gives you

[(1, 3), (4, 6), (7, 7)]   # last chunk is only one item

You may instead want to distribute the error along the sequence, a la Bresenham's algorithm.

Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99