Skip to content

Instantly share code, notes, and snippets.

@sonnyksimon
Created September 30, 2024 15:26
Show Gist options
  • Save sonnyksimon/bc0bc95f9270112045ff4da5b5737451 to your computer and use it in GitHub Desktop.
Save sonnyksimon/bc0bc95f9270112045ff4da5b5737451 to your computer and use it in GitHub Desktop.
Chunking combinations and simple lists
#!/usr/bin/env python
class SimpleChunker:
"""For chunking simple lists"""
def __init__(self, size, chunksize):
self.size = size
self.chunksize = chunksize
self.chunk = 0
self.total = size
def gen_chunks(self):
self.chunk += 1
begin = (self.chunk - 1) * self.chunksize
end = begin + self.chunksize
if end > self.size:
end = self.size
if begin > self.size:
begin = self.size
return range(begin,end)
def get_chunk(self):
return list(self.gen_chunks())
def chunk_available(self):
return self.chunk * self.chunksize <= self.total
class CombinationChunker:
"""For chunking combinations"""
def __init__(self, size, chunksize):
self.size = size
self.chunksize = chunksize
self.chunk = 0
self.total = int(self.size * (self.size - 1) / 2)
def gen_chunks(self):
self.chunk += 1
t = self.chunk*self.chunksize
stop = 1
for i in range(self.size - 1):
ii = self.size - 1 -i
if t > ii:
t -= ii
stop += 1
else:
break
z0 = (self.chunk-1)*self.chunksize
z = self.chunk*self.chunksize
for x in range(stop):
h = self.size - 1 -x
p = 1 + x
if z0+1 > h:
begin = self.size
else:
if z0 < 0:
z0 = 0
begin = z0 + p
if z+1 > h:
end = self.size
else:
end = z + p
z0 -= h
z -= h
for y in range(begin,end):
yield((x,y))
def get_chunk(self):
return list(self.gen_chunks())
def chunk_available(self):
return self.chunk*self.chunksize <= self.total
def main():
print("\nCHUNKED ITEM GENERATION")
c = SimpleChunker(10,3)
print("Chunk Available" if c.chunk_available() else "Finished", c.get_chunk()) #=> (0,3)
print("Chunk Available" if c.chunk_available() else "Finished", c.get_chunk()) #=> (3,6)
print("Chunk Available" if c.chunk_available() else "Finished", c.get_chunk()) #=> (6,9)
print("Chunk Available" if c.chunk_available() else "Finished", c.get_chunk()) #=> (9,10)
print("Chunk Available" if c.chunk_available() else "Finished", c.get_chunk()) #=> (10,10)
print("Chunk Available" if c.chunk_available() else "Finished", c.get_chunk()) #=> (10,10)
print("\nCHUNKED COMBINATION GENERATION")
b = CombinationChunker(10, 3)
print("Chunk Available" if b.chunk_available() else "Finished", b.get_chunk()) #=> (1,4)
print("Chunk Available" if b.chunk_available() else "Finished", b.get_chunk()) #=> (4,7)
print("Chunk Available" if b.chunk_available() else "Finished", b.get_chunk()) #=> (7,10)
print("Chunk Available" if b.chunk_available() else "Finished", b.get_chunk()) #=> (10,10) (2,5)
print("Chunk Available" if b.chunk_available() else "Finished", b.get_chunk()) #=> (10,10) (5,8)
print("Chunk Available" if b.chunk_available() else "Finished", b.get_chunk()) #=> (10,10) (8,10) (3,4)
print("Chunk Available" if b.chunk_available() else "Finished", b.get_chunk()) #=> (10,10) (10,10) (4,7)
print("Chunk Available" if b.chunk_available() else "Finished", b.get_chunk()) #=> (10,10) (10,10) (7,10)
print("Chunk Available" if b.chunk_available() else "Finished", b.get_chunk()) #=> (10,10) (10,10) (10,10) (4,7)
print("Chunk Available" if b.chunk_available() else "Finished", b.get_chunk()) #=> (10,10) (10,10) (10,10) (7,10) ...
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment