Created
September 22, 2014 09:03
-
-
Save aquarla/d5020df8633929629c0b to your computer and use it in GitHub Desktop.
要素数が出来るだけ均等になるように、配列をn個に分割する
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
# -*- coding: utf-8 -*- | |
class Array | |
# 配列を要素数が出来るだけ均等になるようにn個に分割 | |
def split_into(n) | |
sizes = (0...n).collect { |i| | |
(size % n >= (i+1)) ? (size / n + 1) : (size / n) | |
} | |
(0...n).collect { |i| slice(sizes[0,i].inject(0, :+), sizes[i]) } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment