-
-
Save Peilonrayz/857ca8649d34a8a8de4494d9e0eb8d06 to your computer and use it in GitHub Desktop.
Example alturnate solution to https://codereview.stackexchange.com/q/180920
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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