Skip to content

Instantly share code, notes, and snippets.

@zenwerk
Created September 27, 2013 08:51
Show Gist options
  • Save zenwerk/6725853 to your computer and use it in GitHub Desktop.
Save zenwerk/6725853 to your computer and use it in GitHub Desktop.
与えらた数をバランスよくN分割したリストを返す関数。
# -*- coding:utf-8 -*-
def divide(num, by):
""" num を バランスよく by分割する関数
e,g:num=13, by=2 => [7, 6]
num=30, by=4 => [8, 8, 7, 7]
"""
quotient, rest = divmod(num, by)
ans = [quotient for i in range(by)]
for i in [i % len(ans) for i in range(rest)]:
ans[i] += 1
return ans
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment