Skip to content

Instantly share code, notes, and snippets.

@Peilonrayz
Last active November 21, 2017 10:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Peilonrayz/857ca8649d34a8a8de4494d9e0eb8d06 to your computer and use it in GitHub Desktop.
Save Peilonrayz/857ca8649d34a8a8de4494d9e0eb8d06 to your computer and use it in GitHub Desktop.
Example alturnate solution to https://codereview.stackexchange.com/q/180920
from itertools import groupby
def sublist_size(max_size):
cur_size = 0
id_ = 0
def inner(n):
nonlocal cur_size, id_
if len(n) > max_size:
raise ValueError(f'Use a bigger size than {max_size}')
cur_size += len(n)
if cur_size > max_size:
cur_size = len(n)
id_ += 1
return id_
return inner
data = ["1", "22", "333", "4444", "55555", "666666", "7777777", "88888888"]
for n in range(17):
try:
print(n, [list(v) for k, v in groupby(data, sublist_size(n))])
except ValueError as e:
print(e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment